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