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

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

E-mail: