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

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

E-mail: