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

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

E-mail: