Annotation of parser3/src/classes/op.C, revision 1.63
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.63 ! paf 7: $Id: op.C,v 1.62.2.1.2.2 2001/12/06 21:34:50 paf Exp $
1.1 paf 8: */
9:
1.13 paf 10: #include "classes.h"
1.1 paf 11: #include "pa_config_includes.h"
12: #include "pa_common.h"
13: #include "pa_request.h"
14: #include "pa_vint.h"
15: #include "pa_sql_connection.h"
16:
1.18 parser 17: // limits
18:
19: #define MAX_LOOPS 10000
20:
1.10 paf 21: // defines
22:
23: #define OP_CLASS_NAME "OP"
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:
44: bool condition=r.process(condition_code,
45: 0/*no name*/,
46: false/*don't intercept string*/).as_bool();
1.6 paf 47: if(condition)
1.34 parser 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.34 parser 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)
63: throw Exception(0, 0,
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.5 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.53 parser 86: throw Exception(0, 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:
94: String result(r.pool());
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.1 paf 103: // calculate pseudo file name of processed chars
104: // would be something like "/some/file(4) process"
105: char place[MAX_STRING];
106: #ifndef NO_STRING_ORIGIN
107: const Origin& origin=method_name.origin();
108: snprintf(place, MAX_STRING, "%s(%d) %s",
109: origin.file, 1+origin.line,
110: method_name.cstr());
111: #else
1.39 parser 112: strncpy(place, method_name.cstr(), MAX_STRING-1); place[MAX_STRING-1]=0;
1.1 paf 113: #endif
114:
115: VStateless_class& self_class=*r.self->get_class();
116: {
117: // temporary zero @main so to maybe-replace it in processed code
118: Temp_method temp_method_main(self_class, *main_method_name, 0);
119: // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
120: Temp_method temp_method_auto(self_class, *auto_method_name, 0);
121:
122: // evaluate source to process
123: const String& source=
1.61 paf 124: r.process(params->as_junction(0, "body must be code")).as_string();
1.1 paf 125:
126: // process source code, append processed methods to 'self' class
127: // maybe-define new @main
1.62 paf 128: r.use_buf(source.cstr(String::UL_UNSPECIFIED, r.connection), place, &self_class);
1.1 paf 129:
130: // maybe-execute @main[]
131: if(const Method *method=self_class.get_method(*main_method_name)) {
132: // execute!
133: r.execute(*method->parser_code);
134: }
135: }
136: }
137:
1.5 paf 138: static void _rem(Request& r, const String&, MethodParams *params) {
1.34 parser 139: params->as_junction(0, "body must be code");
1.1 paf 140: }
141:
1.5 paf 142: static void _while(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 143: Pool& pool=r.pool();
144:
1.34 parser 145: Value& vcondition=params->as_junction(0, "condition must be expression");
146: Value& body=params->as_junction(1, "body must be code");
1.1 paf 147:
148: // while...
149: int endless_loop_count=0;
150: while(true) {
1.18 parser 151: if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.53 parser 152: throw Exception(0, 0,
1.1 paf 153: &method_name,
154: "endless loop detected");
155:
156: bool condition=
157: r.process(
158: vcondition,
159: 0/*no name*/,
160: false/*don't intercept string*/).as_bool();
161: if(!condition) // ...condition is true
162: break;
163:
164: // write processed body
165: r.write_pass_lang(r.process(body));
166: }
167: }
168:
1.5 paf 169: static void _use(Request& r, const String& method_name, MethodParams *params) {
1.34 parser 170: Value& vfile=params->as_no_junction(0, "file name must not be code");
1.37 parser 171: r.use_file(vfile.as_string());
1.1 paf 172: }
173:
1.5 paf 174: static void _for(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 175: Pool& pool=r.pool();
1.48 parser 176: const String& var_name=params->as_string(0, "var name must be string");
177: int from=params->as_int(1, "from must be int", r);
178: int to=params->as_int(2, "to must be int", r);
1.34 parser 179: Value& body_code=params->as_junction(3, "body must be code");
1.50 parser 180: Value *delim_maybe_code=params->size()>4?¶ms->get(4):0;
1.1 paf 181:
1.57 paf 182: if(to-from>=MAX_LOOPS) // too long loop?
183: throw Exception(0, 0,
184: &method_name,
185: "endless loop detected");
186:
1.1 paf 187: bool need_delim=false;
188: VInt *vint=new(pool) VInt(pool, 0);
189: for(int i=from; i<=to; i++) {
190: vint->set_int(i);
1.56 paf 191: r.root->put_element(var_name, vint);
1.1 paf 192:
193: Value& processed_body=r.process(body_code);
1.49 parser 194: if(delim_maybe_code) { // delimiter set?
1.1 paf 195: const String *string=processed_body.get_string();
196: if(need_delim && string && string->size()) // need delim & iteration produced string?
1.49 parser 197: r.write_pass_lang(r.process(*delim_maybe_code));
1.1 paf 198: need_delim=true;
199: }
200: r.write_pass_lang(processed_body);
201: }
202: }
203:
1.15 paf 204: static void _eval(Request& r, const String& method_name, MethodParams *params) {
1.34 parser 205: Value& expr=params->as_junction(0, "need expression");
1.1 paf 206: // evaluate expresion
207: Value *result=r.process(expr,
1.16 paf 208: 0/*no name YET*/,
1.1 paf 209: true/*don't intercept string*/).as_expr_result();
1.50 parser 210: if(params->size()>1) {
1.34 parser 211: Value& fmt=params->as_no_junction(1, "fmt must not be code");
1.1 paf 212:
213: Pool& pool=r.pool();
214: String& string=*new(pool) String(pool);
215: string.APPEND_CONST(format(pool, result->as_double(), fmt.as_string().cstr()));
216: result=new(pool) VString(string);
217: }
1.16 paf 218: result->set_name(method_name);
1.1 paf 219: r.write_no_lang(*result);
220: }
221:
1.52 parser 222: static void _error(Request& r, const String& method_name, MethodParams *params) {
223: Pool& pool=r.pool();
224:
225: const String& serror=params->as_string(0, "message must be string");
1.53 parser 226: throw Exception(0, 0,
1.52 parser 227: &method_name,
228: "%s", serror.cstr());
229: }
230:
1.1 paf 231:
1.53 parser 232: /// @todo rewrite ugly code with try/try to autoobject TempConnection
1.42 parser 233: static void _connect(Request& r, const String& method_name, MethodParams *params) {
1.1 paf 234: Pool& pool=r.pool();
1.63 ! paf 235: #ifdef RESOURCES_DEBUG
! 236: struct timeval mt[2];
! 237: #endif
1.34 parser 238: Value& url=params->as_no_junction(0, "url must not be code");
239: Value& body_code=params->as_junction(1, "body must be code");
1.1 paf 240:
1.19 parser 241: Table *protocol2driver_and_client=
1.35 parser 242: static_cast<Table *>(r.classes_conf.get(r.OP.name()));
1.11 paf 243:
1.63 ! paf 244: #ifdef RESOURCES_DEBUG
! 245: //measure:before
! 246: gettimeofday(&mt[0],NULL);
! 247: #endif
1.1 paf 248: // connect
249: SQL_Connection& connection=SQL_driver_manager->get_connection(
1.42 parser 250: url.as_string(), method_name, protocol2driver_and_client);
1.1 paf 251:
1.63 ! paf 252: #ifdef RESOURCES_DEBUG
! 253: //measure:after connect
! 254: gettimeofday(&mt[1],NULL);
! 255:
! 256: double t[2];
! 257: for(int i=0;i<2;i++)
! 258: t[i]=mt[i].tv_sec+mt[i].tv_usec/1000000.0;
! 259:
! 260: r.sql_connect_time+=t[1]-t[0];
! 261: #endif
1.1 paf 262: // remember/set current connection
263: SQL_Connection *saved_connection=r.connection;
264: r.connection=&connection;
265: // execute body
1.53 parser 266: try {
267: try {
268: r.write_assign_lang(r.process(body_code));
269:
270: connection.commit();
271: } catch(...) { // process/commit problem
272: connection.rollback();
273:
274: /*re*/throw;
275: }
276:
277: } catch(...) {
278: // close connection [cache it]
279: connection.close();
280: // recall current connection from remembered
281: r.connection=saved_connection;
1.1 paf 282:
1.53 parser 283: /*re*/throw;
1.1 paf 284: }
285:
1.53 parser 286: // and anyway
1.1 paf 287: // close connection [cache it]
1.21 parser 288: connection.close();
1.1 paf 289: // recall current connection from remembered
290: r.connection=saved_connection;
291: }
292:
1.41 parser 293: #ifndef DOXYGEN
1.28 parser 294: struct Switch_data {
295: Value *searching;
296: Value *found;
297: Value *_default;
298: };
1.41 parser 299: #endif
1.28 parser 300: static void _switch(Request& r, const String&, MethodParams *params) {
301: void *backup=r.classes_conf.get(*switch_data_name);
302: Switch_data data={&r.process(params->get(0))};
303: r.classes_conf.put(*switch_data_name, &data);
304:
1.34 parser 305: r.process(params->as_junction(1, "switch cases must be code")); // and ignore result
1.28 parser 306:
307: r.classes_conf.put(*switch_data_name, backup);
308:
309: if(Value *code=data.found ? data.found : data._default)
310: r.write_pass_lang(r.process(*code));
311: }
312:
1.38 parser 313: static void _case(Request& r, const String& method_name, MethodParams *params) {
314: Pool& pool=r.pool();
315:
316: Switch_data *data=static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
317: if(!data)
1.53 parser 318: throw Exception(0, 0,
1.38 parser 319: &method_name,
320: "without switch");
1.28 parser 321:
322: int count=params->size();
1.34 parser 323: Value *code=¶ms->as_junction(--count, "case result must be code");
1.28 parser 324: for(int i=0; i<count; i++) {
325: Value& value=r.process(params->get(i));
326:
1.36 parser 327: if(value.as_string() == *case_default_value) {
1.38 parser 328: data->_default=code;
1.28 parser 329: break;
330: }
331:
332: bool matches;
1.38 parser 333: if(data->searching->is_string())
334: matches=data->searching->as_string() == value.as_string();
1.28 parser 335: else
1.38 parser 336: matches=data->searching->as_double() == value.as_double();
1.28 parser 337:
338: if(matches) {
1.38 parser 339: data->found=code;
1.28 parser 340: break;
341: }
342: }
343: }
344:
1.63 ! paf 345: // cache--
! 346:
! 347: // consts
! 348:
! 349: const int DATA_STRING_SERIALIZED_VERSION=0x0001;
! 350:
! 351: // helper types
! 352:
! 353: #ifndef DOXYGEN
! 354: struct Data_string_serialized_prolog {
! 355: int version;
! 356: };
! 357: #endif
! 358:
! 359: void cache_delete(Pool& pool, const String& file_spec) {
! 360: file_delete(pool, file_spec, false/*fail_on_read_problem*/);
! 361: }
! 362: void cache_put(Pool& pool, const String& file_spec, const String& data_string) {
! 363: void *data; size_t data_size;
! 364: data_string.serialize(
! 365: sizeof(Data_string_serialized_prolog),
! 366: data, data_size);
! 367: Data_string_serialized_prolog& prolog=
! 368: *static_cast<Data_string_serialized_prolog *>(data);
! 369:
! 370: prolog.version=DATA_STRING_SERIALIZED_VERSION;
! 371:
! 372: file_write(pool,
! 373: file_spec,
! 374: data, data_size,
! 375: false/*as_text*/);
! 376: }
! 377: String *cache_get(Pool& pool, const String& file_spec) {
! 378: void* data; size_t data_size;
! 379: if(!file_read(pool, file_spec,
! 380: data, data_size,
! 381: false/*as_text*/,
! 382: false/*fail_on_read_problem*/))
! 383: return 0;
! 384:
! 385: Data_string_serialized_prolog& prolog=
! 386: *static_cast<Data_string_serialized_prolog *>(data);
! 387:
! 388: if(data_size<sizeof(Data_string_serialized_prolog))
! 389: throw Exception(0, 0,
! 390: &file_spec,
! 391: "bad cached file, too short");
! 392:
! 393: if(prolog.version!=DATA_STRING_SERIALIZED_VERSION)
! 394: throw Exception(0, 0,
! 395: &file_spec,
! 396: "data string version 0x%04X not equal to 0x%04X, recreate file",
! 397: prolog.version, DATA_STRING_SERIALIZED_VERSION);
! 398:
! 399: String& result=*new(pool) String(pool);
! 400: if(data_size) {
! 401: result.deserialize(
! 402: sizeof(Data_string_serialized_prolog),
! 403: data, data_size, file_spec.cstr());
! 404: }
! 405:
! 406: return &result;
! 407: }
! 408: static void _cache(Request& r, const String& method_name, MethodParams *params) {
! 409: // ^cache[file_spec](lifespan){code} time=0 no cache
! 410: Pool& pool=r.pool();
! 411:
! 412: // file_spec, expires, body code
! 413: const String &file_spec=params->as_string(0, "key must be string");
! 414: time_t lifespan=(time_t)params->as_double(1, "lifespan must be number", r);
! 415: Value& body_code=params->as_junction(2, "body must be code");
! 416:
! 417: if(lifespan) { // 'lifespan' specified? try cached copy...
! 418: size_t size;
! 419: time_t atime, mtime, ctime;
! 420: // {file_spec} modification time
! 421: if(!file_stat(file_spec, size, atime, mtime, ctime, false/*no exception on error*/)
! 422: || (time(0)-mtime) > lifespan) // cached file expired
! 423: cache_delete(pool, file_spec);
! 424: else
! 425: if(String *cached_body=cache_get(pool, file_spec)) { // have cached copy?
! 426: // write it out
! 427: r.write_assign_lang(*cached_body);
! 428: // happy with it
! 429: return;
! 430: }
! 431: } else // 'lifespan'=0, forget cached copy
! 432: cache_delete(pool, file_spec);
! 433:
! 434: // process
! 435: Value& processed_body=r.process(body_code);
! 436:
! 437: // put it to cache if 'lifespan' specified
! 438: if(lifespan)
! 439: cache_put(pool, file_spec, processed_body.as_string());
! 440:
! 441: // write it out
! 442: r.write_assign_lang(processed_body);
! 443: }
! 444:
1.10 paf 445: // constructor
446:
1.11 paf 447: MOP::MOP(Pool& apool) : Methoded(apool),
448: main_sql_name(apool, MAIN_SQL_NAME),
449: main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
450: {
1.10 paf 451: set_name(*NEW String(pool(), OP_CLASS_NAME));
1.28 parser 452:
1.1 paf 453: // ^if(condition){code-when-true}
454: // ^if(condition){code-when-true}{code-when-false}
1.10 paf 455: add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1 paf 456:
457: // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59 paf 458: add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1 paf 459:
460: // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10 paf 461: add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1 paf 462:
463: // ^process[code]
1.10 paf 464: add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1 paf 465:
466: // ^rem{code}
1.51 parser 467: add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1 paf 468:
469: // ^while(condition){code}
1.10 paf 470: add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1 paf 471:
472: // ^use[file]
1.10 paf 473: add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1 paf 474:
1.54 paf 475: // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10 paf 476: add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1 paf 477:
478: // ^eval(expr)
479: // ^eval(expr)[format]
1.10 paf 480: add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52 parser 481:
482: // ^error[msg]
483: add_native_method("error", Method::CT_ANY, _error, 1, 1);
1.32 parser 484:
1.1 paf 485:
486: // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10 paf 487: add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
488:
1.63 ! paf 489:
! 490: // ^cache[file_spec](time){code} time=0 no cache
! 491: add_native_method("cache", Method::CT_ANY, _cache, 3, 3);
! 492:
1.28 parser 493: // switch
494:
495: // ^switch[value]{cases}
496: add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
497:
498: // ^case[value]{code}
1.51 parser 499: add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.10 paf 500: }
1.11 paf 501:
502: // constructor & configurator
1.1 paf 503:
1.10 paf 504: Methoded *MOP_create(Pool& pool) {
1.35 parser 505: return new(pool) MOP(pool);
1.11 paf 506: }
507:
508: void MOP::configure_user(Request& r) {
509: Pool& pool=r.pool();
510:
511: // $MAIN:SQL.drivers
512: if(Value *sql=r.main_class->get_element(main_sql_name))
513: if(Value *element=sql->get_element(main_sql_drivers_name))
514: if(Table *protocol2library=element->get_table())
515: r.classes_conf.put(name(), protocol2library);
1.1 paf 516: }
E-mail: