Annotation of parser3/src/classes/op.C, revision 1.73
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.1 paf 6:
1.73 ! paf 7: $Id: op.C,v 1.72 2002/03/04 10:03:35 paf Exp $
1.1 paf 8: */
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"
15:
1.18 parser 16: // limits
17:
18: #define MAX_LOOPS 10000
19:
1.10 paf 20: // defines
21:
22: #define OP_CLASS_NAME "OP"
1.11 paf 23:
1.10 paf 24: // class
25:
26: class MOP : public Methoded {
27: public:
28: MOP(Pool& pool);
1.11 paf 29: public: // Methoded
1.10 paf 30: bool used_directly() { return true; }
1.11 paf 31: void configure_user(Request& r);
1.36 parser 32:
1.11 paf 33: private:
34: String main_sql_name;
35: String main_sql_drivers_name;
1.10 paf 36: };
37:
38: // methods
39:
1.5 paf 40: static void _if(Request& r, const String&, MethodParams *params) {
1.48 parser 41: Value& condition_code=params->as_junction(0, "condition must be expression");
1.1 paf 42:
43: bool condition=r.process(condition_code,
44: 0/*no name*/,
45: false/*don't intercept string*/).as_bool();
1.6 paf 46: if(condition)
1.34 parser 47: r.write_pass_lang(r.process(params->as_junction(1, "'then' parameter must be code")));
1.50 parser 48: else if(params->size()>2)
1.34 parser 49: r.write_pass_lang(r.process(params->as_junction(2, "'else' parameter must be code")));
1.1 paf 50: }
51:
1.5 paf 52: static void _untaint(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 53: Pool& pool=r.pool();
54:
1.60 paf 55: uchar lang;
1.59 paf 56: if(params->size()==1)
57: lang=String::UL_AS_IS; // mark as simply 'tainted'. useful in html from sql
58: else {
59: const String& lang_name=params->as_string(0, "lang must be string");
1.60 paf 60: lang=untaint_lang_name2enum->get_int(lang_name);
1.59 paf 61: if(!lang)
62: throw Exception(0, 0,
63: &lang_name,
64: "invalid taint language");
65: }
1.1 paf 66:
67: {
1.59 paf 68: Value& vbody=params->as_junction(params->size()-1, "body must be code");
1.1 paf 69:
70: Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
1.5 paf 71: r.write_pass_lang(r.process(vbody)); // process marking tainted with that lang
1.1 paf 72: }
73: }
74:
1.5 paf 75: static void _taint(Request& r, const String&, MethodParams *params) {
1.1 paf 76: Pool& pool=r.pool();
77:
1.60 paf 78: uchar lang;
1.1 paf 79: if(params->size()==1)
80: lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
81: else {
1.48 parser 82: const String& lang_name=params->as_string(0, "lang must be string");
1.60 paf 83: lang=untaint_lang_name2enum->get_int(lang_name);
1.1 paf 84: if(!lang)
1.53 parser 85: throw Exception(0, 0,
1.3 paf 86: &lang_name,
87: "invalid taint language");
1.1 paf 88: }
89:
90: {
1.34 parser 91: Value& vbody=params->as_no_junction(params->size()-1, "body must not be code");
1.1 paf 92:
93: String result(r.pool());
94: result.append(
95: vbody.as_string(), // process marking tainted with that lang
96: lang, true); // force result language to specified
97: r.write_pass_lang(result);
98: }
99: }
100:
1.5 paf 101: static void _process(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 102: // calculate pseudo file name of processed chars
103: // would be something like "/some/file(4) process"
104: char place[MAX_STRING];
105: #ifndef NO_STRING_ORIGIN
106: const Origin& origin=method_name.origin();
107: snprintf(place, MAX_STRING, "%s(%d) %s",
108: origin.file, 1+origin.line,
109: method_name.cstr());
110: #else
1.39 parser 111: strncpy(place, method_name.cstr(), MAX_STRING-1); place[MAX_STRING-1]=0;
1.1 paf 112: #endif
113:
114: VStateless_class& self_class=*r.self->get_class();
1.73 ! paf 115: const Method *main_method;
1.1 paf 116: {
1.73 ! paf 117: // temporary remove language change
! 118: Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.1 paf 119: // temporary zero @main so to maybe-replace it in processed code
120: Temp_method temp_method_main(self_class, *main_method_name, 0);
121: // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
122: Temp_method temp_method_auto(self_class, *auto_method_name, 0);
123:
124: // evaluate source to process
125: const String& source=
1.61 paf 126: r.process(params->as_junction(0, "body must be code")).as_string();
1.1 paf 127:
128: // process source code, append processed methods to 'self' class
129: // maybe-define new @main
1.67 paf 130: r.use_buf(
131: source.cstr(String::UL_UNSPECIFIED, r.connection(0)),
132: place,
133: &self_class);
1.1 paf 134:
1.73 ! paf 135: // main_method
! 136: main_method=self_class.get_method(*main_method_name);
! 137: }
! 138: // after restoring current-request-lang
! 139: // maybe-execute @main[]
! 140: if(main_method) {
! 141: // execute!
! 142: r.execute(*main_method->parser_code);
1.1 paf 143: }
144: }
145:
1.5 paf 146: static void _rem(Request& r, const String&, MethodParams *params) {
1.34 parser 147: params->as_junction(0, "body must be code");
1.1 paf 148: }
149:
1.5 paf 150: static void _while(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 151: Pool& pool=r.pool();
152:
1.34 parser 153: Value& vcondition=params->as_junction(0, "condition must be expression");
154: Value& body=params->as_junction(1, "body must be code");
1.1 paf 155:
156: // while...
157: int endless_loop_count=0;
158: while(true) {
1.18 parser 159: if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.53 parser 160: throw Exception(0, 0,
1.1 paf 161: &method_name,
162: "endless loop detected");
163:
164: bool condition=
165: r.process(
166: vcondition,
167: 0/*no name*/,
168: false/*don't intercept string*/).as_bool();
169: if(!condition) // ...condition is true
170: break;
171:
172: // write processed body
173: r.write_pass_lang(r.process(body));
174: }
175: }
176:
1.5 paf 177: static void _use(Request& r, const String& method_name, MethodParams *params) {
1.34 parser 178: Value& vfile=params->as_no_junction(0, "file name must not be code");
1.37 parser 179: r.use_file(vfile.as_string());
1.1 paf 180: }
181:
1.5 paf 182: static void _for(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 183: Pool& pool=r.pool();
1.48 parser 184: const String& var_name=params->as_string(0, "var name must be string");
185: int from=params->as_int(1, "from must be int", r);
186: int to=params->as_int(2, "to must be int", r);
1.34 parser 187: Value& body_code=params->as_junction(3, "body must be code");
1.50 parser 188: Value *delim_maybe_code=params->size()>4?¶ms->get(4):0;
1.1 paf 189:
1.57 paf 190: if(to-from>=MAX_LOOPS) // too long loop?
191: throw Exception(0, 0,
192: &method_name,
193: "endless loop detected");
194:
1.1 paf 195: bool need_delim=false;
196: VInt *vint=new(pool) VInt(pool, 0);
197: for(int i=from; i<=to; i++) {
198: vint->set_int(i);
1.56 paf 199: r.root->put_element(var_name, vint);
1.1 paf 200:
201: Value& processed_body=r.process(body_code);
1.49 parser 202: if(delim_maybe_code) { // delimiter set?
1.1 paf 203: const String *string=processed_body.get_string();
204: if(need_delim && string && string->size()) // need delim & iteration produced string?
1.49 parser 205: r.write_pass_lang(r.process(*delim_maybe_code));
1.1 paf 206: need_delim=true;
207: }
208: r.write_pass_lang(processed_body);
209: }
210: }
211:
1.15 paf 212: static void _eval(Request& r, const String& method_name, MethodParams *params) {
1.34 parser 213: Value& expr=params->as_junction(0, "need expression");
1.1 paf 214: // evaluate expresion
215: Value *result=r.process(expr,
1.16 paf 216: 0/*no name YET*/,
1.1 paf 217: true/*don't intercept string*/).as_expr_result();
1.50 parser 218: if(params->size()>1) {
1.34 parser 219: Value& fmt=params->as_no_junction(1, "fmt must not be code");
1.1 paf 220:
221: Pool& pool=r.pool();
222: String& string=*new(pool) String(pool);
223: string.APPEND_CONST(format(pool, result->as_double(), fmt.as_string().cstr()));
224: result=new(pool) VString(string);
225: }
1.16 paf 226: result->set_name(method_name);
1.1 paf 227: r.write_no_lang(*result);
228: }
229:
1.52 parser 230: static void _error(Request& r, const String& method_name, MethodParams *params) {
231: Pool& pool=r.pool();
232:
233: const String& serror=params->as_string(0, "message must be string");
1.53 parser 234: throw Exception(0, 0,
1.52 parser 235: &method_name,
236: "%s", serror.cstr());
237: }
238:
1.1 paf 239:
1.53 parser 240: /// @todo rewrite ugly code with try/try to autoobject TempConnection
1.42 parser 241: static void _connect(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 242: Pool& pool=r.pool();
1.63 paf 243: #ifdef RESOURCES_DEBUG
244: struct timeval mt[2];
245: #endif
1.34 parser 246: Value& url=params->as_no_junction(0, "url must not be code");
247: Value& body_code=params->as_junction(1, "body must be code");
1.1 paf 248:
1.19 parser 249: Table *protocol2driver_and_client=
1.35 parser 250: static_cast<Table *>(r.classes_conf.get(r.OP.name()));
1.11 paf 251:
1.63 paf 252: #ifdef RESOURCES_DEBUG
253: //measure:before
254: gettimeofday(&mt[0],NULL);
255: #endif
1.1 paf 256: // connect
1.67 paf 257: SQL_Connection_ptr connection=SQL_driver_manager->get_connection(
1.42 parser 258: url.as_string(), method_name, protocol2driver_and_client);
1.1 paf 259:
1.63 paf 260: #ifdef RESOURCES_DEBUG
261: //measure:after connect
262: gettimeofday(&mt[1],NULL);
263:
264: double t[2];
265: for(int i=0;i<2;i++)
266: t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
267:
268: r.sql_connect_time+=t[1]-t[0];
269: #endif
1.67 paf 270: Temp_connection temp_connection(r, connection.get());
1.1 paf 271: // execute body
1.53 parser 272: try {
1.67 paf 273: r.write_assign_lang(r.process(body_code));
274:
275: } catch(...) { // process/commit problem
276: connection->mark_to_rollback();
277: /*re*/throw;
1.1 paf 278: }
279: }
280:
1.41 parser 281: #ifndef DOXYGEN
1.28 parser 282: struct Switch_data {
283: Value *searching;
284: Value *found;
285: Value *_default;
286: };
1.41 parser 287: #endif
1.28 parser 288: static void _switch(Request& r, const String&, MethodParams *params) {
289: void *backup=r.classes_conf.get(*switch_data_name);
290: Switch_data data={&r.process(params->get(0))};
291: r.classes_conf.put(*switch_data_name, &data);
292:
1.34 parser 293: r.process(params->as_junction(1, "switch cases must be code")); // and ignore result
1.28 parser 294:
295: r.classes_conf.put(*switch_data_name, backup);
296:
297: if(Value *code=data.found ? data.found : data._default)
298: r.write_pass_lang(r.process(*code));
299: }
300:
1.38 parser 301: static void _case(Request& r, const String& method_name, MethodParams *params) {
302: Pool& pool=r.pool();
303:
304: Switch_data *data=static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
305: if(!data)
1.53 parser 306: throw Exception(0, 0,
1.38 parser 307: &method_name,
308: "without switch");
1.28 parser 309:
310: int count=params->size();
1.34 parser 311: Value *code=¶ms->as_junction(--count, "case result must be code");
1.28 parser 312: for(int i=0; i<count; i++) {
313: Value& value=r.process(params->get(i));
314:
1.36 parser 315: if(value.as_string() == *case_default_value) {
1.38 parser 316: data->_default=code;
1.28 parser 317: break;
318: }
319:
320: bool matches;
1.38 parser 321: if(data->searching->is_string())
322: matches=data->searching->as_string() == value.as_string();
1.28 parser 323: else
1.38 parser 324: matches=data->searching->as_double() == value.as_double();
1.28 parser 325:
326: if(matches) {
1.38 parser 327: data->found=code;
1.28 parser 328: break;
329: }
330: }
331: }
332:
1.63 paf 333: // cache--
334:
335: // consts
336:
337: const int DATA_STRING_SERIALIZED_VERSION=0x0001;
338:
339: // helper types
340:
341: #ifndef DOXYGEN
342: struct Data_string_serialized_prolog {
343: int version;
344: };
345: #endif
346:
1.68 paf 347: void cache_delete(const String& file_spec) {
348: file_delete(file_spec, false/*fail_on_read_problem*/);
1.63 paf 349: }
1.69 paf 350:
351: #ifndef DOXYGEN
352: struct Locked_process_and_cache_put_action_info {
353: Request *r;
354: Value *body;
355: };
356: #endif
357: static void locked_process_and_cache_put_action(int f, void *context) {
358: Locked_process_and_cache_put_action_info& info=
359: *static_cast<Locked_process_and_cache_put_action_info *>(context);
360:
361: // body->process
362: info.body=&info.r->process(*info.body);
363:
364: // result->string
365: const String& data_string=info.body->as_string();
366:
367: // string -serialize> buffer
1.63 paf 368: void *data; size_t data_size;
369: data_string.serialize(
370: sizeof(Data_string_serialized_prolog),
371: data, data_size);
372: Data_string_serialized_prolog& prolog=
373: *static_cast<Data_string_serialized_prolog *>(data);
374: prolog.version=DATA_STRING_SERIALIZED_VERSION;
1.69 paf 375:
376: // buffer -write> file
377: write(f, data, data_size);
378: }
379: Value *locked_process_and_cache_put(Request& r,
380: Value& body_code,
381: const String& file_spec) {
382: Locked_process_and_cache_put_action_info info={
383: &r,
384: &body_code,
385: };
386:
387: return file_write_action_under_lock(
388: file_spec,
389: "cache_put", locked_process_and_cache_put_action, &info,
390: false/*as_text*/,
391: false/*do_append*/,
392: false/*block*/) ? info.body : 0;
1.63 paf 393: }
394: String *cache_get(Pool& pool, const String& file_spec) {
395: void* data; size_t data_size;
1.72 paf 396: if(file_read(pool, file_spec,
1.63 paf 397: data, data_size,
398: false/*as_text*/,
1.69 paf 399: false/*fail_on_read_problem*/)
1.72 paf 400: && data_size/* ignore reads which are empty due to
401: non-unary open+lockEX conflict with lockSH */) {
1.63 paf 402:
1.72 paf 403: Data_string_serialized_prolog& prolog=
404: *static_cast<Data_string_serialized_prolog *>(data);
1.63 paf 405:
1.72 paf 406: String *result=new(pool) String(pool);
407: if(
408: data_size>=sizeof(Data_string_serialized_prolog)
409: && prolog.version==DATA_STRING_SERIALIZED_VERSION
410: && result->deserialize(
411: sizeof(Data_string_serialized_prolog), data, data_size, file_spec.cstr()))
412: return result;
413: }
1.63 paf 414:
1.72 paf 415: return 0;
1.63 paf 416: }
417: static void _cache(Request& r, const String& method_name, MethodParams *params) {
418: Pool& pool=r.pool();
419:
420: // file_spec, expires, body code
1.65 paf 421: const String &file_spec=r.absolute(params->as_string(0, "filespec must be string"));
422: if(params->size()==1) { // delete
1.68 paf 423: cache_delete(file_spec);
1.65 paf 424: return;
425: }
426:
1.63 paf 427: time_t lifespan=(time_t)params->as_double(1, "lifespan must be number", r);
428: Value& body_code=params->as_junction(2, "body must be code");
429:
430: if(lifespan) { // 'lifespan' specified? try cached copy...
431: size_t size;
432: time_t atime, mtime, ctime;
1.69 paf 433:
434: // hence we don't hope to have unary create/lockEX
435: // we need some plan to live in a life like that, so...
436: // worst races plan:
437: // A B
438: // open
439: // |open
440: // lockSH
441: // |nonblocking-lockEX fails
442: // unlockSH
443: // close, cache_get returns 0
444: // open
445: // nonblocking-lockEX succeeds; process, write, close
446: // |retry1: open
447: // ...
448: // |lockSH succeeds; ...
449:
1.63 paf 450: // {file_spec} modification time
1.69 paf 451: for(int retry=0; retry<2; retry++) {
452: if(file_stat(file_spec, size, atime, mtime, ctime, false/*no exception on error*/)) // exists?
453: if(time(0)-mtime > lifespan) // expired
454: cache_delete(file_spec);
455: else // not expired
456: if(String *cached_body=cache_get(pool, file_spec)) { // have cached copy?
457: // write it out
458: r.write_assign_lang(*cached_body);
459: // happy with it
460: return;
461: }
462:
463: // non-blocked lock; process; cache it
464: if(Value *processed_body=locked_process_and_cache_put(r, body_code, file_spec)) {
1.63 paf 465: // write it out
1.69 paf 466: r.write_assign_lang(*processed_body);
1.63 paf 467: // happy with it
468: return;
1.69 paf 469: } else { // somebody writing result right now
470: pa_sleep(0, 500000); // waiting half a second
471: retry=0; // prolonging our wait, than could cache_get it, without processing body_code
1.63 paf 472: }
1.69 paf 473: }
474: throw Exception(0, 0,
475: &file_spec,
476: "locking problem");
477: } else {
478: // 'lifespan'=0, forget cached copy
1.68 paf 479: cache_delete(file_spec);
1.69 paf 480: // process
481: Value& processed_body=r.process(body_code);
482: // write it out
483: r.write_assign_lang(processed_body);
484: // happy with it
485: return;
486: }
487: // never reached
1.63 paf 488: }
489:
1.10 paf 490: // constructor
491:
1.11 paf 492: MOP::MOP(Pool& apool) : Methoded(apool),
493: main_sql_name(apool, MAIN_SQL_NAME),
494: main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
495: {
1.10 paf 496: set_name(*NEW String(pool(), OP_CLASS_NAME));
1.28 parser 497:
1.1 paf 498: // ^if(condition){code-when-true}
499: // ^if(condition){code-when-true}{code-when-false}
1.10 paf 500: add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1 paf 501:
502: // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59 paf 503: add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1 paf 504:
505: // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10 paf 506: add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1 paf 507:
508: // ^process[code]
1.10 paf 509: add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1 paf 510:
511: // ^rem{code}
1.51 parser 512: add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1 paf 513:
514: // ^while(condition){code}
1.10 paf 515: add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1 paf 516:
517: // ^use[file]
1.10 paf 518: add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1 paf 519:
1.54 paf 520: // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10 paf 521: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 522:
523: // ^eval(expr)
524: // ^eval(expr)[format]
1.10 paf 525: add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52 parser 526:
527: // ^error[msg]
528: add_native_method("error", Method::CT_ANY, _error, 1, 1);
1.32 parser 529:
1.1 paf 530:
531: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 532: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
533:
1.63 paf 534:
1.65 paf 535: // ^cache[file_spec] delete cache
1.63 paf 536: // ^cache[file_spec](time){code} time=0 no cache
1.65 paf 537: add_native_method("cache", Method::CT_ANY, _cache, 1, 3);
1.63 paf 538:
1.28 parser 539: // switch
540:
541: // ^switch[value]{cases}
542: add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
543:
544: // ^case[value]{code}
1.51 parser 545: add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.10 paf 546: }
1.11 paf 547:
548: // constructor & configurator
1.1 paf 549:
1.10 paf 550: Methoded *MOP_create(Pool& pool) {
1.35 parser 551: return new(pool) MOP(pool);
1.11 paf 552: }
553:
554: void MOP::configure_user(Request& r) {
555: Pool& pool=r.pool();
556:
557: // $MAIN:SQL.drivers
558: if(Value *sql=r.main_class->get_element(main_sql_name))
559: if(Value *element=sql->get_element(main_sql_drivers_name))
560: if(Table *protocol2library=element->get_table())
561: r.classes_conf.put(name(), protocol2library);
1.1 paf 562: }
E-mail: