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