Annotation of parser3/src/classes/op.C, revision 1.286
1.1 paf 1: /** @file
1.9 paf 2: Parser: parser @b operators.
1.1 paf 3:
1.280 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.262 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.98 paf 6: */
1.1 paf 7:
1.13 paf 8: #include "classes.h"
1.129 paf 9: #include "pa_vmethod_frame.h"
10:
1.1 paf 11: #include "pa_common.h"
1.134 paf 12: #include "pa_os.h"
1.1 paf 13: #include "pa_request.h"
14: #include "pa_vint.h"
15: #include "pa_sql_connection.h"
1.79 paf 16: #include "pa_vdate.h"
1.105 paf 17: #include "pa_vmethod_frame.h"
1.129 paf 18: #include "pa_vclass.h"
1.144 paf 19: #include "pa_charset.h"
1.279 moko 20: #include "pa_varray.h"
1.1 paf 21:
1.273 moko 22: #ifdef HAVE_SYSLOG
23: #include "syslog.h"
24: #endif
25:
1.286 ! moko 26: volatile const char * IDENT_OP_C="$Id: op.C,v 1.285 2026/09/26 19:06:55 moko Exp $";
1.18 parser 27:
1.10 paf 28: // defines
29:
1.82 paf 30: #define CASE_DEFAULT_VALUE "DEFAULT"
1.11 paf 31:
1.10 paf 32: // class
33:
1.113 paf 34: class VClassMAIN: public VClass {
1.10 paf 35: public:
1.129 paf 36: VClassMAIN();
1.10 paf 37: };
38:
1.129 paf 39: // defines for statics
40:
41: #define SWITCH_DATA_NAME "SWITCH-DATA"
42: #define CACHE_DATA_NAME "CACHE-DATA"
1.164 paf 43:
1.129 paf 44: #define EXCEPTION_VAR_NAME "exception"
45:
46: // statics
47:
48: //^switch ^case
49: static const String switch_data_name(SWITCH_DATA_NAME);
50: //^cache
51: static const String cache_data_name(CACHE_DATA_NAME);
52:
53: static const String exception_var_name(EXCEPTION_VAR_NAME);
54:
1.164 paf 55:
1.136 paf 56: // local defines
57:
58: #define CACHE_EXCEPTION_HANDLED_CACHE_NAME "cache"
59:
1.10 paf 60: // methods
61:
1.129 paf 62: static void _if(Request& r, MethodParams& params) {
1.200 moko 63: size_t max_param=params.count()-1;
64: size_t i=0;
65: do {
66: bool condition=params.as_bool(i, "condition must be expression", r);
67: if(condition) {
1.238 moko 68: r.process_write(params[i+1]);
1.200 moko 69: return;
70: }
71: i+=2;
72: } while (i < max_param);
73:
74: if(i == max_param)
1.238 moko 75: r.process_write(params[i]);
1.1 paf 76: }
77:
1.129 paf 78: static void _untaint(Request& r, MethodParams& params) {
79: String::Language lang;
80: if(params.count()==1)
1.241 moko 81: lang=String::L_AS_IS; // mark as simply 'as-is'. useful in html from sql
1.175 misha 82: else
1.286 ! moko 83: lang=String::name2lang(params.as_string(0, "lang must be string"));
1.1 paf 84:
1.241 moko 85: Value& vbody=params.as_junction(params.count()-1, "body must be code");
86: Value& result=r.process(vbody);
87:
88: if(const String* string=result.get_string()){
89: String &untainted=*new String();
90: string->append_to(untainted, lang); // mark all tainted to specified language
1.243 moko 91: r.write(untainted);
1.241 moko 92: } else
1.243 moko 93: r.write(result); // this is not normal, just backward compatibility
1.1 paf 94: }
95:
1.129 paf 96: static void _taint(Request& r, MethodParams& params) {
97: String::Language lang;
98: if(params.count()==1)
1.175 misha 99: lang=String::L_TAINTED; // mark as simply 'tainted'. useful in table:create
100: else
1.286 ! moko 101: lang=String::name2lang(params.as_string(0, "lang must be string"));
1.1 paf 102:
103: {
1.129 paf 104: Value& vbody=params.as_no_junction(params.count()-1, "body must not be code");
1.1 paf 105:
1.191 misha 106: String result(vbody.as_string(), lang); // force result language to specified
1.243 moko 107: r.write(result);
1.1 paf 108: }
109: }
110:
1.205 moko 111: static void _apply_taint(Request& r, MethodParams& params) {
1.286 ! moko 112: String::Language lang=params.count()==1 ? String::L_AS_IS : String::name2lang(params.as_string(0, "lang must be string"));
1.205 moko 113: const String &sbody=params.as_string(params.count()-1, "body must be string");
1.213 moko 114: String::Body result_body=sbody.cstr_to_string_body_untaint(lang, r.connection(false), &r.charsets);
1.243 moko 115: r.write(*new String(result_body, String::L_AS_IS));
1.205 moko 116: }
117:
1.129 paf 118: static void _process(Request& r, MethodParams& params) {
119: Method* main_method;
120:
121: size_t index=0;
122: Value* target_self;
123: Value& maybe_target_self=params[index];
124: if(maybe_target_self.get_string() || maybe_target_self.get_junction())
125: target_self=&r.get_method_frame()->caller()->self();
126: else {
1.233 moko 127: target_self=&maybe_target_self;
128: if(params.count()==1)
129: throw Exception(PARSER_RUNTIME, 0, "no body specified");
130: index++;
1.129 paf 131: }
132:
1.1 paf 133: {
1.197 misha 134: VStateless_class *target_class=target_self->get_class();
1.116 paf 135: if(!target_class)
1.232 moko 136: throw Exception(PARSER_RUNTIME, 0, "no target class");
1.114 paf 137:
1.1 paf 138: // temporary zero @main so to maybe-replace it in processed code
1.129 paf 139: Temp_method temp_method_main(*target_class, main_method_name, 0);
140:
141: const String* main_alias=0;
1.146 paf 142: const String* file_alias=0;
143: int line_no_alias_offset=0;
1.216 moko 144: bool allow_class_replace=false;
1.211 misha 145:
146: size_t options_index=index+1;
147: if(options_index<params.count())
148: if(HashStringValue* options=params.as_hash(options_index)) {
1.216 moko 149: for(HashStringValue::Iterator i(*options); i; i.next() ){
150: String::Body key=i.key();
151: Value* value=i.value();
152: if(key == "main") {
153: main_alias=&value->as_string();
154: } else if(key == "file") {
155: file_alias=&value->as_string();
156: } else if(key == "lineno") {
157: line_no_alias_offset=value->as_int();
158: } else if(key == "replace") {
1.236 moko 159: allow_class_replace=r.process(*value).as_bool();
1.275 moko 160: } else
161: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.211 misha 162: }
1.146 paf 163: }
1.129 paf 164:
1.258 moko 165: uint processe_file_no=file_alias ? r.register_file(r.full_disk_path(*file_alias)) : pseudo_file_no__process;
1.146 paf 166: // process...{string}
167: Value& vjunction=params.as_junction(index, "body must be code");
168: // evaluate source to process
169: const String& source=r.process_to_string(vjunction);
1.216 moko 170:
171: Temp_class_replace class_replace(r, allow_class_replace);
172:
1.241 moko 173: r.use_buf(*target_class, source.untaint_cstr(String::L_PARSER_CODE, r.connection(false)), main_alias, processe_file_no, line_no_alias_offset);
1.1 paf 174:
1.73 paf 175: // main_method
1.129 paf 176: main_method=target_class->get_method(main_method_name);
1.73 paf 177: }
178: // after restoring current-request-lang
179: // maybe-execute @main[]
180: if(main_method) {
1.244 moko 181: METHOD_FRAME_ACTION(*main_method, r.get_method_frame()->caller(), *target_self, {
182: frame.empty_params();
183: r.call(frame);
184: r.write(frame.result());
185: });
1.1 paf 186: }
187: }
188:
1.138 paf 189: static void _rem(Request&, MethodParams& params) {
1.129 paf 190: params.as_junction(0, "body must be code");
1.1 paf 191: }
192:
1.129 paf 193: static void _while(Request& r, MethodParams& params) {
1.196 misha 194: InCycle temp(r);
1.164 paf 195:
1.170 misha 196: Value& vcondition=params.as_expression(0, "condition must be number, bool or expression");
197:
1.162 paf 198: Value& body_code=params.as_junction(1, "body must be code");
199: Value* delim_maybe_code=params.count()>2?¶ms[2]:0;
1.1 paf 200:
201: // while...
202: int endless_loop_count=0;
1.186 misha 203: if(delim_maybe_code){ // delimiter set
1.185 misha 204: bool need_delim=false;
205: while(true) {
1.235 moko 206: if(++endless_loop_count>=pa_loop_limit) // endless loop?
207: throw Exception(PARSER_RUNTIME, 0, "endless loop detected");
1.185 misha 208:
1.237 moko 209: if(!r.process(vcondition).as_bool())
1.185 misha 210: break;
1.1 paf 211:
1.234 moko 212: Value& sv_processed=r.process(body_code);
1.249 moko 213: TempSkip4Delimiter skip(r);
1.186 misha 214:
1.185 misha 215: const String* s_processed=sv_processed.get_string();
1.186 misha 216: if(s_processed && !s_processed->is_empty()) { // we have body
1.185 misha 217: if(need_delim) // need delim & iteration produced string?
1.243 moko 218: r.write(r.process(*delim_maybe_code));
1.185 misha 219: else
220: need_delim=true;
221: }
1.243 moko 222: r.write(sv_processed);
1.1 paf 223:
1.249 moko 224: if(skip.check_break())
1.185 misha 225: break;
1.162 paf 226: }
1.185 misha 227: } else {
228: while(true) {
1.235 moko 229: if(++endless_loop_count>=pa_loop_limit) // endless loop?
230: throw Exception(PARSER_RUNTIME, 0, "endless loop detected");
1.185 misha 231:
1.237 moko 232: if(!r.process(vcondition).as_bool())
1.185 misha 233: break;
1.164 paf 234:
1.185 misha 235: r.process_write(body_code);
236:
1.249 moko 237: if(r.check_skip_break())
1.185 misha 238: break;
239: }
1.1 paf 240: }
241: }
242:
1.129 paf 243: static void _use(Request& r, MethodParams& params) {
1.270 moko 244: const String& file_name=params.as_file_name(0);
1.199 misha 245:
1.256 moko 246: const String* use_origin=0;
1.216 moko 247: bool allow_class_replace=false;
1.256 moko 248: bool load_auto_p=false;
1.216 moko 249:
250: if(params.count()==2)
251: if(HashStringValue* options=params.as_hash(1)) {
252: for(HashStringValue::Iterator i(*options); i; i.next() ){
253: String::Body key=i.key();
254: Value* value=i.value();
1.256 moko 255: if(key == "origin") {
256: use_origin=&value->as_string();
1.275 moko 257: } else if(key == "replace") {
1.236 moko 258: allow_class_replace=r.process(*value).as_bool();
1.275 moko 259: } else if(key == "main") {
1.256 moko 260: load_auto_p=r.process(*value).as_bool();
1.275 moko 261: } else
1.246 moko 262: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.216 moko 263: }
264: }
265:
1.246 moko 266: if(!use_origin)
267: if(VMethodFrame* caller=r.get_method_frame()->caller())
1.247 moko 268: use_origin=r.get_method_filespec(&caller->method);
1.246 moko 269:
1.216 moko 270: Temp_class_replace class_replace(r, allow_class_replace);
271:
1.269 moko 272: r.use_file(file_name, use_origin, load_auto_p);
1.1 paf 273: }
274:
1.164 paf 275: static void set_skip(Request& r, Request::Skip askip) {
1.196 misha 276: if(!r.get_in_cycle())
1.227 moko 277: throw Exception(askip==Request::SKIP_BREAK ? "parser.break" : "parser.continue", 0, "without cycle");
1.164 paf 278: r.set_skip(askip);
279: }
280:
1.227 moko 281: static void _break(Request& r, MethodParams& params) {
282: if(!params.count() || params.as_bool(0, "condition must be expression", r)) set_skip(r, Request::SKIP_BREAK);
1.164 paf 283: }
284:
1.227 moko 285: static void _continue(Request& r, MethodParams& params) {
286: if(!params.count() || params.as_bool(0, "condition must be expression", r)) set_skip(r, Request::SKIP_CONTINUE);
1.164 paf 287: }
288:
1.249 moko 289: static void _return(Request& r, MethodParams& params) {
1.250 moko 290: VMethodFrame& caller=*r.get_method_frame()->caller();
1.249 moko 291: if(params.count())
1.255 moko 292: r.put_element(caller, Symbols::RESULT_SYMBOL, &r.process(params[0]));
1.250 moko 293: r.set_skip_return(caller);
1.249 moko 294: }
295:
1.129 paf 296: static void _for(Request& r, MethodParams& params) {
1.196 misha 297: InCycle temp(r);
1.164 paf 298:
1.129 paf 299: const String& var_name=params.as_string(0, "var name must be string");
300: int from=params.as_int(1, "from must be int", r);
301: int to=params.as_int(2, "to must be int", r);
1.186 misha 302: Value& body_code=params.as_junction(3, "body must be code");
1.129 paf 303: Value* delim_maybe_code=params.count()>4?¶ms[4]:0;
1.1 paf 304:
1.235 moko 305: if(to-from>=pa_loop_limit) // too long loop?
306: throw Exception(PARSER_RUNTIME, 0, "endless loop detected");
1.57 paf 307:
1.129 paf 308: VInt* vint=new VInt(0);
1.154 paf 309:
310: VMethodFrame& caller=*r.get_method_frame()->caller();
1.218 moko 311: r.put_element(caller, var_name, vint);
1.186 misha 312: if(delim_maybe_code){ // delimiter set
1.185 misha 313: bool need_delim=false;
314:
315: for(int i=from; i<=to; i++) {
316: vint->set_int(i);
317:
1.234 moko 318: Value& sv_processed=r.process(body_code);
1.249 moko 319: TempSkip4Delimiter skip(r);
1.186 misha 320:
1.185 misha 321: const String* s_processed=sv_processed.get_string();
1.186 misha 322: if(s_processed && !s_processed->is_empty()) { // we have body
1.185 misha 323: if(need_delim) // need delim & iteration produced string?
1.243 moko 324: r.write(r.process(*delim_maybe_code));
1.185 misha 325: else
326: need_delim=true;
327: }
1.243 moko 328: r.write(sv_processed);
1.1 paf 329:
1.249 moko 330: if(skip.check_break())
1.185 misha 331: break;
1.1 paf 332: }
1.185 misha 333: } else {
334: for(int i=from; i<=to; i++) {
335: vint->set_int(i);
336:
337: r.process_write(body_code);
1.164 paf 338:
1.249 moko 339: if(r.check_skip_break())
1.185 misha 340: break;
341: }
1.1 paf 342: }
343: }
344:
1.129 paf 345: static void _eval(Request& r, MethodParams& params) {
346: Value& expr=params.as_junction(0, "need expression");
1.1 paf 347: // evaluate expresion
1.237 moko 348: Value& value_result=r.process(expr).as_expr_result();
1.129 paf 349: if(params.count()>1) {
1.177 misha 350: const String& fmt=params.as_string(1, "fmt must be string").trim();
351: if(fmt.is_empty()){
1.243 moko 352: r.write(value_result);
1.177 misha 353: } else {
1.264 moko 354: r.write(String(format_double(value_result.as_double(), fmt.cstrm())));
1.177 misha 355: }
1.91 paf 356: } else
1.243 moko 357: r.write(value_result);
1.1 paf 358: }
359:
1.129 paf 360: static void _connect(Request& r, MethodParams& params) {
361: Value& url=params.as_no_junction(0, "url must not be code");
362: Value& body_code=params.as_junction(1, "body must be code");
1.1 paf 363:
1.129 paf 364: Table* protocol2driver_and_client=0;
1.197 misha 365: if(Value* sql=r.main_class.get_element(String(MAIN_SQL_NAME))) {
366: if(Value* element=sql->get_element(String(MAIN_SQL_DRIVERS_NAME))) {
1.113 paf 367: protocol2driver_and_client=element->get_table();
368: }
369: }
1.11 paf 370:
1.1 paf 371: // connect
1.142 paf 372: SQL_Connection* connection=SQL_driver_manager->get_connection(url.as_string(),
1.144 paf 373: protocol2driver_and_client,
1.180 misha 374: r.charsets.source().NAME().cstr(),
375: r.request_info.document_root);
1.1 paf 376:
1.214 moko 377: Temp_connection temp_connection(r, connection);
1.63 paf 378:
1.1 paf 379: // execute body
1.53 parser 380: try {
1.189 misha 381: r.process_write(body_code);
1.129 paf 382: connection->commit();
383: connection->close();
1.75 paf 384: } catch(...) { // process problem
1.129 paf 385: connection->rollback();
386: connection->close();
387: rethrow;
1.1 paf 388: }
389: }
390:
1.41 parser 391: #ifndef DOXYGEN
1.129 paf 392: class Switch_data: public PA_Object {
393: public:
394: Request& r;
1.181 misha 395: const String* searching_string;
396: double searching_double;
1.207 misha 397: bool searching_bool;
1.129 paf 398: Value* found;
399: Value* _default;
400: public:
401: Switch_data(Request& ar, Value& asearching):
1.181 misha 402: r(ar)
403: {
404: if(asearching.is_string() || asearching.is_void()){
405: searching_string=&asearching.as_string();
406: searching_double=0;
1.207 misha 407: searching_bool=false;
1.181 misha 408: } else {
409: searching_string=0;
410: searching_double=asearching.as_double();
1.207 misha 411: searching_bool=asearching.is_bool();
1.181 misha 412: }
413: }
1.28 parser 414: };
1.41 parser 415: #endif
1.129 paf 416: static void _switch(Request& r, MethodParams& params) {
1.236 moko 417: Switch_data* data=new Switch_data(r, r.process(params[0]));
1.215 moko 418: Temp_hash_value<HashString<void*>, void*> switch_data_setter(&r.classes_conf, switch_data_name, data);
1.28 parser 419:
1.129 paf 420: Value& cases_code=params.as_junction(1, "switch cases must be code");
1.82 paf 421: // execution of found ^case[...]{code} must be in context of ^switch[...]{code}
422: // because of stacked WWrapper used there as wcontext
1.237 moko 423: r.process(cases_code);
1.147 paf 424: if(Value* selected_code=data->found? data->found: data->_default)
1.245 moko 425: r.process_write(*selected_code);
1.28 parser 426: }
427:
1.129 paf 428: static void _case(Request& r, MethodParams& params) {
429: Switch_data* data=static_cast<Switch_data*>(r.classes_conf.get(switch_data_name));
1.38 parser 430: if(!data)
1.166 misha 431: throw Exception(PARSER_RUNTIME,
1.129 paf 432: 0,
1.38 parser 433: "without switch");
1.28 parser 434:
1.181 misha 435: if(data->found) // matches already was found
436: return;
437:
1.129 paf 438: int count=params.count();
1.184 misha 439: Value* code=¶ms.as_expression(--count, "case result must be code");
440:
441: #ifdef USE_DESTRUCTORS
442: Junction *j=code->get_junction();
443: if (j){
444: code=new VJunction(j->self,j->method,j->method_frame,j->rcontext,j->wcontext,j->code);
445: if (j->wcontext) j->wcontext->attach_junction((VJunction *)code);
446: }
447: #endif
1.83 paf 448:
1.181 misha 449: for(int i=0; i<count; i++){
1.236 moko 450: Value& value=r.process(params[i]);
1.28 parser 451:
1.181 misha 452: if(value.is_string() && value.as_string() == CASE_DEFAULT_VALUE){
1.184 misha 453: data->_default=code;
1.181 misha 454: continue;
1.28 parser 455: }
456:
457: bool matches;
1.181 misha 458: if(data->searching_string)
459: matches=(*data->searching_string) == value.as_string();
1.207 misha 460: else if(data->searching_bool || value.is_bool())
461: matches=(data->searching_double != 0) == value.as_bool();
1.28 parser 462: else
1.181 misha 463: matches=data->searching_double == value.as_double();
1.82 paf 464:
1.181 misha 465: if(matches){
1.184 misha 466: data->found=code;
1.28 parser 467: break;
468: }
469: }
470: }
1.136 paf 471: #ifndef DOXYGEN
472: struct Try_catch_result {
1.234 moko 473: ValueRef processed_code;
1.136 paf 474: const String* exception_should_be_handled;
475:
476: Try_catch_result(): exception_should_be_handled(0) {}
477: };
1.229 moko 478:
479: /// Auto-object used for temporary changing Request::skip.
480: class Temp_skip {
481: Request& frequest;
482: Request::Skip saved_skip;
483: public:
484: Temp_skip(Request& arequest) : frequest(arequest), saved_skip(arequest.get_skip()) {
485: arequest.set_skip(Request::SKIP_NOTHING);
486: }
487: ~Temp_skip() {
488: if(frequest.get_skip() == Request::SKIP_NOTHING)
489: frequest.set_skip(saved_skip);
490: }
491: };
1.136 paf 492: #endif
493:
494: /// used by ^try and ^cache, @returns $exception.handled[string] if any
495: template<class I>
1.234 moko 496: static Try_catch_result try_catch(Request& r, Value& body_code(Request&, I), I info, Value* catch_code, bool could_be_handled_by_caller=false) {
1.136 paf 497: Try_catch_result result;
1.179 misha 498:
1.136 paf 499: if(!catch_code) {
500: result.processed_code=body_code(r, info);
501: return result;
502: }
503:
504: // taking snapshot of try-context
505: Request_context_saver try_context(r);
1.282 moko 506: size_t trace_start=r.exception_trace.top_index();
507:
1.136 paf 508: try {
509: result.processed_code=body_code(r, info);
1.284 moko 510: } catch(const Recursion_limit_exception&) {
511: rethrow;
1.136 paf 512: } catch(const Exception& e) {
1.282 moko 513: size_t trace_bottom=r.exception_trace.bottom_index();
514: size_t trace_top=r.exception_trace.top_index();
1.230 moko 515:
1.281 moko 516: VException& details=*new VException(r, e);
1.230 moko 517:
1.282 moko 518: try_context.restore(); // restoring try-context for catch code
1.285 moko 519: r.recursion_limit_try_restore(); // here to allow "endless" recursion in catch
1.136 paf 520:
1.282 moko 521: r.exception_trace.set_bottom_index(trace_top); // keep the original trace occupied; errors in catch start after it
1.136 paf 522:
523: bool bhandled=false;
1.282 moko 524: try {
525: {
526: Temp_value_element temp(r, *catch_code->get_junction()->method_frame, exception_var_name, &details);
527: Temp_skip temp_skip(r);
528: result.processed_code=r.process(*catch_code);
529: }
530:
531: Value* vhandled=details.handled();
532: if(vhandled) {
533: if(vhandled->is_string()) { // not simple $exception.handled(1/0)?
534: if(bhandled=could_be_handled_by_caller) { // and we can possibly handle it
535: result.exception_should_be_handled=vhandled->get_string(); // considering 'recovered' and let the caller recover
536: }
537: } else
538: bhandled=vhandled->as_bool();
539: }
540: } catch(...) {
1.285 moko 541: try_context.restore(); // restore execution for finally, but keep new error trace.
542: r.recursion_limit_try_restore(); // here to allow "endless" recursion in finally
1.283 moko 543: details.expire_trace();
1.282 moko 544: rethrow;
1.136 paf 545: }
546:
1.283 moko 547: details.expire_trace();
1.230 moko 548: if(!bhandled){
1.282 moko 549: r.exception_trace.set_range(trace_bottom, trace_top);
1.136 paf 550: rethrow;
1.230 moko 551: }
1.285 moko 552:
1.282 moko 553: r.exception_trace.set_range(trace_start, trace_start);
1.136 paf 554: }
1.179 misha 555:
1.136 paf 556: return result;
557: }
1.28 parser 558:
1.234 moko 559: static Value& process_try_body_code(Request& r, Value* body_code) {
1.222 moko 560: return r.process(*body_code);
561: }
562:
1.63 paf 563: // cache--
564:
565: // consts
566:
1.152 paf 567: const int DATA_STRING_SERIALIZED_VERSION=0x0006;
1.63 paf 568:
569: // helper types
570:
571: #ifndef DOXYGEN
572: struct Data_string_serialized_prolog {
573: int version;
1.79 paf 574: time_t expires;
1.63 paf 575: };
576: #endif
577:
1.68 paf 578: void cache_delete(const String& file_spec) {
1.167 misha 579: file_delete(file_spec, false/*fail_on_problem*/);
1.63 paf 580: }
1.69 paf 581:
582: #ifndef DOXYGEN
1.135 paf 583: struct Cache_scope {
1.129 paf 584: public:
1.79 paf 585: time_t expires;
1.135 paf 586: const String* body_from_disk;
1.79 paf 587: };
1.69 paf 588: struct Locked_process_and_cache_put_action_info {
589: Request *r;
1.136 paf 590: Cache_scope *scope;
1.167 misha 591: Value* body_code;
592: Value* catch_code;
1.136 paf 593: const String* processed_code;
1.69 paf 594: };
595: #endif
1.136 paf 596:
597:
598:
1.234 moko 599: static Value& process_cache_body_code(Request& r, Value* body_code) {
600: return r.process(*body_code);
1.136 paf 601: }
602:
1.129 paf 603: /* @todo maybe network order worth spending some effort?
604: don't bothering myself with network byte order,
605: am not planning to be able to move resulting file across platforms
606: */
1.69 paf 607: static void locked_process_and_cache_put_action(int f, void *context) {
608: Locked_process_and_cache_put_action_info& info=
609: *static_cast<Locked_process_and_cache_put_action_info *>(context);
1.129 paf 610:
1.160 paf 611: const String* body_from_disk=info.scope->body_from_disk;
1.69 paf 612: // body->process
1.136 paf 613: Try_catch_result result=try_catch(*info.r,
614: process_cache_body_code, info.body_code,
1.160 paf 615: info.catch_code, body_from_disk!=0 /*we have something old=we can handle=recover later*/);
1.136 paf 616:
617: if(result.exception_should_be_handled) {
618: if(*result.exception_should_be_handled==CACHE_EXCEPTION_HANDLED_CACHE_NAME) {
1.160 paf 619: assert(body_from_disk);
620: info.processed_code=body_from_disk;
1.136 paf 621: } else
1.166 misha 622: throw Exception(PARSER_RUNTIME,
1.136 paf 623: result.exception_should_be_handled,
1.281 moko 624: "$" EXCEPTION_VAR_NAME ".handled value must be "
1.219 moko 625: "either boolean or string '" CACHE_EXCEPTION_HANDLED_CACHE_NAME "'");
1.136 paf 626: } else
1.234 moko 627: info.processed_code=&((Value &)result.processed_code).as_string();
1.69 paf 628:
1.79 paf 629: // expiration time not spoiled by ^cache(0) or something?
1.136 paf 630: if(info.scope->expires > time(0)) {
1.79 paf 631: // string -serialize> buffer
1.136 paf 632: String::Cm serialized=info.processed_code->serialize(
1.129 paf 633: sizeof(Data_string_serialized_prolog));
1.79 paf 634: Data_string_serialized_prolog& prolog=
1.129 paf 635: *reinterpret_cast<Data_string_serialized_prolog *>(serialized.str);
1.79 paf 636: prolog.version=DATA_STRING_SERIALIZED_VERSION;
1.136 paf 637: prolog.expires=info.scope->expires;
1.79 paf 638:
639: // buffer -write> file
1.272 moko 640: if(write(f, serialized.str, serialized.length)<0)
641: throw Exception("file.write", 0, "write failed: %s (%d)", strerror(errno), errno);
1.79 paf 642: } else // expired!
1.136 paf 643: info.scope->expires=0; // flag it so that could be easily checked by caller
1.69 paf 644: }
1.129 paf 645: const String* locked_process_and_cache_put(Request& r,
1.128 paf 646: Value& body_code,
1.136 paf 647: Value* catch_code,
648: Cache_scope& scope,
649: const String& file_spec)
650: {
1.140 paf 651: Locked_process_and_cache_put_action_info info={&r, &scope, &body_code, catch_code, 0};
1.69 paf 652:
1.129 paf 653: const String* result=file_write_action_under_lock(
1.69 paf 654: file_spec,
1.168 misha 655: "cache_put",
656: locked_process_and_cache_put_action,
657: &info,
1.69 paf 658: false/*as_text*/,
659: false/*do_append*/,
1.173 misha 660: false/*block == don't wait till other thread release lock*/,
661: false/*dun throw exception if lock failed*/) ? info.processed_code: 0;
1.168 misha 662:
1.100 paf 663: time_t now=time(0);
1.136 paf 664: if(scope.expires<=now)
1.79 paf 665: cache_delete(file_spec);
666: return result;
1.63 paf 667: }
1.229 moko 668:
1.137 paf 669: #ifndef DOXYGEN
1.135 paf 670: struct Cache_get_result {
671: const String* body;
672: bool expired;
1.137 paf 673: };
674: #endif
1.229 moko 675:
1.271 moko 676: static Cache_get_result cache_get(const String& file_spec, time_t now) {
1.140 paf 677: Cache_get_result result={0, false};
1.135 paf 678:
1.253 moko 679: File_read_result file=file_read_binary(file_spec, false /*fail_on_read_problem*/);
1.252 moko 680: if(file.success && file.length /*ignore reads which are empty due to non-unary open+lockEX conflict with lockSH*/) {
681:
682: Data_string_serialized_prolog& prolog = *reinterpret_cast<Data_string_serialized_prolog *>(file.str);
683:
1.135 paf 684: String* body=new String;
1.252 moko 685: if(file.length>=sizeof(Data_string_serialized_prolog) && prolog.version==DATA_STRING_SERIALIZED_VERSION) {
1.135 paf 686: if(body->deserialize(sizeof(Data_string_serialized_prolog), file.str, file.length)) {
687: result.body=body;
688: result.expired=prolog.expires <= now;
689: }
690: }
1.72 paf 691: }
1.63 paf 692:
1.135 paf 693: return result;
1.63 paf 694: }
1.252 moko 695:
696: static time_t as_expires(Request& r, MethodParams& params, int index, time_t now) {
1.79 paf 697: time_t result;
1.263 moko 698: if(VDate* vdate=dynamic_cast<VDate*>(¶ms[index]))
1.274 moko 699: result=(time_t)vdate->get_time();
1.79 paf 700: else
1.129 paf 701: result=now+(time_t)params.as_double(index, "lifespan must be date or number", r);
1.79 paf 702:
703: return result;
704: }
1.229 moko 705:
1.129 paf 706: static const String& as_file_spec(Request& r, MethodParams& params, int index) {
1.258 moko 707: return r.full_disk_path(params.as_string(index, "filespec must be string"));
1.79 paf 708: }
1.229 moko 709:
1.129 paf 710: static void _cache(Request& r, MethodParams& params) {
1.173 misha 711: if(params.count()==0) {
712: // ^cache[] -- return current expiration time
1.158 paf 713: Cache_scope* scope=static_cast<Cache_scope*>(r.classes_conf.get(cache_data_name));
714: if(!scope)
1.221 moko 715: throw Exception(PARSER_RUNTIME, 0, "expire-time get without cache");
1.243 moko 716: r.write(*new VDate((pa_time_t)scope->expires));
1.158 paf 717: return;
718: }
719:
1.79 paf 720: time_t now=time(0);
721:
1.129 paf 722: if(params.count()==1) {
1.173 misha 723: // ^cache[filename] ^cache(seconds) ^cache[expires date]
1.129 paf 724: if(params[0].is_string()) { // filename?
1.79 paf 725: cache_delete(as_file_spec(r, params, 0));
726: return;
727: }
728:
729: // secods|expires date
1.135 paf 730: Cache_scope* scope=static_cast<Cache_scope*>(r.classes_conf.get(cache_data_name));
731: if(!scope)
1.166 misha 732: throw Exception(PARSER_RUNTIME,
1.129 paf 733: 0,
1.79 paf 734: "expire-time reducing instruction without cache");
735:
1.129 paf 736: time_t expires=as_expires(r, params, 0, now);
1.135 paf 737: if(expires < scope->expires)
738: scope->expires=expires;
1.79 paf 739:
740: return;
1.149 paf 741: } else if(params.count()<3)
1.166 misha 742: throw Exception(PARSER_RUNTIME,
1.149 paf 743: 0,
744: "invalid number of parameters");
1.63 paf 745:
746: // file_spec, expires, body code
1.172 misha 747: const String& file_spec=as_file_spec(r, params, 0);
1.65 paf 748:
1.141 paf 749: Cache_scope scope={as_expires(r, params, 1, now), 0};
1.135 paf 750:
1.215 moko 751: Temp_hash_value<HashString<void*>, void*> cache_scope_setter(&r.classes_conf, cache_data_name, &scope);
1.136 paf 752: Value& body_code=params.as_junction(2, "body_code must be code");
753: Value* catch_code=0;
754: if(params.count()>3)
755: catch_code=¶ms.as_junction(3, "catch_code must be code");
1.63 paf 756:
1.168 misha 757: if(scope.expires>now) {
1.271 moko 758: Cache_get_result cached=cache_get(file_spec, now);
1.69 paf 759:
1.168 misha 760: if(cached.body) { // have cached copy
1.174 misha 761: if(cached.expired) {
762: scope.body_from_disk=cached.body; // storing for user to retrive it with ^cache[]
763: } else {
764: // and it's not expired yet write it out
1.243 moko 765: r.write(*cached.body);
1.168 misha 766: // happy with it
767: return;
768: }
769: }
770:
771: // no cached info or it's already expired
1.173 misha 772:
773: // trying to process it under lock and store result in file
1.174 misha 774: const String* processed_body=locked_process_and_cache_put(r, body_code, catch_code, scope, file_spec);
1.173 misha 775: if(processed_body){
1.174 misha 776: // write it out
1.243 moko 777: r.write(*processed_body);
1.174 misha 778: // happy with it
779: return;
1.173 misha 780: } else {
781: // we fail while get exclusive lock. nvm, we just execute body_code a bit later
1.174 misha 782: }
1.69 paf 783: } else {
1.79 paf 784: // instructed not to cache; forget cached copy
1.68 paf 785: cache_delete(file_spec);
1.69 paf 786: }
1.168 misha 787:
1.222 moko 788: // process without caching
789: if(catch_code){
790: Try_catch_result result=try_catch(r, process_try_body_code, &body_code, catch_code);
1.243 moko 791: r.write(result.processed_code);
1.222 moko 792: } else {
1.243 moko 793: r.write(r.process_to_string(body_code));
1.222 moko 794: }
1.63 paf 795: }
796:
1.129 paf 797: static void _try_operator(Request& r, MethodParams& params) {
798: Value& body_code=params.as_junction(0, "body_code must be code");
799: Value& catch_code=params.as_junction(1, "catch_code must be code");
1.179 misha 800: Value* finally_code=(params.count()==3) ? ¶ms.as_junction(2, "finally_code must be code") : 0;
801:
802: Try_catch_result result;
1.229 moko 803:
1.179 misha 804: try{
1.229 moko 805: // process try and catch code
1.222 moko 806: result=try_catch(r, process_try_body_code, &body_code, &catch_code);
1.284 moko 807: } catch(const Recursion_limit_exception&) {
808: rethrow;
1.179 misha 809: } catch(...){
1.229 moko 810: // process finally code but ignore the result
811: if(finally_code){
1.282 moko 812: size_t trace_bottom=r.exception_trace.bottom_index();
813: size_t trace_top=r.exception_trace.top_index();
814: r.exception_trace.set_bottom_index(trace_top);
1.229 moko 815: Temp_skip temp(r);
1.248 moko 816: /* Value &finally_result= */ r.process(*finally_code);
1.282 moko 817: r.exception_trace.set_range(trace_bottom, trace_top);
1.229 moko 818: }
1.179 misha 819: rethrow;
820: }
1.74 paf 821:
1.229 moko 822: // process finally code
823: if(finally_code){
824: Temp_skip temp(r);
1.234 moko 825: Value& finally_result=r.process(*finally_code);
1.229 moko 826:
1.234 moko 827: // no exception in try/catch or finally, writing processed body_code or catch_code
1.243 moko 828: r.write(result.processed_code);
1.123 paf 829:
1.234 moko 830: // write out processed finally code
1.243 moko 831: r.write(finally_result);
1.234 moko 832: } else {
833: // no exception in try/catch, writing processed body_code or catch_code
1.243 moko 834: r.write(result.processed_code);
1.234 moko 835: }
1.74 paf 836: }
837:
1.138 paf 838: static void _throw_operator(Request&, MethodParams& params) {
1.178 misha 839: if(params.count()==1 && !params[0].is_string()) {
1.129 paf 840: if(HashStringValue *hash=params[0].get_hash()) {
841: const char* type=0;
1.281 moko 842: if(Value* value=hash->get(Symbols::TYPE_SYMBOL))
1.78 paf 843: type=value->as_string().cstr();
1.129 paf 844: const String* source=0;
1.281 moko 845: if(Value* value=hash->get(Symbols::SOURCE_SYMBOL))
1.74 paf 846: source=&value->as_string();
1.138 paf 847: const char* comment=0;
1.281 moko 848: if(Value* value=hash->get(Symbols::COMMENT_SYMBOL))
1.74 paf 849: comment=value->as_string().cstr();
850:
1.261 moko 851: throw Exception(type, source, "%s", comment); // to avoid MAX_STRING limit
1.74 paf 852: } else
1.260 moko 853: throw Exception(PARSER_RUNTIME, 0, "one-param version has hash or string param");
1.74 paf 854: } else {
1.129 paf 855: const char* type=params.as_string(0, "type must be string").cstr();
1.261 moko 856: const String* source=params.count()>1 ? ¶ms.as_string(1, "source must be string") : 0;
857: const char* comment=params.count()>2 ? params.as_string(2, "comment must be string").cstr() : 0;
858: throw Exception(type, source, "%s", comment); // to avoid MAX_STRING limit
1.74 paf 859: }
1.261 moko 860: }
1.129 paf 861:
1.150 paf 862: static void _sleep_operator(Request& r, MethodParams& params) {
863: double seconds=params.as_double(0, "seconds must be double", r);
1.206 moko 864: if(seconds>0)
865: pa_sleep((int)trunc(seconds), (int)trunc((seconds-trunc(seconds))*1000000));
1.261 moko 866: }
1.150 paf 867:
1.279 moko 868: static void _a_operator(Request& r, MethodParams& params) {
869: VArray& result=*new VArray;
870: ArrayValue& array=result.array();
871:
872: int count=params.count();
873: for(int i=0; i<count; i++)
874: array+=&r.process(params[i]);
875: array.confirm_all_used();
876:
877: r.write(result);
878: }
879:
1.274 moko 880: #ifdef HAVE_SYSLOG
1.273 moko 881: static int log_level(const String &name){
882: if(name.is_empty()) return LOG_INFO;
883: const char *sname = str_upper(name.cstr());
884:
885: if(!strcmp(sname,"INFO")) return LOG_INFO;
886: if(!strcmp(sname,"WARNING")) return LOG_WARNING;
887: if(!strcmp(sname,"ERROR")) return LOG_ERR;
888: if(!strcmp(sname,"DEBUG")) return LOG_DEBUG;
889: throw Exception("syslog", &name, "invalid log level value");
890: }
1.274 moko 891: #endif
1.273 moko 892:
1.277 moko 893: static void _syslog_operator(Request& r, MethodParams& params) {
1.273 moko 894: const char* message=params.as_string(1, "message must be string").cstr();
1.274 moko 895: #ifdef HAVE_SYSLOG
1.278 moko 896: const char* ident=params.as_string(0, "ident must be string").cstr();
1.273 moko 897: int level=params.count()>2 ? log_level(params.as_string(2, "level must be string")) : LOG_INFO;
898:
899: openlog(*ident ? ident : "parser3", LOG_PID, LOG_USER);
900: syslog(level, "%s", message);
901: closelog();
1.277 moko 902: (void)r;
1.273 moko 903: #else
904: SAPI::log(r.sapi_info, "syslog: %s", message);
905: #endif
906: }
907:
1.254 moko 908: #if defined(WIN32) && defined(_DEBUG) && !defined(_WIN64)
1.129 paf 909: # define PA_BPT
1.138 paf 910: static void _bpt(Request&, MethodParams&) {
1.129 paf 911: _asm int 3;
912: }
913: #endif
914:
1.10 paf 915: // constructor
916:
1.226 moko 917: VClassMAIN::VClassMAIN(): VClass(MAIN_CLASS_NAME) {
1.129 paf 918:
919: #ifdef PA_BPT
920: // ^bpt[]
921: add_native_method("bpt", Method::CT_ANY, _bpt, 0, 0);
922: #endif
1.121 paf 923:
1.1 paf 924: // ^if(condition){code-when-true}
925: // ^if(condition){code-when-true}{code-when-false}
1.200 moko 926: // ^if(condition){code-when-true} (another condition){code-when-true} ... {code-when-false}
927: add_native_method("if", Method::CT_ANY, _if, 2, 10000, Method::CO_WITHOUT_FRAME);
1.1 paf 928:
1.194 misha 929: // ^untaint[as-is|uri|sql|js|html|html-typo|regex|parser-code]{code}
930: add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2, Method::CO_WITHOUT_FRAME);
1.1 paf 931:
1.194 misha 932: // ^taint[as-is|uri|sql|js|html|html-typo|regex|parser-code]{code}
933: add_native_method("taint", Method::CT_ANY, _taint, 1, 2, Method::CO_WITHOUT_FRAME);
1.1 paf 934:
1.205 moko 935: // ^apply-taint[untaint lang][string]
936: add_native_method("apply-taint", Method::CT_ANY, _apply_taint, 1, 2, Method::CO_WITHOUT_FRAME);
937:
1.216 moko 938: // ^process{code}
939: // ^process[context]{code}[options hash]
1.146 paf 940: add_native_method("process", Method::CT_ANY, _process, 1, 3);
1.1 paf 941:
942: // ^rem{code}
1.185 misha 943: add_native_method("rem", Method::CT_ANY, _rem, 1, 10000, Method::CO_WITHOUT_FRAME);
1.1 paf 944:
945: // ^while(condition){code}
1.185 misha 946: add_native_method("while", Method::CT_ANY, _while, 2, 3, Method::CO_WITHOUT_FRAME);
1.1 paf 947:
1.216 moko 948: // ^use[file[;options hash]]
949: add_native_method("use", Method::CT_ANY, _use, 1, 2);
1.1 paf 950:
1.164 paf 951: // ^break[]
1.227 moko 952: // ^break(condition)
953: add_native_method("break", Method::CT_ANY, _break, 0, 1, Method::CO_WITHOUT_FRAME);
1.185 misha 954:
1.164 paf 955: // ^continue[]
1.227 moko 956: // ^continue(condition)
957: add_native_method("continue", Method::CT_ANY, _continue, 0, 1, Method::CO_WITHOUT_FRAME);
1.185 misha 958:
1.249 moko 959: // ^return[]
960: // ^return[result]
1.250 moko 961: add_native_method("return", Method::CT_ANY, _return, 0, 1);
1.249 moko 962:
1.54 paf 963: // ^for[i](from-number;to-number-inclusive){code}[delim]
1.239 moko 964: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 965:
966: // ^eval(expr)
967: // ^eval(expr)[format]
1.185 misha 968: add_native_method("eval", Method::CT_ANY, _eval, 1, 2, Method::CO_WITHOUT_FRAME);
1.52 parser 969:
1.1 paf 970: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 971: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
972:
1.63 paf 973:
1.136 paf 974: // ^cache[file_spec](time){code}[{catch code}] time=0 no cache
1.65 paf 975: // ^cache[file_spec] delete cache
1.161 paf 976: // ^cache[] get current expiration time
977: add_native_method("cache", Method::CT_ANY, _cache, 0, 4);
1.63 paf 978:
1.28 parser 979: // switch
980:
981: // ^switch[value]{cases}
1.185 misha 982: add_native_method("switch", Method::CT_ANY, _switch, 2, 2, Method::CO_WITHOUT_FRAME);
1.28 parser 983:
984: // ^case[value]{code}
1.185 misha 985: add_native_method("case", Method::CT_ANY, _case, 2, 10000, Method::CO_WITHOUT_FRAME);
1.74 paf 986:
987: // try-catch
988:
989: // ^try{code}{catch code}
1.185 misha 990: add_native_method("try", Method::CT_ANY, _try_operator, 2, 3, Method::CO_WITHOUT_FRAME);
1.74 paf 991: // ^throw[$exception hash]
992: // ^throw[type;source;comment]
993: add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
994:
1.150 paf 995: add_native_method("sleep", Method::CT_ANY, _sleep_operator, 1, 1);
1.273 moko 996:
1.279 moko 997: // equivalent to ^array::create[...]
998: add_native_method("A", Method::CT_ANY, _a_operator, 0, 10000, Method::CO_WITHOUT_FRAME);
999:
1.273 moko 1000: // ^syslog[ident;message[;info|warning|error|debug]]
1001: add_native_method("syslog", Method::CT_ANY, _syslog_operator, 2, 3);
1.10 paf 1002: }
1.11 paf 1003:
1004: // constructor & configurator
1.1 paf 1005:
1.129 paf 1006: VStateless_class& VClassMAIN_create() {
1007: return *new VClassMAIN;
1.1 paf 1008: }
E-mail: