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

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

E-mail: