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

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