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