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