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

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

E-mail: