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