Annotation of parser3/src/classes/op.C, revision 1.68
1.1 paf 1: /** @file
1.9 paf 2: Parser: parser @b operators.
1.1 paf 3:
4: Copyright (c) 2001 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.68 ! paf 7: $Id: op.C,v 1.67 2002/01/16 10:28:33 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.68 ! paf 344: void cache_put(const String& file_spec, const String& data_string) {
1.63 paf 345: void *data; size_t data_size;
346: data_string.serialize(
347: sizeof(Data_string_serialized_prolog),
348: data, data_size);
349: Data_string_serialized_prolog& prolog=
350: *static_cast<Data_string_serialized_prolog *>(data);
351:
352: prolog.version=DATA_STRING_SERIALIZED_VERSION;
353:
1.68 ! paf 354: file_write(
1.63 paf 355: file_spec,
356: data, data_size,
357: false/*as_text*/);
358: }
359: String *cache_get(Pool& pool, const String& file_spec) {
360: void* data; size_t data_size;
361: if(!file_read(pool, file_spec,
362: data, data_size,
363: false/*as_text*/,
364: false/*fail_on_read_problem*/))
365: return 0;
366:
367: Data_string_serialized_prolog& prolog=
368: *static_cast<Data_string_serialized_prolog *>(data);
369:
370: if(data_size<sizeof(Data_string_serialized_prolog))
371: throw Exception(0, 0,
372: &file_spec,
373: "bad cached file, too short");
374:
375: if(prolog.version!=DATA_STRING_SERIALIZED_VERSION)
376: throw Exception(0, 0,
377: &file_spec,
378: "data string version 0x%04X not equal to 0x%04X, recreate file",
379: prolog.version, DATA_STRING_SERIALIZED_VERSION);
380:
381: String& result=*new(pool) String(pool);
1.66 paf 382: result.deserialize(
383: sizeof(Data_string_serialized_prolog),
384: data, data_size, file_spec.cstr());
1.63 paf 385: return &result;
386: }
387: static void _cache(Request& r, const String& method_name, MethodParams *params) {
388: Pool& pool=r.pool();
389:
390: // file_spec, expires, body code
1.65 paf 391: const String &file_spec=r.absolute(params->as_string(0, "filespec must be string"));
392: if(params->size()==1) { // delete
1.68 ! paf 393: cache_delete(file_spec);
1.65 paf 394: return;
395: }
396:
1.63 paf 397: time_t lifespan=(time_t)params->as_double(1, "lifespan must be number", r);
398: Value& body_code=params->as_junction(2, "body must be code");
399:
400: if(lifespan) { // 'lifespan' specified? try cached copy...
401: size_t size;
402: time_t atime, mtime, ctime;
403: // {file_spec} modification time
404: if(!file_stat(file_spec, size, atime, mtime, ctime, false/*no exception on error*/)
405: || (time(0)-mtime) > lifespan) // cached file expired
1.68 ! paf 406: cache_delete(file_spec);
1.63 paf 407: else
408: if(String *cached_body=cache_get(pool, file_spec)) { // have cached copy?
409: // write it out
410: r.write_assign_lang(*cached_body);
411: // happy with it
412: return;
413: }
414: } else // 'lifespan'=0, forget cached copy
1.68 ! paf 415: cache_delete(file_spec);
1.63 paf 416:
417: // process
418: Value& processed_body=r.process(body_code);
419:
420: // put it to cache if 'lifespan' specified
421: if(lifespan)
1.68 ! paf 422: cache_put(file_spec, processed_body.as_string());
1.63 paf 423:
424: // write it out
425: r.write_assign_lang(processed_body);
426: }
427:
1.10 paf 428: // constructor
429:
1.11 paf 430: MOP::MOP(Pool& apool) : Methoded(apool),
431: main_sql_name(apool, MAIN_SQL_NAME),
432: main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
433: {
1.10 paf 434: set_name(*NEW String(pool(), OP_CLASS_NAME));
1.28 parser 435:
1.1 paf 436: // ^if(condition){code-when-true}
437: // ^if(condition){code-when-true}{code-when-false}
1.10 paf 438: add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1 paf 439:
440: // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59 paf 441: add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1 paf 442:
443: // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10 paf 444: add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1 paf 445:
446: // ^process[code]
1.10 paf 447: add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1 paf 448:
449: // ^rem{code}
1.51 parser 450: add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1 paf 451:
452: // ^while(condition){code}
1.10 paf 453: add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1 paf 454:
455: // ^use[file]
1.10 paf 456: add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1 paf 457:
1.54 paf 458: // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10 paf 459: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 460:
461: // ^eval(expr)
462: // ^eval(expr)[format]
1.10 paf 463: add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52 parser 464:
465: // ^error[msg]
466: add_native_method("error", Method::CT_ANY, _error, 1, 1);
1.32 parser 467:
1.1 paf 468:
469: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 470: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
471:
1.63 paf 472:
1.65 paf 473: // ^cache[file_spec] delete cache
1.63 paf 474: // ^cache[file_spec](time){code} time=0 no cache
1.65 paf 475: add_native_method("cache", Method::CT_ANY, _cache, 1, 3);
1.63 paf 476:
1.28 parser 477: // switch
478:
479: // ^switch[value]{cases}
480: add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
481:
482: // ^case[value]{code}
1.51 parser 483: add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.10 paf 484: }
1.11 paf 485:
486: // constructor & configurator
1.1 paf 487:
1.10 paf 488: Methoded *MOP_create(Pool& pool) {
1.35 parser 489: return new(pool) MOP(pool);
1.11 paf 490: }
491:
492: void MOP::configure_user(Request& r) {
493: Pool& pool=r.pool();
494:
495: // $MAIN:SQL.drivers
496: if(Value *sql=r.main_class->get_element(main_sql_name))
497: if(Value *element=sql->get_element(main_sql_drivers_name))
498: if(Table *protocol2library=element->get_table())
499: r.classes_conf.put(name(), protocol2library);
1.1 paf 500: }
E-mail: