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