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