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

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

E-mail: