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

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.83    ! paf         7:        $Id: op.C,v 1.82 2002/04/15 06:45:55 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"
1.79      paf        15: #include "pa_vdate.h"
1.1       paf        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.82      paf        24: #define CASE_DEFAULT_VALUE "DEFAULT"
1.11      paf        25: 
1.10      paf        26: // class
                     27: 
                     28: class MOP : public Methoded {
                     29: public:
                     30:        MOP(Pool& pool);
1.11      paf        31: public: // Methoded
1.10      paf        32:        bool used_directly() { return true; }
1.11      paf        33:        void configure_user(Request& r);
1.36      parser     34: 
1.11      paf        35: private:
                     36:        String main_sql_name;
                     37:        String main_sql_drivers_name;
1.10      paf        38: };
                     39: 
                     40: // methods
                     41: 
1.5       paf        42: static void _if(Request& r, const String&, MethodParams *params) {
1.48      parser     43:        Value& condition_code=params->as_junction(0, "condition must be expression");
1.1       paf        44: 
1.81      paf        45:        bool condition=r.process_to_value(condition_code, 
1.83    ! paf        46:                /*0/*no name* /,*/
1.1       paf        47:                false/*don't intercept string*/).as_bool();
1.6       paf        48:        if(condition)
1.81      paf        49:                r.write_pass_lang(r.process_to_value(params->as_junction(1, "'then' parameter must be code")));
1.50      parser     50:        else if(params->size()>2)
1.81      paf        51:                r.write_pass_lang(r.process_to_value(params->as_junction(2, "'else' parameter must be code")));
1.1       paf        52: }
                     53: 
1.5       paf        54: static void _untaint(Request& r, const String& method_name, MethodParams *params) {
1.1       paf        55:        Pool& pool=r.pool();
                     56: 
1.60      paf        57:        uchar lang;
1.59      paf        58:        if(params->size()==1)
                     59:                lang=String::UL_AS_IS; // mark as simply 'tainted'. useful in html from sql 
                     60:        else {
                     61:                const String& lang_name=params->as_string(0, "lang must be string");
1.60      paf        62:                lang=untaint_lang_name2enum->get_int(lang_name);
1.59      paf        63:                if(!lang)
1.78      paf        64:                        throw Exception(0,
1.59      paf        65:                                &lang_name,
                     66:                                "invalid taint language");
                     67:        }
1.1       paf        68: 
                     69:        {
1.59      paf        70:                Value& vbody=params->as_junction(params->size()-1, "body must be code");
1.1       paf        71:                
                     72:                Temp_lang temp_lang(r, lang); // set temporarily specified ^untaint[language;
1.81      paf        73:                r.write_pass_lang(r.process_to_value(vbody)); // process marking tainted with that lang
1.1       paf        74:        }
                     75: }
                     76: 
1.5       paf        77: static void _taint(Request& r, const String&, MethodParams *params) {
1.1       paf        78:        Pool& pool=r.pool();
                     79: 
1.60      paf        80:        uchar lang;
1.1       paf        81:        if(params->size()==1)
                     82:                lang=String::UL_TAINTED; // mark as simply 'tainted'. useful in table:set
                     83:        else {
1.48      parser     84:                const String& lang_name=params->as_string(0, "lang must be string");
1.60      paf        85:                lang=untaint_lang_name2enum->get_int(lang_name);
1.1       paf        86:                if(!lang)
1.78      paf        87:                        throw Exception(0,
1.3       paf        88:                                &lang_name,
                     89:                                "invalid taint language");
1.1       paf        90:        }
                     91: 
                     92:        {
1.34      parser     93:                Value& vbody=params->as_no_junction(params->size()-1, "body must not be code");
1.1       paf        94:                
                     95:                String result(r.pool());
                     96:                result.append(
                     97:                        vbody.as_string(),  // process marking tainted with that lang
                     98:                        lang, true);  // force result language to specified
                     99:                r.write_pass_lang(result);
                    100:        }
                    101: }
                    102: 
1.5       paf       103: static void _process(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       104:        // calculate pseudo file name of processed chars
                    105:        // would be something like "/some/file(4) process"
                    106:        char place[MAX_STRING];
                    107: #ifndef NO_STRING_ORIGIN
                    108:        const Origin& origin=method_name.origin();
                    109:        snprintf(place, MAX_STRING, "%s(%d) %s", 
                    110:                origin.file, 1+origin.line,
                    111:                method_name.cstr());
                    112: #else
1.39      parser    113:        strncpy(place, method_name.cstr(), MAX_STRING-1); place[MAX_STRING-1]=0;
1.1       paf       114: #endif 
                    115: 
                    116:        VStateless_class& self_class=*r.self->get_class();
1.73      paf       117:        const Method *main_method;
1.1       paf       118:        {
1.73      paf       119:                // temporary remove language change
                    120:                Temp_lang temp_lang(r, String::UL_PASS_APPENDED);
1.1       paf       121:                // temporary zero @main so to maybe-replace it in processed code
                    122:                Temp_method temp_method_main(self_class, *main_method_name, 0);
                    123:                // temporary zero @auto so it wouldn't be auto-called in Request::use_buf
                    124:                Temp_method temp_method_auto(self_class, *auto_method_name, 0);
                    125:                
                    126:                // evaluate source to process
                    127:                const String& source=
1.81      paf       128:                        r.process_to_string(params->as_junction(0, "body must be code"));
1.1       paf       129: 
                    130:                // process source code, append processed methods to 'self' class
                    131:                // maybe-define new @main
1.67      paf       132:                r.use_buf(
                    133:                        source.cstr(String::UL_UNSPECIFIED, r.connection(0)), 
                    134:                        place, 
                    135:                        &self_class);
1.1       paf       136:                
1.73      paf       137:                // main_method
                    138:                main_method=self_class.get_method(*main_method_name);
                    139:        }
                    140:        // after restoring current-request-lang
                    141:        // maybe-execute @main[]
                    142:        if(main_method) {
                    143:                // execute!     
                    144:                r.execute(*main_method->parser_code);
1.1       paf       145:        }
                    146: }
                    147:        
1.5       paf       148: static void _rem(Request& r, const String&, MethodParams *params) {
1.34      parser    149:        params->as_junction(0, "body must be code");
1.1       paf       150: }
                    151: 
1.5       paf       152: static void _while(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       153:        Pool& pool=r.pool();
                    154: 
1.34      parser    155:        Value& vcondition=params->as_junction(0, "condition must be expression");
                    156:        Value& body=params->as_junction(1, "body must be code");
1.1       paf       157: 
                    158:        // while...
                    159:        int endless_loop_count=0;
                    160:        while(true) {
1.18      parser    161:                if(++endless_loop_count>=MAX_LOOPS) // endless loop?
1.78      paf       162:                        throw Exception("parser.runtime",
1.1       paf       163:                                &method_name,
                    164:                                "endless loop detected");
                    165: 
                    166:                bool condition=
1.81      paf       167:                        r.process_to_value(
1.1       paf       168:                                vcondition, 
1.83    ! paf       169:                                /*0/*no name* /,*/
1.1       paf       170:                                false/*don't intercept string*/).as_bool();
                    171:                if(!condition) // ...condition is true
                    172:                        break;
                    173: 
                    174:                // write processed body
1.81      paf       175:                r.write_pass_lang(r.process_to_value(body));
1.1       paf       176:        }
                    177: }
                    178: 
1.5       paf       179: static void _use(Request& r, const String& method_name, MethodParams *params) {
1.34      parser    180:        Value& vfile=params->as_no_junction(0, "file name must not be code");
1.37      parser    181:        r.use_file(vfile.as_string());
1.1       paf       182: }
                    183: 
1.5       paf       184: static void _for(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       185:        Pool& pool=r.pool();
1.48      parser    186:        const String& var_name=params->as_string(0, "var name must be string");
                    187:        int from=params->as_int(1, "from must be int", r);
                    188:        int to=params->as_int(2, "to must be int", r);
1.34      parser    189:        Value& body_code=params->as_junction(3, "body must be code");
1.50      parser    190:        Value *delim_maybe_code=params->size()>4?&params->get(4):0;
1.1       paf       191: 
1.57      paf       192:        if(to-from>=MAX_LOOPS) // too long loop?
1.78      paf       193:                throw Exception("parser.runtime",
1.57      paf       194:                        &method_name,
                    195:                        "endless loop detected");
                    196: 
1.1       paf       197:        bool need_delim=false;
                    198:        VInt *vint=new(pool) VInt(pool, 0);
                    199:        for(int i=from; i<=to; i++) {
                    200:                vint->set_int(i);
1.56      paf       201:                r.root->put_element(var_name, vint);
1.1       paf       202: 
1.81      paf       203:                Value& processed_body=r.process_to_value(body_code);
1.49      parser    204:                if(delim_maybe_code) { // delimiter set?
1.1       paf       205:                        const String *string=processed_body.get_string();
                    206:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
1.81      paf       207:                                r.write_pass_lang(r.process_to_string(*delim_maybe_code));
1.1       paf       208:                        need_delim=true;
                    209:                }
                    210:                r.write_pass_lang(processed_body);
                    211:        }
                    212: }
                    213: 
1.15      paf       214: static void _eval(Request& r, const String& method_name, MethodParams *params) {
1.34      parser    215:        Value& expr=params->as_junction(0, "need expression");
1.1       paf       216:        // evaluate expresion
1.81      paf       217:        Value *result=r.process_to_value(expr, 
1.83    ! paf       218:                /*0/*no name YET* /,*/
1.1       paf       219:                true/*don't intercept string*/).as_expr_result();
1.50      parser    220:        if(params->size()>1) {
1.34      parser    221:                Value& fmt=params->as_no_junction(1, "fmt must not be code");
1.1       paf       222: 
                    223:                Pool& pool=r.pool();
                    224:                String& string=*new(pool) String(pool);
                    225:                string.APPEND_CONST(format(pool, result->as_double(), fmt.as_string().cstr()));
                    226:                result=new(pool) VString(string);
                    227:        }
1.16      paf       228:        result->set_name(method_name);
1.1       paf       229:        r.write_no_lang(*result);
                    230: }
                    231: 
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
1.67      paf       248:        SQL_Connection_ptr 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.67      paf       261:        Temp_connection temp_connection(r, connection.get());
1.1       paf       262:        // execute body
1.53      parser    263:        try {
1.81      paf       264:                r.write_assign_lang(r.process_to_value(body_code));
1.75      paf       265:        } catch(...) { // process problem
1.67      paf       266:                connection->mark_to_rollback();
                    267:                /*re*/throw; 
1.1       paf       268:        }
                    269: }
                    270: 
1.41      parser    271: #ifndef DOXYGEN
1.28      parser    272: struct Switch_data {
1.82      paf       273:        Request *r;
1.28      parser    274:        Value *searching;
                    275:        Value *found;
                    276:        Value *_default;
                    277: };
1.41      parser    278: #endif
1.28      parser    279: static void _switch(Request& r, const String&, MethodParams *params) {
1.82      paf       280:        Switch_data data={&r, &r.process_to_value(params->get(0))};     
1.79      paf       281:        Temp_hash_value switch_data_setter(r.classes_conf, *switch_data_name, &data);
1.28      parser    282: 
1.83    ! paf       283:        Value& cases_code=params->as_junction(1, "switch cases must be code");
1.82      paf       284:        // execution of found ^case[...]{code} must be in context of ^switch[...]{code}
                    285:        // because of stacked WWrapper used there as wcontext
1.83    ! paf       286:        r.process(
        !           287:                cases_code,
        !           288:                true/*intercept_string*/
1.82      paf       289:        );
1.83    ! paf       290:        if(Value *selected_code=data.found ? data.found : data._default) {
        !           291:                // setting code context, would execute in ^switch[...]{>>context<<}
        !           292:                selected_code->get_junction()->change_context(cases_code.get_junction());
        !           293:                r.write_pass_lang(r.process_to_value(*selected_code));
        !           294:        }
1.28      parser    295: }
                    296: 
1.38      parser    297: static void _case(Request& r, const String& method_name, MethodParams *params) {
                    298:        Pool& pool=r.pool();
                    299: 
                    300:        Switch_data *data=static_cast<Switch_data *>(r.classes_conf.get(*switch_data_name));
                    301:        if(!data)
1.78      paf       302:                throw Exception("parser.runtime",
1.38      parser    303:                        &method_name,
                    304:                        "without switch");
1.28      parser    305: 
                    306:        int count=params->size();
1.34      parser    307:        Value *code=&params->as_junction(--count, "case result must be code");
1.83    ! paf       308:        
        !           309:        // killing context for safety, would execute in ^switch[...]{>>context<<}
        !           310:        // reason: context is stacked, and it would become invalid afterwards
        !           311:        code->get_junction()->change_context(0);
        !           312: 
1.28      parser    313:        for(int i=0; i<count; i++) {
1.81      paf       314:                Value& value=r.process_to_value(params->get(i));
1.28      parser    315: 
1.82      paf       316:                if(value.as_string() == CASE_DEFAULT_VALUE) {
1.38      parser    317:                        data->_default=code;
1.28      parser    318:                        break;
                    319:                }
                    320: 
                    321:                bool matches;
1.38      parser    322:                if(data->searching->is_string())
                    323:                        matches=data->searching->as_string() == value.as_string();
1.28      parser    324:                else
1.38      parser    325:                        matches=data->searching->as_double() == value.as_double();
1.28      parser    326: 
                    327:                if(matches) {
1.82      paf       328:                        if(data->found)
                    329:                                throw Exception("parser.runtime",
                    330:                                        &method_name,
                    331:                                        "duplicate found");
                    332: 
1.38      parser    333:                        data->found=code;
1.28      parser    334:                        break;
                    335:                }
                    336:        }
                    337: }
                    338: 
1.63      paf       339: // cache--
                    340: 
                    341: // consts
                    342: 
1.79      paf       343: const int DATA_STRING_SERIALIZED_VERSION=0x0002;
1.63      paf       344: 
                    345: // helper types
                    346: 
                    347: #ifndef DOXYGEN
                    348: struct Data_string_serialized_prolog {
                    349:        int version;
1.79      paf       350:        time_t expires;
1.63      paf       351: };
                    352: #endif
                    353: 
1.68      paf       354: void cache_delete(const String& file_spec) {
                    355:        file_delete(file_spec, false/*fail_on_read_problem*/);
1.63      paf       356: }
1.69      paf       357: 
                    358: #ifndef DOXYGEN
1.79      paf       359: struct Cache_data {
                    360:        time_t expires;
                    361: };
1.69      paf       362: struct Locked_process_and_cache_put_action_info {
                    363:        Request *r;
1.79      paf       364:        Cache_data *data;
1.81      paf       365:        Value *body_code; const String *evaluated_body;
1.69      paf       366: };
                    367: #endif
                    368: static void locked_process_and_cache_put_action(int f, void *context) {
                    369:        Locked_process_and_cache_put_action_info& info=
                    370:                *static_cast<Locked_process_and_cache_put_action_info *>(context);
                    371:        
                    372:        // body->process 
1.81      paf       373:        info.evaluated_body=&info.r->process_to_string(*info.body_code);
1.69      paf       374: 
1.79      paf       375:        // expiration time not spoiled by ^cache(0) or something?
                    376:        if(info.data->expires > time(0)) {
                    377:                // string -serialize> buffer
                    378:                void *data; size_t data_size;
1.81      paf       379:                info.evaluated_body->serialize(
1.79      paf       380:                        sizeof(Data_string_serialized_prolog), 
                    381:                        data, data_size);
                    382:                Data_string_serialized_prolog& prolog=
                    383:                        *static_cast<Data_string_serialized_prolog *>(data);
                    384:                prolog.version=DATA_STRING_SERIALIZED_VERSION;
                    385:                prolog.expires=info.data->expires;
                    386:                
                    387:                // buffer -write> file
                    388:                write(f, data, data_size);
                    389:        } else // expired!
                    390:                info.data->expires=0; // flag it so that could be easily checked by caller
1.69      paf       391: }
1.81      paf       392: const String *locked_process_and_cache_put(Request& r, 
1.69      paf       393:                                                                        Value& body_code,
1.79      paf       394:                                                                        Cache_data& data,
1.69      paf       395:                                                                        const String& file_spec) {
                    396:        Locked_process_and_cache_put_action_info info={
                    397:                &r,
1.81      paf       398:                &data,
                    399:                &body_code
1.69      paf       400:        };
                    401: 
1.81      paf       402:        const String *result=file_write_action_under_lock(
1.69      paf       403:                file_spec, 
                    404:                "cache_put", locked_process_and_cache_put_action, &info,
                    405:                false/*as_text*/,
                    406:                false/*do_append*/,
1.81      paf       407:                false/*block*/) ? info.evaluated_body: 0;
1.79      paf       408:        if(data.expires==0)
                    409:                cache_delete(file_spec);
                    410:        return result;
1.63      paf       411: }
1.79      paf       412: String *cache_get(Pool& pool, const String& file_spec, time_t now) {
1.63      paf       413:        void* data; size_t data_size;
1.72      paf       414:        if(file_read(pool, file_spec, 
1.63      paf       415:                           data, data_size, 
                    416:                           false/*as_text*/, 
1.69      paf       417:                           false/*fail_on_read_problem*/)
1.72      paf       418:            && data_size/* ignore reads which are empty due to 
                    419:                        non-unary open+lockEX conflict with lockSH */) {
1.63      paf       420:        
1.72      paf       421:                Data_string_serialized_prolog& prolog=
                    422:                        *static_cast<Data_string_serialized_prolog *>(data);
1.63      paf       423: 
1.72      paf       424:                String *result=new(pool) String(pool);
                    425:                if(
                    426:                        data_size>=sizeof(Data_string_serialized_prolog)
                    427:                        && prolog.version==DATA_STRING_SERIALIZED_VERSION
1.79      paf       428:                        && prolog.expires > now
1.72      paf       429:                        && result->deserialize(
                    430:                                sizeof(Data_string_serialized_prolog),  data, data_size, file_spec.cstr()))
                    431:                        return result;
                    432:        }
1.63      paf       433: 
1.72      paf       434:        return 0;
1.63      paf       435: }
1.79      paf       436: static time_t as_expires(Request& r, const String& method_name, MethodParams *params, 
                    437:                                                int index, time_t now) {
                    438:        time_t result;
                    439:        Value& vlifespan_or_expires=params->get(index);
                    440:        if(strcmp(vlifespan_or_expires.type(), VDATE_TYPE)==0)
                    441:                result=static_cast<VDate&>(vlifespan_or_expires).get_time();
                    442:        else
                    443:                result=now+(time_t)params->as_double(index, "lifespan must be date or number", r);
                    444:        
                    445:        return result;
                    446: }
                    447: static const String as_file_spec(Request&r, MethodParams *params, int index) {
                    448:        return r.absolute(params->as_string(index, "filespec must be string"));
                    449: }
1.63      paf       450: static void _cache(Request& r, const String& method_name, MethodParams *params) {
                    451:        Pool& pool=r.pool();
1.79      paf       452:        time_t now=time(0);
                    453: 
                    454:        // ^cache[filename] ^cache(seconds) ^cache[expires date]
                    455:        if(params->size()==1) {
                    456:                if(params->get(0).is_string()) { // filename?
                    457:                        cache_delete(as_file_spec(r, params, 0));
                    458:                        return;
                    459:                }
                    460: 
                    461:                // secods|expires date
                    462:                Cache_data *data=static_cast<Cache_data *>(r.classes_conf.get(*cache_data_name));
                    463:                if(!data)
                    464:                        throw Exception("parser.runtime",
                    465:                                &method_name,
                    466:                                "expire-time reducing instruction without cache");
                    467:                
                    468:                time_t expires=as_expires(r, method_name, params, 0, now);
                    469:                if(expires < data->expires)
                    470:                        data->expires=expires;
                    471: 
                    472:                return;
                    473:        }
1.63      paf       474:        
                    475:        // file_spec, expires, body code
1.65      paf       476:        const String &file_spec=r.absolute(params->as_string(0, "filespec must be string"));
                    477: 
1.79      paf       478:        Cache_data data;
                    479:        Temp_hash_value cache_data_setter(r.classes_conf, *cache_data_name, &data);
                    480:        data.expires=as_expires(r, method_name, params, 1, now);
1.63      paf       481:        Value& body_code=params->as_junction(2, "body must be code");
                    482: 
1.79      paf       483:        if(data.expires>now) { // valid 'expires' specified? try cached copy...
1.69      paf       484:                // hence we don't hope to have unary create/lockEX
                    485:                // we need some plan to live in a life like that, so... 
                    486:                // worst races plan:
                    487:                // A        B
                    488:                // open
                    489:                //          |open
                    490:                // lockSH
                    491:                //          |nonblocking-lockEX fails
                    492:                // unlockSH
                    493:                // close, cache_get returns 0
                    494:                // open
                    495:                // nonblocking-lockEX succeeds; process, write, close
                    496:                //          |retry1: open
                    497:                // ...
                    498:                //          |lockSH succeeds; ...
                    499: 
                    500:                for(int retry=0; retry<2; retry++) {
1.79      paf       501:                        if(String *cached_body=cache_get(pool, file_spec, now)) { // have cached copy?
                    502:                                // write it out 
                    503:                                r.write_assign_lang(*cached_body);
                    504:                                // happy with it
                    505:                                return;
                    506:                        }
1.69      paf       507: 
                    508:                        // non-blocked lock; process; cache it
1.81      paf       509:                        if(const String*processed_body=
                    510:                                locked_process_and_cache_put(r, body_code, data, file_spec)) {
1.63      paf       511:                                // write it out 
1.69      paf       512:                                r.write_assign_lang(*processed_body);
1.63      paf       513:                                // happy with it
                    514:                                return;
1.69      paf       515:                        } else { // somebody writing result right now
                    516:                                pa_sleep(0, 500000); // waiting half a second
                    517:                                retry=0; // prolonging our wait, than could cache_get it, without processing body_code
1.63      paf       518:                        }
1.69      paf       519:                }
1.78      paf       520:                throw Exception(0,
1.69      paf       521:                        &file_spec,
                    522:                        "locking problem");
                    523:        } else { 
1.79      paf       524:                // instructed not to cache; forget cached copy
1.68      paf       525:                cache_delete(file_spec);
1.69      paf       526:                // process
1.81      paf       527:                const String& processed_body=r.process_to_string(body_code);
1.69      paf       528:                // write it out 
                    529:                r.write_assign_lang(processed_body);
                    530:                // happy with it
                    531:                return;
                    532:        }
                    533:        // never reached
1.63      paf       534: }
                    535: 
1.74      paf       536: // also used in pa_request.C to pass param to @unhandled_exception
                    537: VHash& exception2vhash(Pool& pool, const Exception& e) {
                    538:        VHash& result=*new(pool) VHash(pool);
                    539:        Hash& hash=result.hash(0);
1.78      paf       540:        if(const char *type=e.type())
                    541:                hash.put(*exception_type_part_name, new(pool) VString(*new(pool) String(pool, type)));
1.76      paf       542:        if(const String *asource=e.problem_source()) {
                    543:                String& source=*new(pool) String(pool); 
                    544:                source.append(*asource, String::UL_TAINTED, true/*forced*/);
                    545:                result.set_name(source);
1.74      paf       546: 
1.76      paf       547:                hash.put(*exception_source_part_name, new(pool) VString(source));
1.74      paf       548: #ifndef NO_STRING_ORIGIN
1.76      paf       549:                const Origin& origin=source.origin();
1.74      paf       550:                hash.put(*new(pool) String(pool, "file"),                               
                    551:                        new(pool) VString(*new(pool) String(pool, origin.file)));
                    552:                hash.put(*new(pool) String(pool, "lineno"),
                    553:                        new(pool) VInt(pool, 1+origin.line));
                    554: #endif
                    555:        }
                    556:        if(const char *ecomment=e.comment()) {
                    557:                int comment_size=strlen(ecomment);
                    558:                char *pcomment=(char *)pool.malloc(comment_size);
                    559:                memcpy(pcomment, ecomment, comment_size);
                    560:                hash.put(*exception_comment_part_name, 
1.76      paf       561:                        new(pool) VString(*new(pool) String(pool, pcomment, comment_size, true/*tainted*/)));
1.74      paf       562:        }
                    563:        hash.put(*exception_handled_part_name, 
                    564:                new(pool) VBool(pool, false));
                    565: 
                    566:        return result;
                    567: }
                    568: 
                    569: static void _try_operator(Request& r, const String& method_name, MethodParams *params) {
                    570:        Pool& pool=r.pool();
                    571: 
                    572:        Value& body_code=params->as_junction(0, "body_code must be code");
                    573:        Value& catch_code=params->as_junction(1, "catch_code must be code");
                    574: 
                    575:        Value *result;
                    576: 
                    577:        // taking snapshot of request processing status
1.77      paf       578:        //int ssexception_trace=r.exception_trace.top_index();
                    579:        int sstack=r.stack.top_index();
1.74      paf       580:        Value *sself=r.self, *sroot=r.root, *srcontext=r.rcontext;  
                    581:        WContext *swcontext=r.wcontext; 
                    582:        try {
1.81      paf       583:                result=&r.process_to_value(body_code);
1.74      paf       584:        } catch(const Exception& e) {
                    585:                // restoring request processing status
1.77      paf       586:                //r.exception_trace.top_index(ssexception_trace);
                    587:                r.stack.top_index(sstack);
1.74      paf       588:                r.self=sself; r.root=sroot, r.rcontext=srcontext; r.wcontext=swcontext;
1.77      paf       589:                
1.74      paf       590:                
                    591:                VHash& vhash=exception2vhash(pool, e);
                    592: 
                    593:                Junction *junction=catch_code.get_junction();
                    594:                Value *saved_exception_var_value=junction->root->get_element(*exception_var_name);
                    595:                junction->root->put_element(*exception_var_name, &vhash);
1.81      paf       596:                result=&r.process_to_value(catch_code);
1.74      paf       597:                bool handled=false;
                    598:                if(Value *value=static_cast<Value *>(vhash.hash(0).get(*exception_handled_part_name)))
                    599:                        handled=value->as_bool();               
                    600:                junction->root->put_element(*exception_var_name, saved_exception_var_value);
                    601: 
                    602:                if(!handled)
                    603:                        throw(e); // rethrow
                    604:        }
                    605:        // write it out 
                    606:        r.write_pass_lang(*result);
                    607: }
                    608: 
                    609: static void _throw_operator(Request& r, const String& method_name, MethodParams *params) {
                    610:        Pool& pool=r.pool();
                    611: 
                    612:        if(params->size()==1) {
                    613:                Value& param0=params->get(0);
                    614:                if(Hash *hash=param0.get_hash(&method_name)) {
1.78      paf       615:                        const char *type=0;
1.74      paf       616:                        if(Value *value=static_cast<Value *>(hash->get(*exception_type_part_name)))
1.78      paf       617:                                type=value->as_string().cstr();
1.74      paf       618:                        const String *source=0;
                    619:                        if(Value *value=static_cast<Value *>(hash->get(*exception_source_part_name)))
                    620:                                source=&value->as_string();
                    621:                        const char *comment=0;
                    622:                        if(Value *value=
                    623:                                static_cast<Value *>(hash->get(*exception_comment_part_name)))
                    624:                                comment=value->as_string().cstr();
                    625: 
1.78      paf       626:                        throw Exception(type,
1.74      paf       627:                                source?source:&method_name,
                    628:                                comment);
                    629:                } else
1.78      paf       630:                        throw Exception("parser.runtime",
1.74      paf       631:                                &method_name,
                    632:                                "one-param version has hash param");
                    633:        } else {
1.78      paf       634:                const char *type=params->as_string(0, "type must be string").cstr();
1.74      paf       635:                const String& source=params->as_string(1, "source must be string");
                    636:                const char *comment=params->as_string(2, "comment must be string").cstr();
1.78      paf       637:                throw Exception(type, &source, comment);
1.74      paf       638:        }
                    639: }
                    640:        
1.10      paf       641: // constructor
                    642: 
1.11      paf       643: MOP::MOP(Pool& apool) : Methoded(apool),
                    644:        main_sql_name(apool, MAIN_SQL_NAME),
                    645:        main_sql_drivers_name(apool, MAIN_SQL_DRIVERS_NAME)
                    646: {
1.10      paf       647:        set_name(*NEW String(pool(), OP_CLASS_NAME));
1.28      parser    648: 
1.1       paf       649:        // ^if(condition){code-when-true}
                    650:        // ^if(condition){code-when-true}{code-when-false}
1.10      paf       651:        add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1       paf       652: 
                    653:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59      paf       654:        add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1       paf       655: 
                    656:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10      paf       657:        add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1       paf       658: 
                    659:        // ^process[code]
1.10      paf       660:        add_native_method("process", Method::CT_ANY, _process, 1, 1);
1.1       paf       661: 
                    662:        // ^rem{code}
1.51      parser    663:        add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1       paf       664: 
                    665:        // ^while(condition){code}
1.10      paf       666:        add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1       paf       667: 
                    668:        // ^use[file]
1.10      paf       669:        add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1       paf       670: 
1.54      paf       671:        // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10      paf       672:        add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1       paf       673: 
                    674:        // ^eval(expr)
                    675:        // ^eval(expr)[format]
1.10      paf       676:        add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52      parser    677: 
1.1       paf       678:        // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10      paf       679:        add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
                    680: 
1.63      paf       681: 
1.65      paf       682:        // ^cache[file_spec] delete cache
1.63      paf       683:        // ^cache[file_spec](time){code} time=0 no cache
1.65      paf       684:        add_native_method("cache", Method::CT_ANY, _cache, 1, 3);
1.63      paf       685:        
1.28      parser    686:        // switch
                    687: 
                    688:        // ^switch[value]{cases}
                    689:        add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
                    690: 
                    691:        // ^case[value]{code}
1.51      parser    692:        add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.74      paf       693: 
                    694:        // try-catch
                    695: 
                    696:        // ^try{code}{catch code}
                    697:        add_native_method("try", Method::CT_ANY, _try_operator, 2, 2);
                    698:        // ^throw[$exception hash]
                    699:        // ^throw[type;source;comment]
                    700:        add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
                    701: 
1.10      paf       702: }
1.11      paf       703: 
                    704: // constructor & configurator
1.1       paf       705: 
1.10      paf       706: Methoded *MOP_create(Pool& pool) {
1.35      parser    707:        return new(pool) MOP(pool);
1.11      paf       708: }
                    709: 
                    710: void MOP::configure_user(Request& r) {
                    711:        Pool& pool=r.pool();
                    712: 
                    713:        // $MAIN:SQL.drivers
                    714:        if(Value *sql=r.main_class->get_element(main_sql_name))
                    715:                if(Value *element=sql->get_element(main_sql_drivers_name))
                    716:                        if(Table *protocol2library=element->get_table())
                    717:                                r.classes_conf.put(name(), protocol2library);
1.1       paf       718: }

E-mail: