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