Annotation of parser3/src/classes/op.C, revision 1.283
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.283 ! moko 26: volatile const char * IDENT_OP_C="$Id: op.C,v 1.282 2026/09/25 12:55:42 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);
551: } catch(const Exception& e) {
1.282 moko 552: size_t trace_bottom=r.exception_trace.bottom_index();
553: size_t trace_top=r.exception_trace.top_index();
1.230 moko 554:
1.281 moko 555: VException& details=*new VException(r, e);
1.230 moko 556:
1.282 moko 557: try_context.restore(); // restoring try-context for catch code
1.136 paf 558:
1.282 moko 559: r.exception_trace.set_bottom_index(trace_top); // keep the original trace occupied; errors in catch start after it
1.136 paf 560:
561: bool bhandled=false;
1.282 moko 562: try {
563: {
564: Temp_value_element temp(r, *catch_code->get_junction()->method_frame, exception_var_name, &details);
565: Temp_skip temp_skip(r);
566: result.processed_code=r.process(*catch_code);
567: }
568:
569: Value* vhandled=details.handled();
570: if(vhandled) {
571: if(vhandled->is_string()) { // not simple $exception.handled(1/0)?
572: if(bhandled=could_be_handled_by_caller) { // and we can possibly handle it
573: result.exception_should_be_handled=vhandled->get_string(); // considering 'recovered' and let the caller recover
574: }
575: } else
576: bhandled=vhandled->as_bool();
577: }
578: } catch(...) {
579: // restore execution for finally, but keep new error trace.
580: try_context.restore();
1.283 ! moko 581: details.expire_trace();
1.282 moko 582: rethrow;
1.136 paf 583: }
584:
1.283 ! moko 585: details.expire_trace();
1.230 moko 586: if(!bhandled){
1.282 moko 587: r.exception_trace.set_range(trace_bottom, trace_top);
1.136 paf 588: rethrow;
1.230 moko 589: }
1.282 moko 590: r.exception_trace.set_range(trace_start, trace_start);
1.136 paf 591: }
1.179 misha 592:
1.136 paf 593: return result;
594: }
1.28 parser 595:
1.234 moko 596: static Value& process_try_body_code(Request& r, Value* body_code) {
1.222 moko 597: return r.process(*body_code);
598: }
599:
1.63 paf 600: // cache--
601:
602: // consts
603:
1.152 paf 604: const int DATA_STRING_SERIALIZED_VERSION=0x0006;
1.63 paf 605:
606: // helper types
607:
608: #ifndef DOXYGEN
609: struct Data_string_serialized_prolog {
610: int version;
1.79 paf 611: time_t expires;
1.63 paf 612: };
613: #endif
614:
1.68 paf 615: void cache_delete(const String& file_spec) {
1.167 misha 616: file_delete(file_spec, false/*fail_on_problem*/);
1.63 paf 617: }
1.69 paf 618:
619: #ifndef DOXYGEN
1.135 paf 620: struct Cache_scope {
1.129 paf 621: public:
1.79 paf 622: time_t expires;
1.135 paf 623: const String* body_from_disk;
1.79 paf 624: };
1.69 paf 625: struct Locked_process_and_cache_put_action_info {
626: Request *r;
1.136 paf 627: Cache_scope *scope;
1.167 misha 628: Value* body_code;
629: Value* catch_code;
1.136 paf 630: const String* processed_code;
1.69 paf 631: };
632: #endif
1.136 paf 633:
634:
635:
1.234 moko 636: static Value& process_cache_body_code(Request& r, Value* body_code) {
637: return r.process(*body_code);
1.136 paf 638: }
639:
1.129 paf 640: /* @todo maybe network order worth spending some effort?
641: don't bothering myself with network byte order,
642: am not planning to be able to move resulting file across platforms
643: */
1.69 paf 644: static void locked_process_and_cache_put_action(int f, void *context) {
645: Locked_process_and_cache_put_action_info& info=
646: *static_cast<Locked_process_and_cache_put_action_info *>(context);
1.129 paf 647:
1.160 paf 648: const String* body_from_disk=info.scope->body_from_disk;
1.69 paf 649: // body->process
1.136 paf 650: Try_catch_result result=try_catch(*info.r,
651: process_cache_body_code, info.body_code,
1.160 paf 652: info.catch_code, body_from_disk!=0 /*we have something old=we can handle=recover later*/);
1.136 paf 653:
654: if(result.exception_should_be_handled) {
655: if(*result.exception_should_be_handled==CACHE_EXCEPTION_HANDLED_CACHE_NAME) {
1.160 paf 656: assert(body_from_disk);
657: info.processed_code=body_from_disk;
1.136 paf 658: } else
1.166 misha 659: throw Exception(PARSER_RUNTIME,
1.136 paf 660: result.exception_should_be_handled,
1.281 moko 661: "$" EXCEPTION_VAR_NAME ".handled value must be "
1.219 moko 662: "either boolean or string '" CACHE_EXCEPTION_HANDLED_CACHE_NAME "'");
1.136 paf 663: } else
1.234 moko 664: info.processed_code=&((Value &)result.processed_code).as_string();
1.69 paf 665:
1.79 paf 666: // expiration time not spoiled by ^cache(0) or something?
1.136 paf 667: if(info.scope->expires > time(0)) {
1.79 paf 668: // string -serialize> buffer
1.136 paf 669: String::Cm serialized=info.processed_code->serialize(
1.129 paf 670: sizeof(Data_string_serialized_prolog));
1.79 paf 671: Data_string_serialized_prolog& prolog=
1.129 paf 672: *reinterpret_cast<Data_string_serialized_prolog *>(serialized.str);
1.79 paf 673: prolog.version=DATA_STRING_SERIALIZED_VERSION;
1.136 paf 674: prolog.expires=info.scope->expires;
1.79 paf 675:
676: // buffer -write> file
1.272 moko 677: if(write(f, serialized.str, serialized.length)<0)
678: throw Exception("file.write", 0, "write failed: %s (%d)", strerror(errno), errno);
1.79 paf 679: } else // expired!
1.136 paf 680: info.scope->expires=0; // flag it so that could be easily checked by caller
1.69 paf 681: }
1.129 paf 682: const String* locked_process_and_cache_put(Request& r,
1.128 paf 683: Value& body_code,
1.136 paf 684: Value* catch_code,
685: Cache_scope& scope,
686: const String& file_spec)
687: {
1.140 paf 688: Locked_process_and_cache_put_action_info info={&r, &scope, &body_code, catch_code, 0};
1.69 paf 689:
1.129 paf 690: const String* result=file_write_action_under_lock(
1.69 paf 691: file_spec,
1.168 misha 692: "cache_put",
693: locked_process_and_cache_put_action,
694: &info,
1.69 paf 695: false/*as_text*/,
696: false/*do_append*/,
1.173 misha 697: false/*block == don't wait till other thread release lock*/,
698: false/*dun throw exception if lock failed*/) ? info.processed_code: 0;
1.168 misha 699:
1.100 paf 700: time_t now=time(0);
1.136 paf 701: if(scope.expires<=now)
1.79 paf 702: cache_delete(file_spec);
703: return result;
1.63 paf 704: }
1.229 moko 705:
1.137 paf 706: #ifndef DOXYGEN
1.135 paf 707: struct Cache_get_result {
708: const String* body;
709: bool expired;
1.137 paf 710: };
711: #endif
1.229 moko 712:
1.271 moko 713: static Cache_get_result cache_get(const String& file_spec, time_t now) {
1.140 paf 714: Cache_get_result result={0, false};
1.135 paf 715:
1.253 moko 716: File_read_result file=file_read_binary(file_spec, false /*fail_on_read_problem*/);
1.252 moko 717: if(file.success && file.length /*ignore reads which are empty due to non-unary open+lockEX conflict with lockSH*/) {
718:
719: Data_string_serialized_prolog& prolog = *reinterpret_cast<Data_string_serialized_prolog *>(file.str);
720:
1.135 paf 721: String* body=new String;
1.252 moko 722: if(file.length>=sizeof(Data_string_serialized_prolog) && prolog.version==DATA_STRING_SERIALIZED_VERSION) {
1.135 paf 723: if(body->deserialize(sizeof(Data_string_serialized_prolog), file.str, file.length)) {
724: result.body=body;
725: result.expired=prolog.expires <= now;
726: }
727: }
1.72 paf 728: }
1.63 paf 729:
1.135 paf 730: return result;
1.63 paf 731: }
1.252 moko 732:
733: static time_t as_expires(Request& r, MethodParams& params, int index, time_t now) {
1.79 paf 734: time_t result;
1.263 moko 735: if(VDate* vdate=dynamic_cast<VDate*>(¶ms[index]))
1.274 moko 736: result=(time_t)vdate->get_time();
1.79 paf 737: else
1.129 paf 738: result=now+(time_t)params.as_double(index, "lifespan must be date or number", r);
1.79 paf 739:
740: return result;
741: }
1.229 moko 742:
1.129 paf 743: static const String& as_file_spec(Request& r, MethodParams& params, int index) {
1.258 moko 744: return r.full_disk_path(params.as_string(index, "filespec must be string"));
1.79 paf 745: }
1.229 moko 746:
1.129 paf 747: static void _cache(Request& r, MethodParams& params) {
1.173 misha 748: if(params.count()==0) {
749: // ^cache[] -- return current expiration time
1.158 paf 750: Cache_scope* scope=static_cast<Cache_scope*>(r.classes_conf.get(cache_data_name));
751: if(!scope)
1.221 moko 752: throw Exception(PARSER_RUNTIME, 0, "expire-time get without cache");
1.243 moko 753: r.write(*new VDate((pa_time_t)scope->expires));
1.158 paf 754: return;
755: }
756:
1.79 paf 757: time_t now=time(0);
758:
1.129 paf 759: if(params.count()==1) {
1.173 misha 760: // ^cache[filename] ^cache(seconds) ^cache[expires date]
1.129 paf 761: if(params[0].is_string()) { // filename?
1.79 paf 762: cache_delete(as_file_spec(r, params, 0));
763: return;
764: }
765:
766: // secods|expires date
1.135 paf 767: Cache_scope* scope=static_cast<Cache_scope*>(r.classes_conf.get(cache_data_name));
768: if(!scope)
1.166 misha 769: throw Exception(PARSER_RUNTIME,
1.129 paf 770: 0,
1.79 paf 771: "expire-time reducing instruction without cache");
772:
1.129 paf 773: time_t expires=as_expires(r, params, 0, now);
1.135 paf 774: if(expires < scope->expires)
775: scope->expires=expires;
1.79 paf 776:
777: return;
1.149 paf 778: } else if(params.count()<3)
1.166 misha 779: throw Exception(PARSER_RUNTIME,
1.149 paf 780: 0,
781: "invalid number of parameters");
1.63 paf 782:
783: // file_spec, expires, body code
1.172 misha 784: const String& file_spec=as_file_spec(r, params, 0);
1.65 paf 785:
1.141 paf 786: Cache_scope scope={as_expires(r, params, 1, now), 0};
1.135 paf 787:
1.215 moko 788: Temp_hash_value<HashString<void*>, void*> cache_scope_setter(&r.classes_conf, cache_data_name, &scope);
1.136 paf 789: Value& body_code=params.as_junction(2, "body_code must be code");
790: Value* catch_code=0;
791: if(params.count()>3)
792: catch_code=¶ms.as_junction(3, "catch_code must be code");
1.63 paf 793:
1.168 misha 794: if(scope.expires>now) {
1.271 moko 795: Cache_get_result cached=cache_get(file_spec, now);
1.69 paf 796:
1.168 misha 797: if(cached.body) { // have cached copy
1.174 misha 798: if(cached.expired) {
799: scope.body_from_disk=cached.body; // storing for user to retrive it with ^cache[]
800: } else {
801: // and it's not expired yet write it out
1.243 moko 802: r.write(*cached.body);
1.168 misha 803: // happy with it
804: return;
805: }
806: }
807:
808: // no cached info or it's already expired
1.173 misha 809:
810: // trying to process it under lock and store result in file
1.174 misha 811: const String* processed_body=locked_process_and_cache_put(r, body_code, catch_code, scope, file_spec);
1.173 misha 812: if(processed_body){
1.174 misha 813: // write it out
1.243 moko 814: r.write(*processed_body);
1.174 misha 815: // happy with it
816: return;
1.173 misha 817: } else {
818: // we fail while get exclusive lock. nvm, we just execute body_code a bit later
1.174 misha 819: }
1.69 paf 820: } else {
1.79 paf 821: // instructed not to cache; forget cached copy
1.68 paf 822: cache_delete(file_spec);
1.69 paf 823: }
1.168 misha 824:
1.222 moko 825: // process without caching
826: if(catch_code){
827: Try_catch_result result=try_catch(r, process_try_body_code, &body_code, catch_code);
1.243 moko 828: r.write(result.processed_code);
1.222 moko 829: } else {
1.243 moko 830: r.write(r.process_to_string(body_code));
1.222 moko 831: }
1.63 paf 832: }
833:
1.129 paf 834: static void _try_operator(Request& r, MethodParams& params) {
835: Value& body_code=params.as_junction(0, "body_code must be code");
836: Value& catch_code=params.as_junction(1, "catch_code must be code");
1.179 misha 837: Value* finally_code=(params.count()==3) ? ¶ms.as_junction(2, "finally_code must be code") : 0;
838:
839: Try_catch_result result;
1.229 moko 840:
1.179 misha 841: try{
1.229 moko 842: // process try and catch code
1.222 moko 843: result=try_catch(r, process_try_body_code, &body_code, &catch_code);
1.179 misha 844: } catch(...){
1.229 moko 845: // process finally code but ignore the result
846: if(finally_code){
1.282 moko 847: size_t trace_bottom=r.exception_trace.bottom_index();
848: size_t trace_top=r.exception_trace.top_index();
849: r.exception_trace.set_bottom_index(trace_top);
1.229 moko 850: Temp_skip temp(r);
1.248 moko 851: /* Value &finally_result= */ r.process(*finally_code);
1.282 moko 852: r.exception_trace.set_range(trace_bottom, trace_top);
1.229 moko 853: }
1.179 misha 854: rethrow;
855: }
1.74 paf 856:
1.229 moko 857: // process finally code
858: if(finally_code){
859: Temp_skip temp(r);
1.234 moko 860: Value& finally_result=r.process(*finally_code);
1.229 moko 861:
1.234 moko 862: // no exception in try/catch or finally, writing processed body_code or catch_code
1.243 moko 863: r.write(result.processed_code);
1.123 paf 864:
1.234 moko 865: // write out processed finally code
1.243 moko 866: r.write(finally_result);
1.234 moko 867: } else {
868: // no exception in try/catch, writing processed body_code or catch_code
1.243 moko 869: r.write(result.processed_code);
1.234 moko 870: }
1.74 paf 871: }
872:
1.138 paf 873: static void _throw_operator(Request&, MethodParams& params) {
1.178 misha 874: if(params.count()==1 && !params[0].is_string()) {
1.129 paf 875: if(HashStringValue *hash=params[0].get_hash()) {
876: const char* type=0;
1.281 moko 877: if(Value* value=hash->get(Symbols::TYPE_SYMBOL))
1.78 paf 878: type=value->as_string().cstr();
1.129 paf 879: const String* source=0;
1.281 moko 880: if(Value* value=hash->get(Symbols::SOURCE_SYMBOL))
1.74 paf 881: source=&value->as_string();
1.138 paf 882: const char* comment=0;
1.281 moko 883: if(Value* value=hash->get(Symbols::COMMENT_SYMBOL))
1.74 paf 884: comment=value->as_string().cstr();
885:
1.261 moko 886: throw Exception(type, source, "%s", comment); // to avoid MAX_STRING limit
1.74 paf 887: } else
1.260 moko 888: throw Exception(PARSER_RUNTIME, 0, "one-param version has hash or string param");
1.74 paf 889: } else {
1.129 paf 890: const char* type=params.as_string(0, "type must be string").cstr();
1.261 moko 891: const String* source=params.count()>1 ? ¶ms.as_string(1, "source must be string") : 0;
892: const char* comment=params.count()>2 ? params.as_string(2, "comment must be string").cstr() : 0;
893: throw Exception(type, source, "%s", comment); // to avoid MAX_STRING limit
1.74 paf 894: }
1.261 moko 895: }
1.129 paf 896:
1.150 paf 897: static void _sleep_operator(Request& r, MethodParams& params) {
898: double seconds=params.as_double(0, "seconds must be double", r);
1.206 moko 899: if(seconds>0)
900: pa_sleep((int)trunc(seconds), (int)trunc((seconds-trunc(seconds))*1000000));
1.261 moko 901: }
1.150 paf 902:
1.279 moko 903: static void _a_operator(Request& r, MethodParams& params) {
904: VArray& result=*new VArray;
905: ArrayValue& array=result.array();
906:
907: int count=params.count();
908: for(int i=0; i<count; i++)
909: array+=&r.process(params[i]);
910: array.confirm_all_used();
911:
912: r.write(result);
913: }
914:
1.274 moko 915: #ifdef HAVE_SYSLOG
1.273 moko 916: static int log_level(const String &name){
917: if(name.is_empty()) return LOG_INFO;
918: const char *sname = str_upper(name.cstr());
919:
920: if(!strcmp(sname,"INFO")) return LOG_INFO;
921: if(!strcmp(sname,"WARNING")) return LOG_WARNING;
922: if(!strcmp(sname,"ERROR")) return LOG_ERR;
923: if(!strcmp(sname,"DEBUG")) return LOG_DEBUG;
924: throw Exception("syslog", &name, "invalid log level value");
925: }
1.274 moko 926: #endif
1.273 moko 927:
1.277 moko 928: static void _syslog_operator(Request& r, MethodParams& params) {
1.273 moko 929: const char* message=params.as_string(1, "message must be string").cstr();
1.274 moko 930: #ifdef HAVE_SYSLOG
1.278 moko 931: const char* ident=params.as_string(0, "ident must be string").cstr();
1.273 moko 932: int level=params.count()>2 ? log_level(params.as_string(2, "level must be string")) : LOG_INFO;
933:
934: openlog(*ident ? ident : "parser3", LOG_PID, LOG_USER);
935: syslog(level, "%s", message);
936: closelog();
1.277 moko 937: (void)r;
1.273 moko 938: #else
939: SAPI::log(r.sapi_info, "syslog: %s", message);
940: #endif
941: }
942:
1.254 moko 943: #if defined(WIN32) && defined(_DEBUG) && !defined(_WIN64)
1.129 paf 944: # define PA_BPT
1.138 paf 945: static void _bpt(Request&, MethodParams&) {
1.129 paf 946: _asm int 3;
947: }
948: #endif
949:
1.10 paf 950: // constructor
951:
1.226 moko 952: VClassMAIN::VClassMAIN(): VClass(MAIN_CLASS_NAME) {
1.129 paf 953:
954: #ifdef PA_BPT
955: // ^bpt[]
956: add_native_method("bpt", Method::CT_ANY, _bpt, 0, 0);
957: #endif
1.121 paf 958:
1.1 paf 959: // ^if(condition){code-when-true}
960: // ^if(condition){code-when-true}{code-when-false}
1.200 moko 961: // ^if(condition){code-when-true} (another condition){code-when-true} ... {code-when-false}
962: add_native_method("if", Method::CT_ANY, _if, 2, 10000, Method::CO_WITHOUT_FRAME);
1.1 paf 963:
1.194 misha 964: // ^untaint[as-is|uri|sql|js|html|html-typo|regex|parser-code]{code}
965: add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2, Method::CO_WITHOUT_FRAME);
1.1 paf 966:
1.194 misha 967: // ^taint[as-is|uri|sql|js|html|html-typo|regex|parser-code]{code}
968: add_native_method("taint", Method::CT_ANY, _taint, 1, 2, Method::CO_WITHOUT_FRAME);
1.1 paf 969:
1.205 moko 970: // ^apply-taint[untaint lang][string]
971: add_native_method("apply-taint", Method::CT_ANY, _apply_taint, 1, 2, Method::CO_WITHOUT_FRAME);
972:
1.216 moko 973: // ^process{code}
974: // ^process[context]{code}[options hash]
1.146 paf 975: add_native_method("process", Method::CT_ANY, _process, 1, 3);
1.1 paf 976:
977: // ^rem{code}
1.185 misha 978: add_native_method("rem", Method::CT_ANY, _rem, 1, 10000, Method::CO_WITHOUT_FRAME);
1.1 paf 979:
980: // ^while(condition){code}
1.185 misha 981: add_native_method("while", Method::CT_ANY, _while, 2, 3, Method::CO_WITHOUT_FRAME);
1.1 paf 982:
1.216 moko 983: // ^use[file[;options hash]]
984: add_native_method("use", Method::CT_ANY, _use, 1, 2);
1.1 paf 985:
1.164 paf 986: // ^break[]
1.227 moko 987: // ^break(condition)
988: add_native_method("break", Method::CT_ANY, _break, 0, 1, Method::CO_WITHOUT_FRAME);
1.185 misha 989:
1.164 paf 990: // ^continue[]
1.227 moko 991: // ^continue(condition)
992: add_native_method("continue", Method::CT_ANY, _continue, 0, 1, Method::CO_WITHOUT_FRAME);
1.185 misha 993:
1.249 moko 994: // ^return[]
995: // ^return[result]
1.250 moko 996: add_native_method("return", Method::CT_ANY, _return, 0, 1);
1.249 moko 997:
1.54 paf 998: // ^for[i](from-number;to-number-inclusive){code}[delim]
1.239 moko 999: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 1000:
1001: // ^eval(expr)
1002: // ^eval(expr)[format]
1.185 misha 1003: add_native_method("eval", Method::CT_ANY, _eval, 1, 2, Method::CO_WITHOUT_FRAME);
1.52 parser 1004:
1.1 paf 1005: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 1006: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
1007:
1.63 paf 1008:
1.136 paf 1009: // ^cache[file_spec](time){code}[{catch code}] time=0 no cache
1.65 paf 1010: // ^cache[file_spec] delete cache
1.161 paf 1011: // ^cache[] get current expiration time
1012: add_native_method("cache", Method::CT_ANY, _cache, 0, 4);
1.63 paf 1013:
1.28 parser 1014: // switch
1015:
1016: // ^switch[value]{cases}
1.185 misha 1017: add_native_method("switch", Method::CT_ANY, _switch, 2, 2, Method::CO_WITHOUT_FRAME);
1.28 parser 1018:
1019: // ^case[value]{code}
1.185 misha 1020: add_native_method("case", Method::CT_ANY, _case, 2, 10000, Method::CO_WITHOUT_FRAME);
1.74 paf 1021:
1022: // try-catch
1023:
1024: // ^try{code}{catch code}
1.185 misha 1025: add_native_method("try", Method::CT_ANY, _try_operator, 2, 3, Method::CO_WITHOUT_FRAME);
1.74 paf 1026: // ^throw[$exception hash]
1027: // ^throw[type;source;comment]
1028: add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
1029:
1.150 paf 1030: add_native_method("sleep", Method::CT_ANY, _sleep_operator, 1, 1);
1.273 moko 1031:
1.279 moko 1032: // equivalent to ^array::create[...]
1033: add_native_method("A", Method::CT_ANY, _a_operator, 0, 10000, Method::CO_WITHOUT_FRAME);
1034:
1.273 moko 1035: // ^syslog[ident;message[;info|warning|error|debug]]
1036: add_native_method("syslog", Method::CT_ANY, _syslog_operator, 2, 3);
1.10 paf 1037: }
1.11 paf 1038:
1039: // constructor & configurator
1.1 paf 1040:
1.129 paf 1041: VStateless_class& VClassMAIN_create() {
1042: return *new VClassMAIN;
1.1 paf 1043: }
E-mail: