Annotation of parser3/src/main/pa_request.C, revision 1.400

1.54      paf         1: /** @file
1.55      paf         2:        Parser: request class main part. @see compile.C and execute.C.
                      3: 
1.374     moko        4:        Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.194     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.218     paf         6: */
1.55      paf         7: 
1.69      paf         8: #include "pa_sapi.h"
1.53      paf         9: #include "pa_common.h"
1.372     moko       10: #include "pa_os.h"
1.1       paf        11: #include "pa_request.h"
1.3       paf        12: #include "pa_wwrapper.h"
                     13: #include "pa_vclass.h"
1.31      paf        14: #include "pa_globals.h"
1.30      paf        15: #include "pa_vint.h"
1.297     misha      16: #include "pa_vmethod_frame.h"
1.32      paf        17: #include "pa_types.h"
1.248     paf        18: #include "pa_venv.h"
                     19: #include "pa_vmath.h"
                     20: #include "pa_vstatus.h"
                     21: #include "pa_vrequest.h"
1.78      paf        22: #include "pa_vtable.h"
1.90      paf        23: #include "pa_vfile.h"
1.147     parser     24: #include "pa_dictionary.h"
1.208     paf        25: #include "pa_charset.h"
1.186     paf        26: #include "pa_charsets.h"
1.248     paf        27: #include "pa_cache_managers.h"
                     28: #include "pa_vmail.h"
                     29: #include "pa_vform.h"
                     30: #include "pa_vcookie.h"
                     31: #include "pa_vresponse.h"
                     32: #include "pa_vmemory.h"
1.253     paf        33: #include "pa_vconsole.h"
1.267     paf        34: #include "pa_vdate.h"
1.159     parser     35: 
1.400   ! moko       36: volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.399 2020/12/07 23:38:46 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H;
1.329     moko       37: 
1.248     paf        38: // consts
                     39: 
1.269     paf        40: #define UNHANDLED_EXCEPTION_METHOD_NAME "unhandled_exception"
1.207     paf        41: 
1.82      paf        42: /// content type of response when no $MAIN:defaults.content-type defined
1.248     paf        43: const char* DEFAULT_CONTENT_TYPE="text/html";
                     44: 
1.369     moko       45: const uint LOOP_LIMIT=20000;
1.361     moko       46: const uint EXECUTE_RECOURSION_LIMIT=1000;
1.390     moko       47: const uint HTTPD_TIMEOUT=4;
1.369     moko       48: const size_t FILE_SIZE_LIMIT=512*1024*1024;
1.361     moko       49: 
1.248     paf        50: // defines for globals
                     51: 
                     52: #define MAIN_METHOD_NAME "main"
                     53: #define AUTO_METHOD_NAME "auto"
1.364     moko       54: #define USE_METHOD_NAME "use"
1.309     misha      55: #define AUTOUSE_METHOD_NAME "autouse"
1.364     moko       56: 
1.248     paf        57: #define EXCEPTION_TYPE_PART_NAME "type"
                     58: #define EXCEPTION_SOURCE_PART_NAME "source"
                     59: #define EXCEPTION_COMMENT_PART_NAME "comment"
                     60: 
1.364     moko       61: #define ORIGIN_KEY "origin"
                     62: 
1.248     paf        63: // globals
                     64: 
                     65: const String main_method_name(MAIN_METHOD_NAME);
                     66: const String auto_method_name(AUTO_METHOD_NAME);
1.364     moko       67: static const String use_method_name(USE_METHOD_NAME);
                     68: static const String autouse_method_name(AUTOUSE_METHOD_NAME);
1.311     misha      69: 
1.248     paf        70: const String exception_type_part_name(EXCEPTION_TYPE_PART_NAME);
                     71: const String exception_source_part_name(EXCEPTION_SOURCE_PART_NAME);
                     72: const String exception_comment_part_name(EXCEPTION_COMMENT_PART_NAME);
                     73: const String exception_handled_part_name(EXCEPTION_HANDLED_PART_NAME);
                     74: 
1.364     moko       75: static const String origin_key(ORIGIN_KEY);
                     76: 
1.369     moko       77: int pa_loop_limit=LOOP_LIMIT;
1.361     moko       78: int pa_execute_recoursion_limit=EXECUTE_RECOURSION_LIMIT;
1.390     moko       79: int pa_httpd_timeout=HTTPD_TIMEOUT;
1.369     moko       80: size_t pa_file_size_limit=FILE_SIZE_LIMIT;
1.361     moko       81: 
1.248     paf        82: // defines for statics
                     83: 
                     84: #define MIME_TYPES_NAME "MIME-TYPES"
                     85: #define CLASS_PATH_NAME "CLASS_PATH"
                     86: 
1.344     moko       87: #define DOWNLOAD_NAME_UPPER "DOWNLOAD"
                     88: #define BODY_NAME_UPPER "BODY"
                     89: 
1.248     paf        90: // statics
                     91: 
                     92: static const String main_class_name(MAIN_CLASS_NAME);
                     93: static const String mime_types_name(MIME_TYPES_NAME);
1.395     moko       94: static const String class_path_name(CLASS_PATH_NAME);
1.361     moko       95: 
1.395     moko       96: static const String charsets_name("CHARSETS");
                     97: static const String strict_vars_name("STRICT-VARS");
                     98: static const String prototype_name("OBJECT-PROTOTYPE");
                     99: static const String limits_name("LIMITS");
                    100: static const String loop_limit_name("max_loop");
                    101: static const String recoursion_limit_name("max_recoursion");
                    102: static const String file_size_limit_name("max_file_size");
                    103: static const String lock_wait_timeout_name("lock_wait_timeout");
                    104: static const String httpd_name("HTTPD");
                    105: static const String httpd_timeout_name("timeout");
                    106: 
                    107: static const String conf_method_name("conf");
                    108: static const String post_process_method_name("postprocess");
                    109: static const String response_body_file_name("file");
1.248     paf       110: 
1.344     moko      111: static const String download_name_upper(DOWNLOAD_NAME_UPPER);
                    112: static const String body_name_upper(BODY_NAME_UPPER);
                    113: 
                    114: // more static
                    115: 
                    116: static const String content_type_name_upper(HTTP_CONTENT_TYPE_UPPER);
                    117: static const String content_disposition_name_upper(CONTENT_DISPOSITION_UPPER);
1.392     moko      118: static const String content_disposition_inline(CONTENT_DISPOSITION_INLINE);
1.344     moko      119: static const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT);
                    120: 
1.248     paf       121: // defines
1.82      paf       122: 
1.344     moko      123: #define CHARSET_NAME_UPPER "CHARSET"
                    124: #define LAST_MODIFIED_NAME_UPPER "LAST-MODIFIED"
                    125: 
1.233     paf       126: // op.C
1.248     paf       127: VStateless_class& VClassMAIN_create();
1.123     paf       128: 
1.157     parser    129: //
1.248     paf       130: Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, 
1.325     moko      131:                                 String::Language adefault_lang):
1.248     paf       132:        // private
                    133:        anti_endless_execute_recoursion(0),
                    134: 
                    135:        // public
1.335     moko      136:        allow_class_replace(false),
1.248     paf       137:        method_frame(0),
                    138:        rcontext(0),
                    139:        wcontext(0),
                    140:        flang(adefault_lang),
1.191     paf       141:        fconnection(0),
1.371     moko      142:        fin_cycle(0),
1.307     misha     143:        fskip(SKIP_NOTHING),
1.248     paf       144: 
                    145:        // public
1.257     paf       146:        request_info(arequest_info),
1.248     paf       147:        sapi_info(asapi_info),
1.359     moko      148:        charsets(pa_UTF8_charset, pa_UTF8_charset, pa_UTF8_charset), // default charsets
1.248     paf       149: 
                    150:        main_class(VClassMAIN_create()),
1.260     paf       151:        form(*new VForm(charsets, arequest_info)),
1.248     paf       152:        mail(*new VMail),
                    153:        response(*new VResponse(arequest_info, charsets)),
1.298     misha     154:        cookie(*new VCookie(charsets, arequest_info)),
1.285     misha     155:        console(*new VConsole),
1.248     paf       156: 
                    157:        // private
                    158:        configure_admin_done(false),
                    159: 
                    160:        // private defaults
                    161:        fdefault_lang(adefault_lang), 
                    162:        // private mime types
                    163:        mime_types(0)
1.157     parser    164: {
1.261     paf       165:        pa_register_thread_request(*this);
                    166: 
1.248     paf       167:        // file_no=0 => unknown
1.347     moko      168:        file_list+="UNKNOWN";
                    169:        file_list+="-body of process-"; // pseudo_file_no__process
1.186     paf       170: 
1.178     paf       171:        // maybe expire old caches
1.264     paf       172:        cache_managers->maybe_expire();
1.178     paf       173:        
1.157     parser    174:        /// directly used
1.233     paf       175:        // MAIN class, operators
1.353     moko      176:        put_class(&main_class);
1.157     parser    177:        // classes:
                    178:        // table, file, random, mail, image, ...
1.248     paf       179:        methoded_array().register_directly_used(*this);
1.157     parser    180: 
                    181:        /// methodless
1.354     moko      182: 
1.157     parser    183:        // env class
1.353     moko      184:        put_class(new VEnv(asapi_info));
1.175     paf       185:        // status class
1.353     moko      186:        put_class(new VStatus());
1.157     parser    187:        // request class
1.353     moko      188:        put_class(new VRequest(arequest_info, charsets, form, asapi_info));
1.157     parser    189:        // cookie class
1.353     moko      190:        put_class(&cookie);
1.253     paf       191:        // console class
1.353     moko      192:        put_class(&console);
1.354     moko      193: 
1.157     parser    194:        /// methoded
1.354     moko      195: 
1.157     parser    196:        // response class
1.353     moko      197:        put_class(&response);
1.157     parser    198:        // form class
1.353     moko      199:        put_class(&form);
1.213     paf       200:        // mail class
1.353     moko      201:        put_class(&mail);
1.157     parser    202:        // math class
1.353     moko      203:        put_class(new VMath);
1.248     paf       204:        // memory class
1.353     moko      205:        put_class(new VMemory);
1.117     paf       206: }
1.192     paf       207: 
                    208: Request::~Request() {
                    209: #ifdef XML
                    210:        // if for some strange reason xml generic errors failed to be reported, free them up
1.248     paf       211:        if(const char* xml_generic_errors=xmlGenericErrors()) {
                    212:                SAPI::log(sapi_info, "warning: unreported xmlGenericErrors: %s", xml_generic_errors);
1.341     moko      213:                pa_free((void *)xml_generic_errors);
1.192     paf       214:        }
                    215: #endif
                    216: }
1.236     paf       217: 
1.248     paf       218: Value& Request::get_self() { return method_frame/*always have!*/->self(); }
1.192     paf       219: 
1.355     moko      220: VStateless_class* Request::get_class(const String& name){
                    221:        VStateless_class* result=classes().get(name);
1.313     misha     222:        if(!result)
1.375     moko      223:                if(const Method *method=main_class.get_element_method(autouse_method_name)){
1.364     moko      224:                        Value *vname=new VString(name);
                    225:                        CONSTRUCTOR_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
                    226:                                frame.store_params(&vname, 1);
                    227:                                // we don't need the result
                    228:                                call(frame);
                    229:                        });
                    230:                        result=classes().get(name);
                    231:                }
1.309     misha     232:        return result;
                    233: }
                    234: 
1.356     moko      235: static void load_charset(HashStringValue::key_type akey, HashStringValue::value_type avalue, Request_charsets* charsets) {
1.359     moko      236:        pa_charsets.load_charset(*charsets, akey, avalue->as_string());
1.208     paf       237: }
1.356     moko      238: 
1.395     moko      239: 
                    240: #define CONF_OPTION(config, name, code, exception_name)                \
                    241:        if(config)                                              \
                    242:                if(Value* option=config->get_element(name)) {   \
                    243:                        if(option->is_evaluated_expr()) {       \
                    244:                                code;                           \
                    245:                        } else                                  \
                    246:                                throw Exception(PARSER_RUNTIME, 0, "$main:" exception_name, name.cstr()); \
                    247:                }
                    248: 
1.248     paf       249: void Request::configure_admin(VStateless_class& conf_class) {
1.208     paf       250:        if(configure_admin_done)
1.356     moko      251:                throw Exception(PARSER_RUNTIME, 0, "parser already configured");
1.208     paf       252:        configure_admin_done=true;
                    253:        
1.227     paf       254:        // charsets must only be specified in method_frame config
1.208     paf       255:        // so that users would not interfere
                    256: 
                    257:        /* $MAIN:CHARSETS[
                    258:                        $.charsetname1[/full/path/to/charset/file.cfg]
                    259:                        ...
                    260:                ]
                    261:        */
1.308     misha     262:        if(Value* vcharsets=conf_class.get_element(charsets_name)) {
1.342     moko      263:                if(!vcharsets->is_string()) {
1.248     paf       264:                        if(HashStringValue* charsets=vcharsets->get_hash())
1.281     paf       265:                                charsets->for_each<Request_charsets*>(load_charset, &this->charsets);
1.240     paf       266:                        else
1.395     moko      267:                                throw Exception(PARSER_RUNTIME, 0, "$main:%s must be hash", charsets_name.cstr());
1.342     moko      268:                }
1.208     paf       269:        }
                    270: 
1.330     moko      271: #ifdef STRICT_VARS
1.332     moko      272:        VVoid::strict_vars=false;
1.330     moko      273:        if(Value* strict_vars=conf_class.get_element(strict_vars_name)) {
                    274:                if(strict_vars->is_bool())
                    275:                        VVoid::strict_vars=strict_vars->as_bool();
1.357     moko      276:                else
1.395     moko      277:                        throw Exception(PARSER_RUNTIME, 0, "$main:%s must be bool", strict_vars_name.cstr());
1.357     moko      278:        }
                    279: #endif
                    280: 
                    281: #ifdef OBJECT_PROTOTYPE
                    282:        VClass::prototype=true;
                    283:        if(Value* prototype=conf_class.get_element(prototype_name)) {
                    284:                if(prototype->is_bool())
                    285:                        VClass::prototype=prototype->as_bool();
                    286:                else
1.395     moko      287:                        throw Exception(PARSER_RUNTIME, 0, "$main:%s must be bool", prototype_name.cstr());
1.330     moko      288:        }
                    289: #endif
                    290: 
1.362     moko      291:        Value* limits=conf_class.get_element(limits_name);
                    292: 
1.361     moko      293:        pa_loop_limit=LOOP_LIMIT;
1.395     moko      294:        CONF_OPTION(limits, loop_limit_name, {
                    295:                pa_loop_limit=option->as_int();
                    296:                if(pa_loop_limit==0) pa_loop_limit=INT_MAX;
                    297:        }, "LIMITS.%s must be int");
1.361     moko      298: 
                    299:        pa_execute_recoursion_limit=EXECUTE_RECOURSION_LIMIT;
1.395     moko      300:        CONF_OPTION(limits, recoursion_limit_name, {
                    301:                pa_execute_recoursion_limit=option->as_int();
                    302:                if(pa_execute_recoursion_limit==0) pa_execute_recoursion_limit=INT_MAX;
                    303:        }, "LIMITS.%s must be int");
1.390     moko      304: 
1.369     moko      305:        pa_file_size_limit=FILE_SIZE_LIMIT;
1.395     moko      306:        CONF_OPTION(limits, file_size_limit_name, {
                    307:                double limit=option->as_double();
                    308:                if(limit >= (double)SSIZE_MAX)
                    309:                        throw Exception(PARSER_RUNTIME, 0, "$main:LIMITS.%s must be less then %.15g", file_size_limit_name.cstr(), (double)SSIZE_MAX);
                    310:                pa_file_size_limit=(size_t)limit;
                    311:                if(pa_file_size_limit==0)
                    312:                        pa_file_size_limit=SSIZE_MAX;
                    313:        }, "LIMITS.%s must be number");
1.361     moko      314: 
1.372     moko      315:        pa_lock_attempts=PA_LOCK_ATTEMPTS;
1.395     moko      316:        CONF_OPTION(limits, lock_wait_timeout_name, {
                    317:                double limit=option->as_double();
                    318:                if(limit >= 3600*24)
                    319:                        throw Exception(PARSER_RUNTIME, 0, "$main:LIMITS.%s must be less then %d", lock_wait_timeout_name.cstr(), 3600*24);
                    320:                pa_lock_attempts=(unsigned int)(limit*2)+1;
                    321:        }, "LIMITS.%s must be number");
                    322: 
                    323:        Value* httpd=conf_class.get_element(httpd_name);
                    324: 
                    325:        pa_httpd_timeout=HTTPD_TIMEOUT;
                    326:        CONF_OPTION(httpd, httpd_timeout_name, {
                    327:                pa_httpd_timeout=option->as_int();
                    328:                if(pa_httpd_timeout==0) pa_httpd_timeout=INT_MAX;
                    329:        }, "HTTPD.%s must be int");
1.372     moko      330: 
1.227     paf       331:        // configure method_frame options
1.208     paf       332:        //      until someone with less privileges have overriden them
1.248     paf       333:        methoded_array().configure_admin(*this);
1.208     paf       334: }
1.150     parser    335: 
1.349     moko      336: const char* Request::get_exception_cstr(const Exception& e, Request::Exception_details& details) {
                    337: 
1.269     paf       338: #define PA_URI_FORMAT "%s: "
1.349     moko      339: #define PA_COMMENT_TYPE_FORMAT "%s [%s]"
                    340: 
1.269     paf       341: #define PA_ORIGIN_FILE_POS_FORMAT "%s(%d:%d): "
                    342: #define PA_SOURCE_FORMAT "'%s' "
1.349     moko      343: 
                    344: #define PA_ORIGIN_FILE_POS_VALUE file_list[details.origin.file_no].cstr(), 1+details.origin.line, 1+details.origin.col,
                    345: #define PA_SOURCE_VALUE details.problem_source->cstr(),
                    346: 
                    347: #define EXCEPTION_CSTR(f1,v1,f2,v2) \
                    348:                        snprintf(result, MAX_STRING, \
                    349:                                PA_URI_FORMAT \
                    350:                                f1 f2 \
                    351:                                PA_COMMENT_TYPE_FORMAT, \
                    352:                                request_info.uri, \
                    353:                                v1 v2 \
                    354:                                e.comment(), e.type() \
                    355:                        );
                    356: 
1.269     paf       357:        char* result=new(PointerFreeGC) char[MAX_STRING];
                    358: 
                    359:        if(details.problem_source) { // do we know the guy?
1.349     moko      360:                if(details.origin.file_no) // do whe know where he came from?
                    361:                        EXCEPTION_CSTR(PA_ORIGIN_FILE_POS_FORMAT, PA_ORIGIN_FILE_POS_VALUE, PA_SOURCE_FORMAT, PA_SOURCE_VALUE)
                    362:                else
                    363:                        EXCEPTION_CSTR(PA_SOURCE_FORMAT, PA_SOURCE_VALUE,,)
                    364:        } else {
                    365:                if(details.origin.file_no) // do whe know where he came from?
                    366:                        EXCEPTION_CSTR(PA_ORIGIN_FILE_POS_FORMAT, PA_ORIGIN_FILE_POS_VALUE,,)
                    367:                else
                    368:                        EXCEPTION_CSTR(,,,)
                    369:        }
1.269     paf       370: 
                    371:        return result;
                    372: }
1.272     paf       373: 
                    374: void Request::configure() {
                    375:        // configure admin options if not configured yet
                    376:        if(!configure_admin_done)
                    377:                configure_admin(main_class);
                    378: 
                    379:        // configure not-admin=user options
                    380:        methoded_array().configure_user(*this);
                    381: 
                    382:        // $MAIN:MIME-TYPES
1.308     misha     383:        if(Value* element=main_class.get_element(mime_types_name))
1.272     paf       384:                if(Table *table=element->get_table())
1.395     moko      385:                        mime_types=table;
1.272     paf       386: }
1.64      paf       387: /**
                    388:        load MAIN class, execute @main.
                    389:        MAIN class consists of all the auto.p files we'd manage to find
                    390:        plus
                    391:        the file user requested us to process
                    392:        all located classes become children of one another,
                    393:        composing class we name 'MAIN'
1.180     paf       394: 
                    395:        @test log stack trace
                    396: 
1.64      paf       397: */
1.390     moko      398: void Request::core(const char* config_filespec, bool header_only, const String &amain_method_name) {
1.169     parser    399:        try {
1.211     paf       400:                // loading config
1.384     moko      401:                if(config_filespec)
1.387     moko      402:                        use_file_directly(*new String(config_filespec));
1.8       paf       403: 
1.338     moko      404:                // filling mail received
                    405:                mail.fill_received(*this);
                    406: 
1.272     paf       407:                try {
                    408:                        // compile requested file
1.389     moko      409:                        if(request_info.path_translated)
                    410:                                use_file_directly(*new String(request_info.path_translated, String::L_TAINTED), true, true /* load auto.p files */);
1.272     paf       411:                        configure();
                    412:                } catch(...) {
                    413:                        configure(); // configure anyway, useful in @unhandled_exception [say, if they would want to mail by SMTP something]
                    414:                        rethrow;
                    415:                }
1.25      paf       416: 
                    417:                // execute @main[]
1.391     moko      418:                const String* body_string=amain_method_name.is_empty() ? &String::Empty : execute_method(main_class, amain_method_name);
1.41      paf       419:                if(!body_string)
1.390     moko      420:                        throw Exception(PARSER_RUNTIME, 0, "'%s' method not found", amain_method_name.cstr());
1.79      paf       421: 
1.250     paf       422:                // extract response body
1.344     moko      423:                Value* body_value=response.fields().get(download_name_upper); // $response:download?
1.250     paf       424:                bool as_attachment=body_value!=0;
                    425:                if(!body_value)
1.344     moko      426:                        body_value=response.fields().get(body_name_upper); // $response:body
1.250     paf       427:                if(!body_value)
                    428:                        body_value=new VString(*body_string); // just result of ^main[]
                    429: 
1.119     paf       430:                // @postprocess
1.383     moko      431:                if(const Method *method=main_class.get_method(post_process_method_name)) {
                    432:                        // preparing to pass parameters to
                    433:                        //      @postprocess[data]
                    434:                        METHOD_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
                    435:                                frame.store_params(&body_value, 1);
                    436:                                call(frame);
                    437:                                body_value=&frame.result();
                    438:                        });
                    439:                }
1.90      paf       440: 
1.303     misha     441:                VFile* body_file=body_value->as_vfile(flang, &charsets);
1.41      paf       442: 
1.45      paf       443:                // OK. write out the result
1.248     paf       444:                output_result(body_file, header_only, as_attachment);
1.183     paf       445: 
1.171     parser    446:        } catch(const Exception& e) { // request handling problem
1.268     paf       447:                try {
1.76      paf       448:                // we're returning not result, but error explanation
1.269     paf       449: 
                    450:                Request::Exception_details details=get_details(e);
                    451:                const char* exception_cstr=get_exception_cstr(e, details);
1.258     paf       452: 
                    453:                // reset language to default
                    454:                flang=fdefault_lang;
                    455:                
                    456:                // reset response
                    457:                response.fields().clear();
                    458: 
                    459:                // this is what we'd return in $response:body
                    460:                const String* body_string=0;
                    461: 
                    462:                // maybe we'd be lucky enough as to report an error
                    463:                // in a gracefull way...
1.383     moko      464:                if(const Method *method=main_class.get_method(*new String(UNHANDLED_EXCEPTION_METHOD_NAME))) {
                    465:                        // preparing to pass parameters to 
                    466:                        //      @unhandled_exception[exception;stack]
                    467: 
                    468:                        // $stack[^table::create{name   file    lineno  colno}]
                    469:                        Table::columns_type stack_trace_columns(new ArrayString);
                    470:                        *stack_trace_columns+=new String("name");
                    471:                        *stack_trace_columns+=new String("file");
                    472:                        *stack_trace_columns+=new String("lineno");
                    473:                        *stack_trace_columns+=new String("colno");
                    474:                        Table& stack_trace=*new Table(stack_trace_columns);
                    475:                        if(!exception_trace.is_empty()/*signed!*/) 
                    476:                                for(size_t i=exception_trace.bottom_index(); i<exception_trace.top_index(); i++) {
                    477:                                        Trace trace=exception_trace.get(i);
                    478:                                        Table::element_type row(new ArrayString);
                    479: 
                    480:                                        *row+=trace.name(); // name column
                    481:                                        Operation::Origin origin=trace.origin();
                    482:                                        if(origin.file_no) {
                    483:                                                *row+=new String(file_list[origin.file_no], String::L_TAINTED); // 'file' column
                    484:                                                *row+=new String(String::Body::Format(1+origin.line), String::L_CLEAN); // 'lineno' column
                    485:                                                *row+=new String(String::Body::Format(1+origin.col), String::L_CLEAN); // 'colno' column
                    486:                                        }
                    487:                                        stack_trace+=row;
1.233     paf       488:                                }
1.383     moko      489: 
                    490:                        // future $response:body=
                    491:                        //   execute ^unhandled_exception[exception;stack]
                    492:                        exception_trace.clear(); // forget all about previous life, in case there would be error inside of this method, error handled  would not be mislead by old stack contents (see extract_origin)
                    493: 
                    494:                        Value *params[]={&details.vhash, new VTable(&stack_trace)};
                    495:                        METHOD_FRAME_ACTION(*method, 0 /*no caller*/, main_class, {
                    496:                                frame.store_params(params, 2);
                    497:                                call(frame);
                    498:                                body_string=&frame.result().as_string();
                    499:                        });
1.258     paf       500:                }
                    501:                
1.273     paf       502:                // conditionally log it
                    503:                Value* vhandled=details.vhash.hash().get(exception_handled_part_name);
                    504:                if(!vhandled || !vhandled->as_bool()) {
                    505:                        SAPI::log(sapi_info, "%s", exception_cstr);
                    506:                }
                    507: 
1.381     moko      508:                if(body_string) {  // could report an error beautifully?
                    509:                        VString body_vstring(*body_string);
                    510:                        VFile* body_file=body_vstring.as_vfile(flang, &charsets);
                    511:                        // write it out the error
                    512:                        output_result(body_file, header_only, false);
                    513:                } else {
                    514:                        // doing that ugly
                    515:                        SAPI::send_error(sapi_info, exception_cstr, !strcmp(e.type(), "file.missing") ? "404" : "500");
                    516:                }
1.287     misha     517: 
1.273     paf       518:                } catch(const Exception& e) { // exception in unhandled exception
1.269     paf       519:                        Request::Exception_details details=get_details(e);
                    520:                        const char* exception_cstr=get_exception_cstr(e, details);
1.380     moko      521:                        // unconditionally log the beast in exception handler
                    522:                        throw Exception(0, 0, "Unhandled exception in %s", exception_cstr);
1.268     paf       523:                }
1.1       paf       524:        }
                    525: }
                    526: 
1.266     paf       527: uint Request::register_file(String::Body file_spec) {
1.248     paf       528:        file_list+=file_spec;
                    529:        return file_list.count()-1;
                    530: }
                    531: 
1.387     moko      532: void Request::use_file_directly(const String& file_spec, bool fail_on_file_absence, bool with_auto_p) {
1.73      paf       533:        // cyclic dependence check
1.318     misha     534:        if(used_files.get(file_spec))
1.248     paf       535:                return;
1.318     misha     536:        used_files.put(file_spec, true);
                    537: 
1.385     moko      538:        if(!fail_on_file_absence && !entry_exists(file_spec)) // ignore file absence if asked for
                    539:                return;
1.318     misha     540: 
1.386     moko      541:        if(with_auto_p) {
                    542:                // loading auto.p files from document_root/.. 
                    543:                // to the one beside requested file.
                    544:                // all assigned bases from upper dir
                    545:                const char* target=file_spec.cstr();
                    546: 
                    547:                // all relative paths are calculated from main document
                    548:                request_info.path_translated=target;
                    549: 
                    550:                const char* after=target;
                    551:                size_t drlen=strlen(request_info.document_root);
                    552:                if(memcmp(after, request_info.document_root, drlen)==0) {
                    553:                        after+=drlen;
                    554:                        if(after[-1]=='/') 
                    555:                                --after;
                    556:                }
                    557: 
                    558:                while(const char* before=strchr(after, '/')) {
                    559:                        String& sfile_spec=*new String;
                    560:                        if(after!=target) {
                    561:                                sfile_spec.append_strdup(target, before-target, String::L_CLEAN);
                    562:                                sfile_spec << "/" AUTO_FILE_NAME;
                    563: 
1.387     moko      564:                                use_file_directly(sfile_spec, false /*ignore absence, sole user*/);
1.386     moko      565:                        }
                    566:                        for(after=before+1;*after=='/';after++);
                    567:                }
                    568:        }
                    569: 
1.385     moko      570:        if(const char* source=file_read_text(charsets, file_spec, true))
1.387     moko      571:                use_buf(main_class, source, 0, register_file(file_spec));
1.318     misha     572: }
                    573: 
                    574: 
1.387     moko      575: void Request::use_file(const String& file_name, const String* use_filespec/*absolute*/, bool with_auto_p) {
1.326     misha     576:        if(file_name.is_empty())
1.366     moko      577:                throw Exception(PARSER_RUNTIME, 0, "usage failed - no filename was specified");
1.326     misha     578: 
1.318     misha     579:        const String* filespec=0;
                    580: 
                    581:        if(file_name.first_char()=='/') //absolute path? [no need to scan MAIN:CLASS_PATH]
1.388     moko      582:                filespec=&full_disk_path(file_name);
1.318     misha     583:        else if(use_filespec){ // search in current dir first
1.331     misha     584:                size_t last_slash_pos=use_filespec->strrpbrk("/");
                    585:                if(last_slash_pos!=STRING_NOT_FOUND)
                    586:                        filespec=file_exist(use_filespec->mid(0, last_slash_pos), file_name); // found in current dir?
1.318     misha     587:        }
                    588: 
                    589:        if(!filespec){
                    590:                // prevent multiple scan CLASS_PATH for searching one file
                    591:                if(searched_along_class_path.get(file_name))
                    592:                        return;
                    593:                searched_along_class_path.put(file_name, true);
1.308     misha     594:                if(Value* element=main_class.get_element(class_path_name)) {
1.233     paf       595:                        if(element->is_string()) {
1.388     moko      596:                                filespec=file_exist(full_disk_path(element->as_string()), file_name); // found at class_path?
1.233     paf       597:                        } else if(Table *table=element->get_table()) {
1.318     misha     598:                                for(size_t i=table->count(); i--; ) {
1.248     paf       599:                                        const String& path=*(*table->get(i))[0];
1.388     moko      600:                                        if(filespec=file_exist(full_disk_path(path), file_name))
1.233     paf       601:                                                break; // found along class_path
                    602:                                }
                    603:                        } else
1.366     moko      604:                                throw Exception(PARSER_RUNTIME, 0, "$" CLASS_PATH_NAME " must be string or table");
1.318     misha     605:                        if(!filespec)
1.395     moko      606:                                throw Exception(PARSER_RUNTIME, &file_name, "not found along $main:" CLASS_PATH_NAME);
1.318     misha     607:                } else 
1.395     moko      608:                        throw Exception(PARSER_RUNTIME, &file_name, "usage failed - no $main:" CLASS_PATH_NAME " were specified");
1.148     parser    609:        }
1.230     paf       610: 
1.387     moko      611:        use_file_directly(*filespec, true, with_auto_p);
1.16      paf       612: }
                    613: 
1.364     moko      614: void Request::use_file(const String& file_name, const String* use_filespec/*absolute*/, Operation::Origin origin) {
1.349     moko      615:        static String use("USE");
                    616:        try {
1.364     moko      617:                static VHash* voptions=new VHash();
1.376     moko      618:                if(const Method *method=main_class.get_method(use_method_name)){
1.364     moko      619:                        Value *params[]={new VString(file_name), voptions};
                    620:                        voptions->hash().put(origin_key, new VString(*use_filespec));
                    621: 
                    622:                        CONSTRUCTOR_FRAME_ACTION(*method, 0 /*no parent*/, main_class, {
                    623:                                frame.store_params(params, 2);
                    624:                                // we don't need the result
                    625:                                call(frame);
                    626:                        });
                    627:                }
1.349     moko      628:        } catch (...) {
                    629:                exception_trace.push(Trace(&use, origin));
                    630:                rethrow;
                    631:        }
                    632: }
1.208     paf       633: 
1.358     moko      634: void Request::use_buf(VStateless_class& aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset) {
                    635:        // temporary zero @conf to avoid it second execution
1.248     paf       636:        Temp_method temp_method_conf(aclass, conf_method_name, 0);
1.358     moko      637:        // temporary zero @auto to avoid it second execution
1.248     paf       638:        Temp_method temp_method_auto(aclass, auto_method_name, 0);
1.233     paf       639: 
1.295     misha     640:        // compile loaded classes
                    641:        ArrayClass& cclasses=compile(&aclass, source, main_alias, file_no, line_no_offset);
1.212     paf       642: 
1.248     paf       643:        VString* vfilespec=
                    644:                new VString(*new String(file_list[file_no], String::L_TAINTED));
1.295     misha     645: 
                    646:        for(size_t i=0; i<cclasses.count(); i++){
                    647:                VStateless_class& cclass=*cclasses.get(i);
1.296     misha     648: 
                    649:                // locate and execute possible @conf[] static
1.382     moko      650:                if(execute_method_if_exists(cclass, conf_method_name, vfilespec))
1.295     misha     651:                        configure_admin(cclass/*, executed.method->name*/);
                    652: 
                    653:                // locate and execute possible @auto[] static
1.382     moko      654:                execute_method_if_exists(cclass, auto_method_name, vfilespec);
1.336     moko      655: 
                    656:                cclass.enable_default_setter();
1.295     misha     657:        }
1.19      paf       658: }
                    659: 
1.248     paf       660: const String& Request::relative(const char* apath, const String& relative_name) {
1.341     moko      661:        char *hpath=pa_strdup(apath);
1.248     paf       662:        String& result=*new String;
                    663:        if(rsplit(hpath, '/')) // if something/splitted
1.217     paf       664:                result << hpath << "/";
1.248     paf       665:        result << relative_name;
                    666:        return result;
1.19      paf       667: }
                    668: 
1.388     moko      669: const String& Request::full_disk_path(const String& relative_name) {
1.217     paf       670:        if(relative_name.first_char()=='/') {
1.341     moko      671:                String& result=*new String(pa_strdup(request_info.document_root));
1.104     paf       672:                result << relative_name;
1.19      paf       673:                return result;
1.389     moko      674:        }
                    675:        if(relative_name.pos("://")!=STRING_NOT_FOUND // something like "http://xxx"
1.247     paf       676: #ifdef WIN32
1.389     moko      677:                || relative_name.pos(":")==1  // DRIVE:
                    678:                || relative_name.starts_with("\\\\") // UNC1
                    679:                || relative_name.starts_with("//") // UNC2
1.247     paf       680: #endif
1.389     moko      681:                )
                    682:                return relative_name;
                    683: 
                    684:        return relative(request_info.path_translated ? request_info.path_translated : request_info.document_root, relative_name);
1.1       paf       685: }
1.45      paf       686: 
1.267     paf       687: #ifndef DOXYGEN
                    688: class Add_header_attribute_info
                    689: {
                    690: public:
                    691:        Add_header_attribute_info(Request& ar) : r(ar), add_last_modified(true) {}
                    692:        Request& r;
                    693:        bool add_last_modified;
                    694: };
                    695: #endif
1.344     moko      696: static void add_header_attribute(HashStringValue::key_type name, HashStringValue::value_type value, Add_header_attribute_info* info) {
                    697:        if(name==BODY_NAME_UPPER || name==DOWNLOAD_NAME_UPPER || name==CHARSET_NAME_UPPER)
1.101     paf       698:                return;
1.290     misha     699:        
1.345     moko      700:        if(name==LAST_MODIFIED_NAME_UPPER)
                    701:                info->add_last_modified=false;
                    702: 
1.316     misha     703:        const char* aname=String(name, String::L_URI).untaint_and_transcode_cstr(String::L_URI, &info->r.charsets);
1.101     paf       704: 
1.267     paf       705:        SAPI::add_header_attribute(info->r.sapi_info,
1.345     moko      706:                        aname,
1.316     misha     707:                        attributed_meaning_to_string(*value, String::L_URI, false).untaint_and_transcode_cstr(String::L_URI, &info->r.charsets)
1.303     misha     708:                );
1.267     paf       709: }
                    710: 
1.366     moko      711: static void output_sole_piece(Request& r, bool header_only, VFile& body_file, Value* body_file_content_type) {
1.267     paf       712:        // transcode text body when "text/*" or simple result
                    713:        String::C output(body_file.value_ptr(), body_file.value_size());
                    714:        if(!body_file_content_type/*vstring.as_vfile*/ || body_file_content_type->as_string().pos("text/")==0)
1.366     moko      715:                output=Charset::transcode(output, r.charsets.source(), r.charsets.client());
1.267     paf       716: 
1.311     misha     717:        // prepare header: Content-Length
                    718:        SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, format(output.length, "%u"));
1.267     paf       719: 
                    720:        // send header
                    721:        SAPI::send_header(r.sapi_info);
                    722:        
                    723:        // send body
                    724:        if(!header_only)
                    725:                SAPI::send_body(r.sapi_info, output.str, output.length);
                    726: }
                    727: 
                    728: #ifndef DOXYGEN
                    729: struct Range
                    730: {
1.396     moko      731:        uint64_t start;
                    732:        uint64_t end;
1.267     paf       733: };
                    734: #endif
1.396     moko      735: 
                    736: #define UNSET ((uint64_t)-1)
                    737: 
1.313     misha     738: static void parse_range(const String* s, Array<Range> &ar) {
1.267     paf       739:        const char *p = s->cstr();
                    740:        if(s->starts_with("bytes="))
                    741:                p += 6;
                    742:        Range r;
                    743:        while(*p){
1.396     moko      744:                r.start = UNSET;
                    745:                r.end = UNSET;
1.267     paf       746:                if(*p >= '0' && *p <= '9'){
1.396     moko      747:                        r.start = pa_atoul(p);
1.267     paf       748:                        while(*p>='0' && *p<='9') ++p;
                    749:                }
                    750:                if(*p++ != '-') break;
                    751:                if(*p >= '0' && *p <= '9'){
1.396     moko      752:                        r.end = pa_atoul(p);
1.267     paf       753:                        while(*p>='0' && *p<='9') ++p;
                    754:                }
                    755:                if(*p == ',') ++p;
                    756:                ar += r;
                    757:        }
1.101     paf       758: }
1.267     paf       759: 
1.400   ! moko      760: struct Send_range_action_info {
        !           761:        Request *r;
        !           762:        uint64_t offset;
        !           763:        uint64_t part_length;
        !           764: };
        !           765: 
        !           766: static void send_range(struct stat& /*finfo*/, int f, const String& /*file_spec*/, void *context){
        !           767:        Send_range_action_info &info = *(Send_range_action_info*)context;
        !           768: 
        !           769:        pa_lseek(f, info.offset, SEEK_SET);
        !           770: 
        !           771:        const size_t BUFSIZE = 128*0x400;
        !           772:        char buf[BUFSIZE];
        !           773:        do{
        !           774:                size_t to_read = info.part_length < BUFSIZE ? (size_t)info.part_length : BUFSIZE;
        !           775:                size_t to_write = file_block_read(f, buf, to_read);
        !           776: 
        !           777:                if(to_write == 0)
        !           778:                        break;
        !           779: 
        !           780:                size_t size = SAPI::send_body(info.r->sapi_info, buf, to_write);
        !           781:                if(size != to_write)
        !           782:                        break;
        !           783: 
        !           784:                info.part_length -= to_write;
        !           785:        } while (info.part_length);
        !           786: }
        !           787: 
1.396     moko      788: static void output_pieces(Request& r, bool header_only, const String& filename, uint64_t content_length, Value& date, bool add_last_modified) {
1.315     misha     789:        SAPI::add_header_attribute(r.sapi_info, "accept-ranges", "bytes");
1.270     paf       790: 
1.339     moko      791:        const char *range = SAPI::Env::get(r.sapi_info, "HTTP_RANGE");
1.396     moko      792:        uint64_t offset=0;
                    793:        uint64_t part_length=content_length;
1.267     paf       794:        if(range){
                    795:                Array<Range> ar;
                    796:                parse_range(new String(range), ar);
1.396     moko      797:                int count = ar.count();
1.267     paf       798:                if(count == 1){
                    799:                        Range &rg = ar.get_ref(0);
1.396     moko      800:                        if(rg.start == UNSET && rg.end == UNSET){
1.311     misha     801:                                SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "416 Requested Range Not Satisfiable");
1.267     paf       802:                                return;
                    803:                        }
1.396     moko      804:                        if(rg.start == UNSET && rg.end != UNSET){
1.267     paf       805:                                rg.start = content_length - rg.end;
                    806:                                rg.end = content_length;
                    807:                                offset += rg.start;
                    808:                                part_length = rg.end-rg.start;
1.396     moko      809:                        }else if(rg.start != UNSET && rg.end == UNSET){
1.267     paf       810:                                rg.end = content_length-1;
                    811:                                offset += rg.start;
                    812:                                part_length -= rg.start;
                    813:                        }
                    814:                        if(part_length == 0){
1.311     misha     815:                                SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "204 No Content");
1.267     paf       816:                                return;
                    817:                        }
1.311     misha     818:                        SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206 Partial Content");
1.400   ! moko      819:                        char buf[MAX_STRING];
        !           820:                        snprintf(buf, MAX_STRING, "bytes %.15g-%.15g/%.15g", (double)rg.start, (double)rg.end, (double)content_length);
1.315     misha     821:                        SAPI::add_header_attribute(r.sapi_info, "content-range", buf);
1.267     paf       822:                }else if(count != 0){
1.311     misha     823:                        SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "501 Not Implemented");
1.267     paf       824:                        return;
                    825:                }
                    826:        }
                    827: 
1.398     moko      828:        SAPI::add_header_attribute(r.sapi_info, HTTP_CONTENT_LENGTH, format((double)part_length, "%.15g"));
1.267     paf       829: 
1.314     misha     830:        if(add_last_modified)
1.315     misha     831:                SAPI::add_header_attribute(r.sapi_info, "last-modified", attributed_meaning_to_string(date, String::L_AS_IS, true).cstr());
1.314     misha     832: 
1.267     paf       833:        SAPI::send_header(r.sapi_info);
                    834: 
                    835:        if(!header_only){
1.400   ! moko      836:                Send_range_action_info info = { &r, offset, part_length};
        !           837:                file_read_action_under_lock(r.full_disk_path(filename), "send", send_range, &info);
1.267     paf       838:        }
                    839: }
                    840: 
1.248     paf       841: void Request::output_result(VFile* body_file, bool header_only, bool as_attachment) {
1.51      paf       842:        // header: cookies
1.248     paf       843:        cookie.output_result(sapi_info);
1.51      paf       844:        
1.394     moko      845:        // $.file and $.name (when body is real vfile)
                    846:        Value* vfile = body_file->fields().get(response_body_file_name);
                    847:        Value* vname = body_file->fields().get(name_name);
                    848: 
                    849:        const String* sfile = vfile ? &vfile->as_string() : NULL;
                    850:        const String* sname = vname ? &vname->as_string() : NULL;
                    851: 
                    852:        if(sname && *sname == NONAME_DAT)
                    853:                sname = NULL;
1.92      paf       854: 
1.314     misha     855:        // Content-Disposition
1.394     moko      856:        const String* disposition_name = sname ? sname->is_empty() ? NULL : sname : sfile;
                    857:        if(disposition_name) {
                    858:                VHash& hash=*new VHash();
                    859:                hash.hash().put(value_name, new VString(as_attachment ? content_disposition_attachment : content_disposition_inline));
                    860:                hash.hash().put(content_disposition_filename_name, new VString(*new String(*disposition_name, String::L_HTTP_HEADER)));
                    861:                response.fields().put(content_disposition_name_upper, &hash);
                    862:        }
1.267     paf       863: 
1.394     moko      864:        // _file_ content-type might be specified
                    865:        Value* body_file_content_type = body_file->fields().get(content_type_name);
                    866:        if(!body_file_content_type){
                    867:                const String* scontent_name = sname && !sname->is_empty() ? sname : sfile;
                    868:                if(scontent_name)
                    869:                        body_file_content_type = new VString(mime_type_of(scontent_name->cstr()));
1.267     paf       870:        }
                    871: 
1.312     misha     872:        // set Content-Type
1.314     misha     873:        if(body_file_content_type) {
                    874:                // body file content type
1.344     moko      875:                response.fields().put(content_type_name_upper, body_file_content_type);
1.314     misha     876:        } else {
                    877:                // default content type
1.344     moko      878:                response.fields().put_dont_replace(content_type_name_upper, new VString(*new String(DEFAULT_CONTENT_TYPE)));
1.314     misha     879:        }
1.312     misha     880: 
1.314     misha     881:        // prepare header: $response:fields without :body, :download and :charset
1.267     paf       882:        Add_header_attribute_info info(*this);
1.281     paf       883:        response.fields().for_each<Add_header_attribute_info*>(add_header_attribute, &info);
1.50      paf       884: 
1.394     moko      885:        if(sfile) {
1.314     misha     886:                // $response:[download|body][$.file[filespec]] -- optput specified file
1.367     moko      887:                uint64_t content_length=0;
1.267     paf       888:                time_t atime=0, mtime=0, ctime=0;
1.394     moko      889:                file_stat(full_disk_path(*sfile), content_length, atime, mtime, ctime);
1.267     paf       890: 
                    891:                VDate* vdate=0;
                    892:                if(Value* v=body_file->fields().get("mdate")) {
1.308     misha     893:                        if(Value* vdatep=v->as(VDATE_TYPE))
1.267     paf       894:                                vdate=static_cast<VDate*>(vdatep);
                    895:                        else 
1.286     misha     896:                                throw Exception(PARSER_RUNTIME, 0, "mdate must be a date");
1.267     paf       897:                }
                    898:                if(!vdate)
1.346     moko      899:                        vdate=new VDate((pa_time_t)mtime);
1.62      paf       900: 
1.399     moko      901:                output_pieces(*this, header_only, *sfile, content_length, *vdate, info.add_last_modified);
1.267     paf       902:        } else {
1.314     misha     903:                if(body_file_content_type)
                    904:                        if(HashStringValue *hash=body_file_content_type->get_hash())
                    905:                                body_file_content_type=hash->get(value_name);
                    906: 
1.366     moko      907:                output_sole_piece(*this, header_only, *body_file, body_file_content_type);
1.267     paf       908:        }
1.50      paf       909: }
1.104     paf       910: 
1.328     misha     911: const String& Request::mime_type_of(const String* file_name) {
                    912:        return mime_type_of(file_name?file_name->taint_cstr(String::L_FILE_SPEC):0);
                    913: }
                    914: 
1.248     paf       915: const String& Request::mime_type_of(const char* user_file_name_cstr) {
1.104     paf       916:        if(mime_types)
1.248     paf       917:                if(const char* cext=strrchr(user_file_name_cstr, '.')) {
                    918:                        String sext(++cext);
1.246     paf       919:                        Table::Action_options options;
1.342     moko      920:                        if(mime_types->locate(0, sext.change_case(charsets.source(), String::CC_LOWER), options)) {
1.313     misha     921:                                if(const String* result=mime_types->item(1))
                    922:                                        return *result;
1.104     paf       923:                                else
1.395     moko      924:                                        throw Exception(PARSER_RUNTIME, 0, MIME_TYPES_NAME " table column elements must not be empty");
1.342     moko      925:                        }
1.104     paf       926:                }
1.248     paf       927: 
                    928:        return *new String("application/octet-stream");
                    929: }
                    930: 
1.365     moko      931: const String* Request::get_used_filespec(uint file_no){
1.318     misha     932:        if(file_no < file_list.count())
                    933:                return new String(file_list[file_no], String::L_TAINTED);
                    934:        return 0;
                    935: }
                    936: 
1.248     paf       937: #ifdef XML
1.280     paf       938: xmlChar* Request::transcode(const String& s) {
1.248     paf       939:        return charsets.source().transcode(s);
                    940: }
                    941: 
1.280     paf       942: xmlChar* Request::transcode(const String::Body s) {
1.248     paf       943:        return charsets.source().transcode(s);
                    944: }
                    945: 
1.280     paf       946: const String& Request::transcode(const xmlChar* s) {
1.248     paf       947:        return charsets.source().transcode(s);
1.233     paf       948: }
1.248     paf       949: #endif
                    950: 
1.349     moko      951: Request::Exception_details Request::get_details(const Exception& e) {
                    952:        const String* problem_source=e.problem_source();
                    953:        VHash& vhash=*new VHash;  HashStringValue& hash=vhash.hash();
                    954:        Operation::Origin origin={0, 0, 0};
1.248     paf       955: 
1.349     moko      956:        if(!exception_trace.is_empty()) {
                    957:                Trace bottom=exception_trace.bottom_value();
1.350     moko      958:                origin=bottom.origin();
                    959:                if(!problem_source) { // we don't know who trigged the bug
                    960:                        problem_source=bottom.name(); // we usually know source of next-from-throw-point exception did that
                    961:                        exception_trace.set_bottom_index(exception_trace.bottom_index()+1);
                    962:                } else if (bottom.name()==problem_source) { // it is that same guy?
1.349     moko      963:                        exception_trace.set_bottom_index(exception_trace.bottom_index()+1); // throw away that trace
                    964:                } else {
                    965:                        // stack top contains not us, leaving intact to help ^throw
1.279     paf       966:                }
1.248     paf       967:        }
                    968: 
                    969:        // $.type
                    970:        if(const char* type=e.type(true))
                    971:                hash.put(exception_type_part_name, new VString(*new String(type)));
                    972: 
                    973:        // $.source
1.349     moko      974:        if(problem_source)
                    975:                hash.put(exception_source_part_name, new VString(*new String(*problem_source, String::L_TAINTED)));
1.248     paf       976: 
1.349     moko      977:        // $.file $.lineno $.colno
                    978:        if(origin.file_no){
1.347     moko      979:                hash.put("file", new VString(*new String(file_list[origin.file_no], String::L_TAINTED)));
                    980:                hash.put("lineno", new VInt(1+origin.line));
                    981:                hash.put("colno", new VInt(1+origin.col));
1.248     paf       982:        }
                    983: 
                    984:        // $.comment
                    985:        if(const char* comment=e.comment(true))
1.349     moko      986:                hash.put(exception_comment_part_name, new VString(*new String(comment, String::L_TAINTED)));
1.248     paf       987: 
                    988:        // $.handled(0)
1.299     misha     989:        hash.put(exception_handled_part_name, &VBool::get(false));
1.233     paf       990: 
1.349     moko      991:        return Request::Exception_details(origin, problem_source, vhash);
1.133     parser    992: }
1.337     moko      993: 
                    994: Temp_value_element::Temp_value_element(Request& arequest, Value& awhere, const String& aname, Value* awhat) :
                    995:        frequest(arequest),
                    996:        fwhere(awhere),
                    997:        fname(aname),
                    998:        saved(awhere.get_element(aname))
                    999: {
                   1000:        Junction* junction;
                   1001:        if(saved && (junction=saved->get_junction()) && junction->is_getter)
                   1002:                saved=0;
                   1003:        frequest.put_element(fwhere, aname, awhat);
                   1004: }
                   1005: 
                   1006: Temp_value_element::~Temp_value_element() {
                   1007:        frequest.put_element(fwhere, fname, saved ? saved : VVoid::get());
                   1008: }

E-mail: