Annotation of parser3/src/classes/op.C, revision 1.162
1.1 paf 1: /** @file
1.9 paf 2: Parser: parser @b operators.
1.1 paf 3:
1.155 paf 4: Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.71 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.98 paf 6: */
1.1 paf 7:
1.162 ! paf 8: static const char * const IDENT_OP_C="$Date: 2005/11/22 15:40:17 $";
1.1 paf 9:
1.13 paf 10: #include "classes.h"
1.129 paf 11: #include "pa_vmethod_frame.h"
12:
1.1 paf 13: #include "pa_common.h"
1.134 paf 14: #include "pa_os.h"
1.1 paf 15: #include "pa_request.h"
16: #include "pa_vint.h"
17: #include "pa_sql_connection.h"
1.79 paf 18: #include "pa_vdate.h"
1.105 paf 19: #include "pa_vmethod_frame.h"
1.129 paf 20: #include "pa_vclass.h"
1.144 paf 21: #include "pa_charset.h"
1.1 paf 22:
1.18 parser 23: // limits
24:
1.162 ! paf 25: #define MAX_LOOPS 20000
1.18 parser 26:
1.10 paf 27: // defines
28:
1.82 paf 29: #define CASE_DEFAULT_VALUE "DEFAULT"
1.146 paf 30: #define PROCESS_MAIN_OPTION_NAME "main"
31: #define PROCESS_FILE_OPTION_NAME "file"
32: #define PROCESS_LINENO_OPTION_NAME "lineno"
1.11 paf 33:
1.10 paf 34: // class
35:
1.113 paf 36: class VClassMAIN: public VClass {
1.10 paf 37: public:
1.129 paf 38: VClassMAIN();
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"
45: #define EXCEPTION_VAR_NAME "exception"
46:
47: // statics
48:
49: //^switch ^case
50: static const String switch_data_name(SWITCH_DATA_NAME);
51: //^cache
52: static const String cache_data_name(CACHE_DATA_NAME);
53:
54: static const String exception_var_name(EXCEPTION_VAR_NAME);
55:
1.136 paf 56: // local defines
57:
58: #define CACHE_EXCEPTION_HANDLED_CACHE_NAME "cache"
59:
1.129 paf 60: // helpers
61:
1.130 paf 62: class Untaint_lang_name2enum: public Hash<const String::Body, String::Language> {
1.129 paf 63: public:
64: Untaint_lang_name2enum() {
65: #define ULN(name, LANG) \
1.130 paf 66: put(String::Body(name), (value_type)(String::L_##LANG));
1.129 paf 67: ULN("as-is", AS_IS);
68: ULN("optimized-as-is", AS_IS|String::L_OPTIMIZE_BIT);
69: ULN("file-spec", FILE_SPEC);
70: ULN("http-header", HTTP_HEADER);
71: ULN("mail-header", MAIL_HEADER);
72: ULN("uri", URI);
73: ULN("sql", SQL);
74: ULN("js", JS);
75: ULN("xml", XML);
76: ULN("optimized-xml", XML|String::L_OPTIMIZE_BIT);
77: ULN("html", HTML);
78: ULN("optimized-html", HTML|String::L_OPTIMIZE_BIT);
1.159 paf 79: ULN("regex", REGEX);
1.129 paf 80: #undef ULN
81: }
82: } untaint_lang_name2enum;
83:
1.10 paf 84: // methods
85:
1.129 paf 86: static void _if(Request& r, MethodParams& params) {
1.156 paf 87: bool condition=params.as_bool(0, "condition must be expression", r);
1.6 paf 88: if(condition)
1.156 paf 89: r.write_pass_lang(r.process(*params.get(1)));
1.129 paf 90: else if(params.count()>2)
1.156 paf 91: r.write_pass_lang(r.process(*params.get(2)));
1.1 paf 92: }
93:
1.129 paf 94: static void _untaint(Request& r, MethodParams& params) {
1.1 paf 95:
1.129 paf 96: String::Language lang;
97: if(params.count()==1)
98: lang=String::L_AS_IS; // mark as simply 'tainted'. useful in html from sql
1.59 paf 99: else {
1.129 paf 100: const String& lang_name=params.as_string(0, "lang must be string");
101: lang=untaint_lang_name2enum.get(lang_name);
1.59 paf 102: if(!lang)
1.78 paf 103: throw Exception(0,
1.59 paf 104: &lang_name,
105: "invalid taint language");
106: }
1.1 paf 107:
108: {
1.129 paf 109: Value& vbody=params.as_junction(params.count()-1, "body must be code");
1.1 paf 110:
111: Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
1.86 paf 112: r.write_pass_lang(r.process(vbody)); // process marking tainted with that lang
1.1 paf 113: }
114: }
115:
1.129 paf 116: static void _taint(Request& r, MethodParams& params) {
117: String::Language lang;
118: if(params.count()==1)
119: lang=String::L_TAINTED; // mark as simply 'tainted'. useful in table:set
1.1 paf 120: else {
1.129 paf 121: const String& lang_name=params.as_string(0, "lang must be string");
122: lang=untaint_lang_name2enum.get(lang_name);
1.1 paf 123: if(!lang)
1.78 paf 124: throw Exception(0,
1.3 paf 125: &lang_name,
126: "invalid taint language");
1.1 paf 127: }
128:
129: {
1.129 paf 130: Value& vbody=params.as_no_junction(params.count()-1, "body must not be code");
1.1 paf 131:
1.129 paf 132: String result;
1.1 paf 133: result.append(
134: vbody.as_string(), // process marking tainted with that lang
135: lang, true); // force result language to specified
136: r.write_pass_lang(result);
137: }
138: }
139:
1.129 paf 140: static void _process(Request& r, MethodParams& params) {
141: Method* main_method;
142:
143: size_t index=0;
144: Value* target_self;
145: Value& maybe_target_self=params[index];
146: if(maybe_target_self.get_string() || maybe_target_self.get_junction())
147: target_self=&r.get_method_frame()->caller()->self();
148: else {
149: target_self=&maybe_target_self; index++;
150: }
151:
1.1 paf 152: {
1.129 paf 153: VStateless_class *target_class=target_self->get_last_derived_class();
1.116 paf 154: if(!target_class)
155: throw Exception("parser.runtime",
1.129 paf 156: 0,
1.116 paf 157: "no target class");
1.114 paf 158:
1.73 paf 159: // temporary remove language change
1.129 paf 160: Temp_lang temp_lang(r, String::L_PASS_APPENDED);
1.1 paf 161: // temporary zero @main so to maybe-replace it in processed code
1.129 paf 162: Temp_method temp_method_main(*target_class, main_method_name, 0);
1.1 paf 163: // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
1.129 paf 164: Temp_method temp_method_auto(*target_class, auto_method_name, 0);
165:
1.146 paf 166: size_t options_index=index+1;
167: HashStringValue* options=0;
168: if(options_index<params.count()) {
169: Value& voptions=params.as_no_junction(options_index, "options must not be code");
170: options=voptions.get_hash();
171: if(!options)
172: throw Exception("parser.runtime",
173: 0,
174: "options must be hash");
175: }
176:
1.129 paf 177: const String* main_alias=0;
1.146 paf 178: const String* file_alias=0;
179: int line_no_alias_offset=0;
180: if(options) {
181: int valid_options=0;
182: if(Value* vmain_alias=options->get(PROCESS_MAIN_OPTION_NAME)) {
183: valid_options++;
184: main_alias=&vmain_alias->as_string();
185: }
186: if(Value* vfile_alias=options->get(PROCESS_FILE_OPTION_NAME)) {
187: valid_options++;
188: file_alias=&vfile_alias->as_string();
189: }
190: if(Value* vline_no_alias_offset=options->get(PROCESS_LINENO_OPTION_NAME)) {
191: valid_options++;
192: line_no_alias_offset=vline_no_alias_offset->as_int();
193: }
194:
195: if(valid_options!=options->count())
196: throw Exception("parser.runtime",
197: 0,
198: "called with invalid option");
199: }
1.129 paf 200:
1.146 paf 201: uint processe_file_no=file_alias?
202: r.register_file(r.absolute(*file_alias))
203: : pseudo_file_no__process;
204: // process...{string}
205: Value& vjunction=params.as_junction(index, "body must be code");
206: // evaluate source to process
207: const String& source=r.process_to_string(vjunction);
208: r.use_buf(*target_class,
209: source.cstr(String::L_UNSPECIFIED, r.connection(false)),
210: main_alias,
211: processe_file_no,
212: line_no_alias_offset);
1.1 paf 213:
1.73 paf 214: // main_method
1.129 paf 215: main_method=target_class->get_method(main_method_name);
1.73 paf 216: }
217: // after restoring current-request-lang
218: // maybe-execute @main[]
219: if(main_method) {
1.119 paf 220: // temporarily set method_frame's self to target_self
1.129 paf 221: Temp_method_frame_self tmfs(*r.get_method_frame(), *target_self);
1.73 paf 222: // execute!
223: r.execute(*main_method->parser_code);
1.1 paf 224: }
225: }
226:
1.138 paf 227: static void _rem(Request&, MethodParams& params) {
1.129 paf 228: params.as_junction(0, "body must be code");
1.1 paf 229: }
230:
1.129 paf 231: static void _while(Request& r, MethodParams& params) {
232: Value& vcondition=params.as_junction(0, "condition must be expression");
1.162 ! paf 233: Value& body_code=params.as_junction(1, "body must be code");
! 234: Value* delim_maybe_code=params.count()>2?¶ms[2]:0;
1.1 paf 235:
236: // while...
237: int endless_loop_count=0;
1.162 ! paf 238: bool need_delim=false;
1.1 paf 239: while(true) {
1.18 parser 240: if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.78 paf 241: throw Exception("parser.runtime",
1.129 paf 242: 0,
1.1 paf 243: "endless loop detected");
244:
1.86 paf 245: bool condition=r.process_to_value(vcondition,
1.1 paf 246: false/*don't intercept string*/).as_bool();
247: if(!condition) // ...condition is true
248: break;
249:
1.162 ! paf 250: StringOrValue sv_processed=r.process(body_code);
! 251: const String* s_processed=sv_processed.get_string();
! 252: if(delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
! 253: if(need_delim) // need delim & iteration produced string?
! 254: r.write_pass_lang(r.process(*delim_maybe_code));
! 255: need_delim=true;
! 256: }
! 257: r.write_pass_lang(sv_processed);
1.1 paf 258: }
259: }
260:
1.129 paf 261: static void _use(Request& r, MethodParams& params) {
262: Value& vfile=params.as_no_junction(0, "file name must not be code");
1.113 paf 263: r.use_file(r.main_class, vfile.as_string());
1.1 paf 264: }
265:
1.129 paf 266: static void _for(Request& r, MethodParams& params) {
267: const String& var_name=params.as_string(0, "var name must be string");
268: int from=params.as_int(1, "from must be int", r);
269: int to=params.as_int(2, "to must be int", r);
270: Value& body_code=params.as_junction(3, "body must be code");
271: Value* delim_maybe_code=params.count()>4?¶ms[4]:0;
1.1 paf 272:
1.57 paf 273: if(to-from>=MAX_LOOPS) // too long loop?
1.78 paf 274: throw Exception("parser.runtime",
1.129 paf 275: 0,
1.57 paf 276: "endless loop detected");
277:
1.1 paf 278: bool need_delim=false;
1.129 paf 279: VInt* vint=new VInt(0);
1.154 paf 280:
281: VMethodFrame& caller=*r.get_method_frame()->caller();
282: caller.put_element(caller, var_name, vint, false);
1.1 paf 283: for(int i=from; i<=to; i++) {
284: vint->set_int(i);
285:
1.108 paf 286: StringOrValue sv_processed=r.process(body_code);
1.129 paf 287: const String* s_processed=sv_processed.get_string();
288: if(delim_maybe_code && s_processed && s_processed->length()) { // delimiter set and we have body
1.108 paf 289: if(need_delim) // need delim & iteration produced string?
1.86 paf 290: r.write_pass_lang(r.process(*delim_maybe_code));
1.1 paf 291: need_delim=true;
292: }
1.108 paf 293: r.write_pass_lang(sv_processed);
1.1 paf 294: }
295: }
296:
1.129 paf 297: static void _eval(Request& r, MethodParams& params) {
298: Value& expr=params.as_junction(0, "need expression");
1.1 paf 299: // evaluate expresion
1.129 paf 300: Value& value_result=r.process_to_value(expr,
1.1 paf 301: true/*don't intercept string*/).as_expr_result();
1.129 paf 302: if(params.count()>1) {
303: Value& fmt=params.as_no_junction(1, "fmt must not be code");
304: r.write_no_lang(String(format(value_result.as_double(), fmt.as_string().cstrm())));
1.91 paf 305: } else
1.129 paf 306: r.write_no_lang(value_result);
1.1 paf 307: }
308:
1.129 paf 309: static void _connect(Request& r, MethodParams& params) {
1.63 paf 310: #ifdef RESOURCES_DEBUG
311: struct timeval mt[2];
312: #endif
1.129 paf 313: Value& url=params.as_no_junction(0, "url must not be code");
314: Value& body_code=params.as_junction(1, "body must be code");
1.1 paf 315:
1.129 paf 316: Table* protocol2driver_and_client=0;
317: if(Value* sql=r.main_class.get_element(String(MAIN_SQL_NAME), r.main_class, false)) {
318: if(Value* element=sql->get_element(String(MAIN_SQL_DRIVERS_NAME), *sql, false)) {
1.113 paf 319: protocol2driver_and_client=element->get_table();
320: }
321: }
1.11 paf 322:
1.63 paf 323: #ifdef RESOURCES_DEBUG
324: //measure:before
325: gettimeofday(&mt[0],NULL);
326: #endif
1.1 paf 327: // connect
1.142 paf 328: SQL_Connection* connection=SQL_driver_manager->get_connection(url.as_string(),
1.144 paf 329: protocol2driver_and_client,
330: r.charsets.source().NAME().cstr());
1.1 paf 331:
1.63 paf 332: #ifdef RESOURCES_DEBUG
333: //measure:after connect
334: gettimeofday(&mt[1],NULL);
335:
336: double t[2];
337: for(int i=0;i<2;i++)
338: t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
339:
340: r.sql_connect_time+=t[1]-t[0];
341: #endif
1.129 paf 342: Temp_connection temp_connection(r, connection);
1.1 paf 343: // execute body
1.53 parser 344: try {
1.86 paf 345: r.write_assign_lang(r.process(body_code));
1.129 paf 346: connection->commit();
347: connection->close();
1.75 paf 348: } catch(...) { // process problem
1.129 paf 349: connection->rollback();
350: connection->close();
351: rethrow;
1.1 paf 352: }
353: }
354:
1.41 parser 355: #ifndef DOXYGEN
1.129 paf 356: class Switch_data: public PA_Object {
357: public:
358: Request& r;
359: Value& searching;
360: Value* found;
361: Value* _default;
362: public:
363: Switch_data(Request& ar, Value& asearching):
364: r(ar), searching(asearching) {}
1.28 parser 365: };
1.41 parser 366: #endif
1.129 paf 367: static void _switch(Request& r, MethodParams& params) {
368: Switch_data* data=new Switch_data(r, r.process_to_value(params[0]));
1.135 paf 369: Temp_hash_value<const String::Body, void*>
1.129 paf 370: switch_data_setter(r.classes_conf, switch_data_name, data);
1.28 parser 371:
1.129 paf 372: Value& cases_code=params.as_junction(1, "switch cases must be code");
1.82 paf 373: // execution of found ^case[...]{code} must be in context of ^switch[...]{code}
374: // because of stacked WWrapper used there as wcontext
1.84 paf 375: r.process(cases_code, true/*intercept_string*/);
1.147 paf 376: if(Value* selected_code=data->found? data->found: data->_default)
1.107 paf 377: r.write_pass_lang(r.process(*selected_code));
1.28 parser 378: }
379:
1.129 paf 380: static void _case(Request& r, MethodParams& params) {
381: Switch_data* data=static_cast<Switch_data*>(r.classes_conf.get(switch_data_name));
1.38 parser 382: if(!data)
1.78 paf 383: throw Exception("parser.runtime",
1.129 paf 384: 0,
1.38 parser 385: "without switch");
1.28 parser 386:
1.129 paf 387: int count=params.count();
388: Value& code=params.as_junction(--count, "case result must be code");
1.83 paf 389:
1.28 parser 390: for(int i=0; i<count; i++) {
1.129 paf 391: Value& value=r.process_to_value(params[i]);
1.28 parser 392:
1.157 paf 393: if(value.is_string() && value.as_string() == CASE_DEFAULT_VALUE) {
1.129 paf 394: data->_default=&code;
1.28 parser 395: break;
396: }
397:
398: bool matches;
1.129 paf 399: if(data->searching.is_string())
400: matches=data->searching.as_string() == value.as_string();
1.28 parser 401: else
1.129 paf 402: matches=data->searching.as_double() == value.as_double();
1.28 parser 403:
404: if(matches) {
1.82 paf 405: if(data->found)
406: throw Exception("parser.runtime",
1.129 paf 407: 0,
1.82 paf 408: "duplicate found");
409:
1.129 paf 410: data->found=&code;
1.28 parser 411: break;
412: }
413: }
414: }
1.136 paf 415: #ifndef DOXYGEN
416: struct Try_catch_result {
417: StringOrValue processed_code;
418: const String* exception_should_be_handled;
419:
420: Try_catch_result(): exception_should_be_handled(0) {}
421: };
422: #endif
423:
424: /// used by ^try and ^cache, @returns $exception.handled[string] if any
425: template<class I>
426: static Try_catch_result try_catch(Request& r,
427: StringOrValue body_code(Request&, I), I info,
1.160 paf 428: Value* catch_code, bool could_be_handled_by_caller=false)
1.136 paf 429: {
430: Try_catch_result result;
431: if(!catch_code) {
432: result.processed_code=body_code(r, info);
433: return result;
434: }
435:
436: // taking snapshot of try-context
437: Request_context_saver try_context(r);
438: try {
439: result.processed_code=body_code(r, info);
440: } catch(const Exception& e) {
441: Request_context_saver throw_context(r); // taking snapshot of throw-context [stack trace contains error]
442: Request::Exception_details details=r.get_details(e);
443: try_context.restore(); // restoring try-context to perform catch-code
444:
445: Junction* junction=catch_code->get_junction();
446: Value* method_frame=junction->method_frame;
447: Value* saved_exception_var_value=method_frame->get_element(exception_var_name, *method_frame, false);
1.154 paf 448: VMethodFrame& frame=*junction->method_frame;
449: frame.put_element(frame, exception_var_name, &details.vhash, false);
1.136 paf 450: result.processed_code=r.process(*catch_code);
451:
452: // retriving $exception.handled, restoring $exception var
453: Value* vhandled=details.vhash.hash().get(exception_handled_part_name);
1.154 paf 454: frame.put_element(frame, exception_var_name, saved_exception_var_value, false);
1.136 paf 455:
456: bool bhandled=false;
457: if(vhandled) {
458: if(vhandled->is_string()) { // not simple $exception.handled(1/0)?
1.160 paf 459: if(could_be_handled_by_caller) { // and we can possibly handle it
460: result.exception_should_be_handled=vhandled->get_string(); // considering 'recovered' and let the caller recover
461: return result;
462: }
463:
464: bhandled=false;
465: } else
466: bhandled=vhandled->as_bool();
1.136 paf 467: }
468:
469: if(!bhandled) {
470: throw_context.restore(); // restoring throw-context [exception were not handled]
471: rethrow;
472: }
473: }
474: return result;
475: }
1.28 parser 476:
1.63 paf 477: // cache--
478:
479: // consts
480:
1.152 paf 481: const int DATA_STRING_SERIALIZED_VERSION=0x0006;
1.63 paf 482:
483: // helper types
484:
485: #ifndef DOXYGEN
486: struct Data_string_serialized_prolog {
487: int version;
1.79 paf 488: time_t expires;
1.63 paf 489: };
490: #endif
491:
1.68 paf 492: void cache_delete(const String& file_spec) {
493: file_delete(file_spec, false/*fail_on_read_problem*/);
1.63 paf 494: }
1.69 paf 495:
496: #ifndef DOXYGEN
1.135 paf 497: struct Cache_scope {
1.129 paf 498: public:
1.79 paf 499: time_t expires;
1.135 paf 500: const String* body_from_disk;
1.79 paf 501: };
1.69 paf 502: struct Locked_process_and_cache_put_action_info {
503: Request *r;
1.136 paf 504: Cache_scope *scope;
505: Value* body_code; Value* catch_code;
506: const String* processed_code;
1.69 paf 507: };
508: #endif
1.136 paf 509:
510:
511:
512: static StringOrValue process_cache_body_code(Request& r, Value* body_code) {
1.153 paf 513: return StringOrValue(r.process_to_string(*body_code));
1.136 paf 514: }
515:
1.129 paf 516: /* @todo maybe network order worth spending some effort?
517: don't bothering myself with network byte order,
518: am not planning to be able to move resulting file across platforms
519: */
1.69 paf 520: static void locked_process_and_cache_put_action(int f, void *context) {
521: Locked_process_and_cache_put_action_info& info=
522: *static_cast<Locked_process_and_cache_put_action_info *>(context);
1.129 paf 523:
1.160 paf 524:
525: const String* body_from_disk=info.scope->body_from_disk;
1.69 paf 526: // body->process
1.136 paf 527: Try_catch_result result=try_catch(*info.r,
528: process_cache_body_code, info.body_code,
1.160 paf 529: info.catch_code, body_from_disk!=0 /*we have something old=we can handle=recover later*/);
1.136 paf 530:
531: if(result.exception_should_be_handled) {
532: if(*result.exception_should_be_handled==CACHE_EXCEPTION_HANDLED_CACHE_NAME) {
1.160 paf 533: assert(body_from_disk);
534: info.processed_code=body_from_disk;
1.136 paf 535: } else
536: throw Exception("parser.runtime",
537: result.exception_should_be_handled,
538: "$"EXCEPTION_VAR_NAME"."EXCEPTION_HANDLED_PART_NAME" value must be "
539: "either boolean or string '"CACHE_EXCEPTION_HANDLED_CACHE_NAME"'");
540: } else
541: info.processed_code=&result.processed_code.as_string();
1.69 paf 542:
1.79 paf 543: // expiration time not spoiled by ^cache(0) or something?
1.136 paf 544: if(info.scope->expires > time(0)) {
1.79 paf 545: // string -serialize> buffer
1.136 paf 546: String::Cm serialized=info.processed_code->serialize(
1.129 paf 547: sizeof(Data_string_serialized_prolog));
1.79 paf 548: Data_string_serialized_prolog& prolog=
1.129 paf 549: *reinterpret_cast<Data_string_serialized_prolog *>(serialized.str);
1.79 paf 550: prolog.version=DATA_STRING_SERIALIZED_VERSION;
1.136 paf 551: prolog.expires=info.scope->expires;
1.79 paf 552:
553: // buffer -write> file
1.129 paf 554: write(f, serialized.str, serialized.length);
1.79 paf 555: } else // expired!
1.136 paf 556: info.scope->expires=0; // flag it so that could be easily checked by caller
1.69 paf 557: }
1.129 paf 558: const String* locked_process_and_cache_put(Request& r,
1.128 paf 559: Value& body_code,
1.136 paf 560: Value* catch_code,
561: Cache_scope& scope,
562: const String& file_spec)
563: {
1.140 paf 564: Locked_process_and_cache_put_action_info info={&r, &scope, &body_code, catch_code, 0};
1.69 paf 565:
1.129 paf 566: const String* result=file_write_action_under_lock(
1.69 paf 567: file_spec,
568: "cache_put", locked_process_and_cache_put_action, &info,
569: false/*as_text*/,
570: false/*do_append*/,
1.96 paf 571: false/*block*/,
1.136 paf 572: false/*fail on lock problem*/) ? info.processed_code: 0;
1.100 paf 573: time_t now=time(0);
1.136 paf 574: if(scope.expires<=now)
1.79 paf 575: cache_delete(file_spec);
576: return result;
1.63 paf 577: }
1.137 paf 578: #ifndef DOXYGEN
1.135 paf 579: struct Cache_get_result {
580: const String* body;
581: bool expired;
1.137 paf 582: };
583: #endif
584: static Cache_get_result cache_get(Request_charsets& charsets, const String& file_spec, time_t now) {
1.140 paf 585: Cache_get_result result={0, false};
1.135 paf 586:
1.129 paf 587: File_read_result file=file_read(charsets, file_spec,
1.63 paf 588: false/*as_text*/,
1.129 paf 589: 0, //no params
590: false/*fail_on_read_problem*/);
591: if(file.success && file.length/* ignore reads which are empty due to
1.72 paf 592: non-unary open+lockEX conflict with lockSH */) {
1.129 paf 593:
1.72 paf 594: Data_string_serialized_prolog& prolog=
1.129 paf 595: *reinterpret_cast<Data_string_serialized_prolog *>(file.str);
1.63 paf 596:
1.135 paf 597: String* body=new String;
1.72 paf 598: if(
1.129 paf 599: file.length>=sizeof(Data_string_serialized_prolog)
1.135 paf 600: && prolog.version==DATA_STRING_SERIALIZED_VERSION) {
601: if(body->deserialize(sizeof(Data_string_serialized_prolog), file.str, file.length)) {
602: result.body=body;
603: result.expired=prolog.expires <= now;
604: }
605: }
1.72 paf 606: }
1.63 paf 607:
1.135 paf 608: return result;
1.63 paf 609: }
1.129 paf 610: static time_t as_expires(Request& r, MethodParams& params,
1.79 paf 611: int index, time_t now) {
612: time_t result;
1.129 paf 613: if(Value* vdate=params[index].as(VDATE_TYPE, false))
614: result=static_cast<VDate*>(vdate)->get_time();
1.79 paf 615: else
1.129 paf 616: result=now+(time_t)params.as_double(index, "lifespan must be date or number", r);
1.79 paf 617:
618: return result;
619: }
1.129 paf 620: static const String& as_file_spec(Request& r, MethodParams& params, int index) {
621: return r.absolute(params.as_string(index, "filespec must be string"));
1.79 paf 622: }
1.129 paf 623: static void _cache(Request& r, MethodParams& params) {
1.158 paf 624: if(params.count()==0)
625: {
626: // return current expiration time
627: Cache_scope* scope=static_cast<Cache_scope*>(r.classes_conf.get(cache_data_name));
628: if(!scope)
629: throw Exception("parser.runtime",
630: 0,
631: "expire-time get without cache");
632: r.write_no_lang(*new VDate(scope->expires));
633: return;
634: }
635:
1.79 paf 636: time_t now=time(0);
637:
638: // ^cache[filename] ^cache(seconds) ^cache[expires date]
1.129 paf 639: if(params.count()==1) {
640: if(params[0].is_string()) { // filename?
1.79 paf 641: cache_delete(as_file_spec(r, params, 0));
642: return;
643: }
644:
645: // secods|expires date
1.135 paf 646: Cache_scope* scope=static_cast<Cache_scope*>(r.classes_conf.get(cache_data_name));
647: if(!scope)
1.79 paf 648: throw Exception("parser.runtime",
1.129 paf 649: 0,
1.79 paf 650: "expire-time reducing instruction without cache");
651:
1.129 paf 652: time_t expires=as_expires(r, params, 0, now);
1.135 paf 653: if(expires < scope->expires)
654: scope->expires=expires;
1.79 paf 655:
656: return;
1.149 paf 657: } else if(params.count()<3)
658: throw Exception("parser.runtime",
659: 0,
660: "invalid number of parameters");
1.63 paf 661:
662: // file_spec, expires, body code
1.129 paf 663: const String& file_spec=r.absolute(params.as_string(0, "filespec must be string"));
1.65 paf 664:
1.141 paf 665: Cache_scope scope={as_expires(r, params, 1, now), 0};
1.135 paf 666:
667: Temp_hash_value<const String::Body, void*>
668: cache_scope_setter(r.classes_conf, cache_data_name, &scope);
1.136 paf 669: Value& body_code=params.as_junction(2, "body_code must be code");
670: Value* catch_code=0;
671: if(params.count()>3)
672: catch_code=¶ms.as_junction(3, "catch_code must be code");
1.63 paf 673:
1.135 paf 674: if(scope.expires>now) { // valid 'expires' specified? try cached copy...
1.69 paf 675: // hence we don't hope to have unary create/lockEX
676: // we need some plan to live in a life like that, so...
677: // worst races plan:
678: // A B
679: // open
680: // |open
681: // lockSH
682: // |nonblocking-lockEX fails
683: // unlockSH
684: // close, cache_get returns 0
685: // open
686: // nonblocking-lockEX succeeds; process, write, close
687: // |retry1: open
688: // ...
689: // |lockSH succeeds; ...
690:
691: for(int retry=0; retry<2; retry++) {
1.135 paf 692: Cache_get_result cached=cache_get(r.charsets, file_spec, now);
1.136 paf 693: if(cached.body) { // have cached copy
1.135 paf 694: if(cached.expired)
695: scope.body_from_disk=cached.body; // storing for user to retrive it with ^cache[]
1.136 paf 696: else // and it's not expired yet
697: {
1.135 paf 698: // write it out
699: r.write_assign_lang(*cached.body);
700: // happy with it
701: return;
702: }
1.79 paf 703: }
1.69 paf 704:
1.135 paf 705: // non-blocked lock; process; cache it
706: if(const String* processed_body=
1.136 paf 707: locked_process_and_cache_put(r, body_code, catch_code, scope, file_spec)) {
1.135 paf 708: // write it out
709: r.write_assign_lang(*processed_body);
710: // happy with it
711: return;
712: } else { // somebody writing result right now
713: pa_sleep(0, 500000); // waiting half a second
714: retry=0; // prolonging our wait, than could cache_get it, without processing body_code
715: }
1.69 paf 716: }
1.78 paf 717: throw Exception(0,
1.69 paf 718: &file_spec,
719: "locking problem");
720: } else {
1.79 paf 721: // instructed not to cache; forget cached copy
1.68 paf 722: cache_delete(file_spec);
1.69 paf 723: // process
1.81 paf 724: const String& processed_body=r.process_to_string(body_code);
1.69 paf 725: // write it out
726: r.write_assign_lang(processed_body);
727: // happy with it
728: return;
729: }
730: // never reached
1.63 paf 731: }
732:
1.136 paf 733: static StringOrValue process_try_body_code(Request& r, Value* body_code) {
734: return r.process(*body_code);
735: }
1.129 paf 736: static void _try_operator(Request& r, MethodParams& params) {
737: Value& body_code=params.as_junction(0, "body_code must be code");
738: Value& catch_code=params.as_junction(1, "catch_code must be code");
1.74 paf 739:
1.136 paf 740: Try_catch_result result=try_catch(r,
741: process_try_body_code, &body_code,
742: &catch_code);
743:
744: if(result.exception_should_be_handled)
745: throw Exception("parser.runtime",
746: result.exception_should_be_handled,
747: "catch block must set $exception.handled to some boolean value, not string");
1.123 paf 748:
1.136 paf 749: // write out processed body_code or catch_code
750: r.write_pass_lang(result.processed_code);
1.74 paf 751: }
752:
1.138 paf 753: static void _throw_operator(Request&, MethodParams& params) {
1.129 paf 754: if(params.count()==1) {
755: if(HashStringValue *hash=params[0].get_hash()) {
756: const char* type=0;
757: if(Value* value=hash->get(exception_type_part_name))
1.78 paf 758: type=value->as_string().cstr();
1.129 paf 759: const String* source=0;
760: if(Value* value=hash->get(exception_source_part_name))
1.74 paf 761: source=&value->as_string();
1.138 paf 762: const char* comment=0;
1.129 paf 763: if(Value* value=hash->get(exception_comment_part_name))
1.74 paf 764: comment=value->as_string().cstr();
765:
1.78 paf 766: throw Exception(type,
1.129 paf 767: source?source:0,
768: "%s", comment?comment:"");
1.74 paf 769: } else
1.78 paf 770: throw Exception("parser.runtime",
1.129 paf 771: 0,
1.74 paf 772: "one-param version has hash param");
773: } else {
1.129 paf 774: const char* type=params.as_string(0, "type must be string").cstr();
775: const String& source=params.as_string(1, "source must be string");
776: const char* comment=params.count()>2? params.as_string(2, "comment must be string").cstr()
777: :0;
1.97 paf 778: throw Exception(type, &source, "%s", comment?comment:"");
1.74 paf 779: }
1.132 paf 780: }
1.129 paf 781:
1.150 paf 782: static void _sleep_operator(Request& r, MethodParams& params) {
783: double seconds=params.as_double(0, "seconds must be double", r);
1.151 paf 784: pa_sleep((int)trunc(seconds), (int)trunc(seconds*1000));
1.150 paf 785: }
786:
1.129 paf 787: #if defined(WIN32) && defined(_DEBUG)
788: # define PA_BPT
1.138 paf 789: static void _bpt(Request&, MethodParams&) {
1.129 paf 790: _asm int 3;
791: }
792: #endif
793:
1.10 paf 794: // constructor
795:
1.129 paf 796: VClassMAIN::VClassMAIN(): VClass() {
797: set_name(*new String(MAIN_CLASS_NAME));
798:
799: #ifdef PA_BPT
800: // ^bpt[]
801: add_native_method("bpt", Method::CT_ANY, _bpt, 0, 0);
802: #endif
1.121 paf 803:
1.1 paf 804: // ^if(condition){code-when-true}
805: // ^if(condition){code-when-true}{code-when-false}
1.10 paf 806: add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1 paf 807:
808: // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59 paf 809: add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1 paf 810:
811: // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10 paf 812: add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1 paf 813:
814: // ^process[code]
1.146 paf 815: add_native_method("process", Method::CT_ANY, _process, 1, 3);
1.1 paf 816:
817: // ^rem{code}
1.51 parser 818: add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1 paf 819:
820: // ^while(condition){code}
1.162 ! paf 821: add_native_method("while", Method::CT_ANY, _while, 2, 3);
1.1 paf 822:
823: // ^use[file]
1.10 paf 824: add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1 paf 825:
1.54 paf 826: // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10 paf 827: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 828:
829: // ^eval(expr)
830: // ^eval(expr)[format]
1.10 paf 831: add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52 parser 832:
1.1 paf 833: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 834: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
835:
1.63 paf 836:
1.136 paf 837: // ^cache[file_spec](time){code}[{catch code}] time=0 no cache
1.65 paf 838: // ^cache[file_spec] delete cache
1.161 paf 839: // ^cache[] get current expiration time
840: add_native_method("cache", Method::CT_ANY, _cache, 0, 4);
1.63 paf 841:
1.28 parser 842: // switch
843:
844: // ^switch[value]{cases}
845: add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
846:
847: // ^case[value]{code}
1.51 parser 848: add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.74 paf 849:
850: // try-catch
851:
852: // ^try{code}{catch code}
853: add_native_method("try", Method::CT_ANY, _try_operator, 2, 2);
854: // ^throw[$exception hash]
855: // ^throw[type;source;comment]
856: add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
857:
1.150 paf 858: add_native_method("sleep", Method::CT_ANY, _sleep_operator, 1, 1);
1.10 paf 859: }
1.11 paf 860:
861: // constructor & configurator
1.1 paf 862:
1.129 paf 863: VStateless_class& VClassMAIN_create() {
864: return *new VClassMAIN;
1.1 paf 865: }
E-mail: