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

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

E-mail: