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