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