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

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

E-mail: