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

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

E-mail: