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

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

E-mail: