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