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

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

E-mail: