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

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

E-mail: