Annotation of parser3/src/classes/op.C, revision 1.65

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

E-mail: