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

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.134   ! paf         8: static const char* IDENT_OP_C="$Date: 2003/10/21 04:45:18 $";
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.130     paf       325:        Temp_hash_value<const String::Body, PA_Object*> 
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.129     paf       399: class Cache_data: public PA_Object {
                    400: public:
1.79      paf       401:        time_t expires;
                    402: };
1.69      paf       403: struct Locked_process_and_cache_put_action_info {
                    404:        Request *r;
1.79      paf       405:        Cache_data *data;
1.129     paf       406:        Value* body_code; const String* evaluated_body;
1.69      paf       407: };
                    408: #endif
1.129     paf       409: /* @todo maybe network order worth spending some effort?
                    410:        don't bothering myself with network byte order,
                    411:        am not planning to be able to move resulting file across platforms
                    412: */
1.69      paf       413: static void locked_process_and_cache_put_action(int f, void *context) {
                    414:        Locked_process_and_cache_put_action_info& info=
                    415:                *static_cast<Locked_process_and_cache_put_action_info *>(context);
1.129     paf       416: 
1.69      paf       417:        // body->process 
1.81      paf       418:        info.evaluated_body=&info.r->process_to_string(*info.body_code);
1.69      paf       419: 
1.79      paf       420:        // expiration time not spoiled by ^cache(0) or something?
                    421:        if(info.data->expires > time(0)) {
                    422:                // string -serialize> buffer
1.129     paf       423:                String::Cm serialized=info.evaluated_body->serialize(
                    424:                        sizeof(Data_string_serialized_prolog));
1.79      paf       425:                Data_string_serialized_prolog& prolog=
1.129     paf       426:                        *reinterpret_cast<Data_string_serialized_prolog *>(serialized.str);
1.79      paf       427:                prolog.version=DATA_STRING_SERIALIZED_VERSION;
                    428:                prolog.expires=info.data->expires;
                    429:                
                    430:                // buffer -write> file
1.129     paf       431:                write(f, serialized.str, serialized.length);
1.79      paf       432:        } else // expired!
                    433:                info.data->expires=0; // flag it so that could be easily checked by caller
1.69      paf       434: }
1.129     paf       435: const String* locked_process_and_cache_put(Request& r, 
1.128     paf       436:                                           Value& body_code,
                    437:                                           Cache_data& data,
                    438:                                           const String& file_spec) {
1.129     paf       439:        Locked_process_and_cache_put_action_info info={0};
                    440:        info.r=&r;
                    441:        info.data=&data;
                    442:        info.body_code=&body_code;
1.69      paf       443: 
1.129     paf       444:        const String* result=file_write_action_under_lock(
1.69      paf       445:                file_spec, 
                    446:                "cache_put", locked_process_and_cache_put_action, &info,
                    447:                false/*as_text*/,
                    448:                false/*do_append*/,
1.96      paf       449:                false/*block*/,
                    450:                false/*fail on lock problem*/) ? info.evaluated_body: 0;
1.100     paf       451:        time_t now=time(0);
                    452:        if(data.expires<=now)
1.79      paf       453:                cache_delete(file_spec);
                    454:        return result;
1.63      paf       455: }
1.129     paf       456: const String* cache_get(Request_charsets& charsets, const String& file_spec, time_t now) {
                    457:        File_read_result file=file_read(charsets, file_spec, 
1.63      paf       458:                           false/*as_text*/, 
1.129     paf       459:                           0, //no params
                    460:                           false/*fail_on_read_problem*/);
                    461:        if(file.success && file.length/* ignore reads which are empty due to 
1.72      paf       462:                        non-unary open+lockEX conflict with lockSH */) {
1.129     paf       463:                        
1.72      paf       464:                Data_string_serialized_prolog& prolog=
1.129     paf       465:                        *reinterpret_cast<Data_string_serialized_prolog *>(file.str);
1.63      paf       466: 
1.129     paf       467:                String* result=new String;
1.72      paf       468:                if(
1.129     paf       469:                        file.length>=sizeof(Data_string_serialized_prolog)
1.72      paf       470:                        && prolog.version==DATA_STRING_SERIALIZED_VERSION
1.79      paf       471:                        && prolog.expires > now
1.129     paf       472:                        && result->deserialize(sizeof(Data_string_serialized_prolog),  file.str, file.length))
1.72      paf       473:                        return result;
                    474:        }
1.63      paf       475: 
1.72      paf       476:        return 0;
1.63      paf       477: }
1.129     paf       478: static time_t as_expires(Request& r, MethodParams& params, 
1.79      paf       479:                                                int index, time_t now) {
                    480:        time_t result;
1.129     paf       481:        if(Value* vdate=params[index].as(VDATE_TYPE, false))
                    482:                result=static_cast<VDate*>(vdate)->get_time();
1.79      paf       483:        else
1.129     paf       484:                result=now+(time_t)params.as_double(index, "lifespan must be date or number", r);
1.79      paf       485:        
                    486:        return result;
                    487: }
1.129     paf       488: static const String& as_file_spec(Request& r, MethodParams& params, int index) {
                    489:        return r.absolute(params.as_string(index, "filespec must be string"));
1.79      paf       490: }
1.129     paf       491: static void _cache(Request& r, MethodParams& params) {
1.79      paf       492:        time_t now=time(0);
                    493: 
                    494:        // ^cache[filename] ^cache(seconds) ^cache[expires date]
1.129     paf       495:        if(params.count()==1) {
                    496:                if(params[0].is_string()) { // filename?
1.79      paf       497:                        cache_delete(as_file_spec(r, params, 0));
                    498:                        return;
                    499:                }
                    500: 
                    501:                // secods|expires date
1.129     paf       502:                Cache_data* data=static_cast<Cache_data*>(r.classes_conf.get(cache_data_name));
1.79      paf       503:                if(!data)
                    504:                        throw Exception("parser.runtime",
1.129     paf       505:                                0,
1.79      paf       506:                                "expire-time reducing instruction without cache");
                    507:                
1.129     paf       508:                time_t expires=as_expires(r, params, 0, now);
1.79      paf       509:                if(expires < data->expires)
                    510:                        data->expires=expires;
                    511: 
                    512:                return;
                    513:        }
1.63      paf       514:        
                    515:        // file_spec, expires, body code
1.129     paf       516:        const String& file_spec=r.absolute(params.as_string(0, "filespec must be string"));
1.65      paf       517: 
1.129     paf       518:        Cache_data* data=new Cache_data;
1.130     paf       519:        Temp_hash_value<const String::Body, PA_Object*> 
1.129     paf       520:                cache_data_setter(r.classes_conf, cache_data_name, data);
                    521:        data->expires=as_expires(r, params, 1, now);
                    522:        Value& body_code=params.as_junction(2, "body must be code");
1.63      paf       523: 
1.129     paf       524:        if(data->expires>now) { // valid 'expires' specified? try cached copy...
1.69      paf       525:                // hence we don't hope to have unary create/lockEX
                    526:                // we need some plan to live in a life like that, so... 
                    527:                // worst races plan:
                    528:                // A        B
                    529:                // open
                    530:                //          |open
                    531:                // lockSH
                    532:                //          |nonblocking-lockEX fails
                    533:                // unlockSH
                    534:                // close, cache_get returns 0
                    535:                // open
                    536:                // nonblocking-lockEX succeeds; process, write, close
                    537:                //          |retry1: open
                    538:                // ...
                    539:                //          |lockSH succeeds; ...
                    540: 
                    541:                for(int retry=0; retry<2; retry++) {
1.129     paf       542:                        if(const String* cached_body=cache_get(r.charsets, file_spec, now)) { // have cached copy?
1.79      paf       543:                                // write it out 
                    544:                                r.write_assign_lang(*cached_body);
                    545:                                // happy with it
                    546:                                return;
                    547:                        }
1.69      paf       548: 
                    549:                        // non-blocked lock; process; cache it
1.129     paf       550:                        if(const String* processed_body=
                    551:                                locked_process_and_cache_put(r, body_code, *data, file_spec)) {
1.63      paf       552:                                // write it out 
1.69      paf       553:                                r.write_assign_lang(*processed_body);
1.63      paf       554:                                // happy with it
                    555:                                return;
1.69      paf       556:                        } else { // somebody writing result right now
                    557:                                pa_sleep(0, 500000); // waiting half a second
                    558:                                retry=0; // prolonging our wait, than could cache_get it, without processing body_code
1.63      paf       559:                        }
1.69      paf       560:                }
1.78      paf       561:                throw Exception(0,
1.69      paf       562:                        &file_spec,
                    563:                        "locking problem");
                    564:        } else { 
1.79      paf       565:                // instructed not to cache; forget cached copy
1.68      paf       566:                cache_delete(file_spec);
1.69      paf       567:                // process
1.81      paf       568:                const String& processed_body=r.process_to_string(body_code);
1.69      paf       569:                // write it out 
                    570:                r.write_assign_lang(processed_body);
                    571:                // happy with it
                    572:                return;
                    573:        }
                    574:        // never reached
1.63      paf       575: }
                    576: 
1.74      paf       577: 
                    578: 
1.129     paf       579: // also used in pa_request.C to pass param to @unhandled_exception
1.74      paf       580: 
1.129     paf       581: static void _try_operator(Request& r, MethodParams& params) {
                    582:        Value& body_code=params.as_junction(0, "body_code must be code");
                    583:        Value& catch_code=params.as_junction(1, "catch_code must be code");
1.74      paf       584: 
1.86      paf       585:        StringOrValue result;
1.123     paf       586:        // taking snapshot of try-context
                    587:        Request_context_saver try_context(r);
1.74      paf       588:        try {
1.86      paf       589:                result=r.process(body_code);
1.74      paf       590:        } catch(const Exception& e) {
1.133     paf       591:                Request_context_saver throw_context(r); // taking snapshot of throw-context [stack trace contains error]
1.129     paf       592:                Request::Exception_details details=r.get_details(e);
1.123     paf       593:                try_context.restore(); // restoring try-context to perform catch-code
                    594: 
1.129     paf       595:                Junction* junction=catch_code.get_junction();
                    596:                Value* method_frame=junction->method_frame;
                    597:                Value* saved_exception_var_value=method_frame->get_element(exception_var_name, *method_frame, false);
                    598:                junction->method_frame->put_element(exception_var_name, &details.vhash, false);
1.86      paf       599:                result=r.process(catch_code);
1.74      paf       600:                bool handled=false;
1.129     paf       601:                if(Value* value=
                    602:                        details.vhash.hash().get(exception_handled_part_name))
1.74      paf       603:                        handled=value->as_bool();               
1.129     paf       604:                junction->method_frame->put_element(exception_var_name, saved_exception_var_value, false);
1.74      paf       605: 
1.123     paf       606:                if(!handled) {
                    607:                        throw_context.restore(); // restoring throw-context [exception were not handled]
1.129     paf       608:                        rethrow;
1.123     paf       609:                }
1.74      paf       610:        }
1.123     paf       611:        // write out result
1.86      paf       612:        r.write_pass_lang(result);
1.74      paf       613: }
                    614: 
1.129     paf       615: static void _throw_operator(Request& r, MethodParams& params) {
                    616:        if(params.count()==1) {
                    617:                if(HashStringValue *hash=params[0].get_hash()) {
                    618:                        const char* type=0;
                    619:                        if(Value* value=hash->get(exception_type_part_name))
1.78      paf       620:                                type=value->as_string().cstr();
1.129     paf       621:                        const String* source=0;
                    622:                        if(Value* value=hash->get(exception_source_part_name))
1.74      paf       623:                                source=&value->as_string();
1.129     paf       624:                        const char* comment;
                    625:                        if(Value* value=hash->get(exception_comment_part_name))
1.74      paf       626:                                comment=value->as_string().cstr();
                    627: 
1.78      paf       628:                        throw Exception(type,
1.129     paf       629:                                source?source:0,
                    630:                                "%s", comment?comment:"");
1.74      paf       631:                } else
1.78      paf       632:                        throw Exception("parser.runtime",
1.129     paf       633:                                0,
1.74      paf       634:                                "one-param version has hash param");
                    635:        } else {
1.129     paf       636:                const char* type=params.as_string(0, "type must be string").cstr();
                    637:                const String& source=params.as_string(1, "source must be string");
                    638:                const char* comment=params.count()>2? params.as_string(2, "comment must be string").cstr()
                    639:                        :0;
1.97      paf       640:                throw Exception(type, &source, "%s", comment?comment:"");
1.74      paf       641:        }
1.132     paf       642:  }
1.129     paf       643: 
                    644: #if defined(WIN32) && defined(_DEBUG)
                    645: #      define PA_BPT
                    646: static void _bpt(Request& r, MethodParams& params) {
                    647:        _asm int 3;
                    648: }
                    649: #endif
                    650: 
1.10      paf       651: // constructor
                    652: 
1.129     paf       653: VClassMAIN::VClassMAIN(): VClass() {
                    654:        set_name(*new String(MAIN_CLASS_NAME));
                    655: 
                    656: #ifdef PA_BPT
                    657:        // ^bpt[]
                    658:        add_native_method("bpt", Method::CT_ANY, _bpt, 0, 0);
                    659: #endif
1.121     paf       660: 
1.1       paf       661:        // ^if(condition){code-when-true}
                    662:        // ^if(condition){code-when-true}{code-when-false}
1.10      paf       663:        add_native_method("if", Method::CT_ANY, _if, 2, 3);
1.1       paf       664: 
                    665:        // ^untaint[as-is|uri|sql|js|html|html-typo]{code}
1.59      paf       666:        add_native_method("untaint", Method::CT_ANY, _untaint, 1, 2);
1.1       paf       667: 
                    668:        // ^taint[as-is|uri|sql|js|html|html-typo]{code}
1.10      paf       669:        add_native_method("taint", Method::CT_ANY, _taint, 1, 2);
1.1       paf       670: 
                    671:        // ^process[code]
1.116     paf       672:        add_native_method("process", Method::CT_ANY, _process, 1, 2);
1.1       paf       673: 
                    674:        // ^rem{code}
1.51      parser    675:        add_native_method("rem", Method::CT_ANY, _rem, 1, 10000);
1.1       paf       676: 
                    677:        // ^while(condition){code}
1.10      paf       678:        add_native_method("while", Method::CT_ANY, _while, 2, 2);
1.1       paf       679: 
                    680:        // ^use[file]
1.10      paf       681:        add_native_method("use", Method::CT_ANY, _use, 1, 1);
1.1       paf       682: 
1.54      paf       683:        // ^for[i](from-number;to-number-inclusive){code}[delim]
1.10      paf       684:        add_native_method("for", Method::CT_ANY, _for, 3+1, 3+1+1);
1.1       paf       685: 
                    686:        // ^eval(expr)
                    687:        // ^eval(expr)[format]
1.10      paf       688:        add_native_method("eval", Method::CT_ANY, _eval, 1, 2);
1.52      parser    689: 
1.1       paf       690:        // ^connect[protocol://user:pass@host[:port]/database]{code with ^sql-s}
1.10      paf       691:        add_native_method("connect", Method::CT_ANY, _connect, 2, 2);
                    692: 
1.63      paf       693: 
1.65      paf       694:        // ^cache[file_spec] delete cache
1.63      paf       695:        // ^cache[file_spec](time){code} time=0 no cache
1.65      paf       696:        add_native_method("cache", Method::CT_ANY, _cache, 1, 3);
1.63      paf       697:        
1.28      parser    698:        // switch
                    699: 
                    700:        // ^switch[value]{cases}
                    701:        add_native_method("switch", Method::CT_ANY, _switch, 2, 2);
                    702: 
                    703:        // ^case[value]{code}
1.51      parser    704:        add_native_method("case", Method::CT_ANY, _case, 2, 10000);
1.74      paf       705: 
                    706:        // try-catch
                    707: 
                    708:        // ^try{code}{catch code}
                    709:        add_native_method("try", Method::CT_ANY, _try_operator, 2, 2);
                    710:        // ^throw[$exception hash]
                    711:        // ^throw[type;source;comment]
                    712:        add_native_method("throw", Method::CT_ANY, _throw_operator, 1, 3);
                    713: 
1.10      paf       714: }
1.11      paf       715: 
                    716: // constructor & configurator
1.1       paf       717: 
1.129     paf       718: VStateless_class& VClassMAIN_create() {
                    719:        return *new VClassMAIN;
1.1       paf       720: }

E-mail: