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

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

E-mail: