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