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

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

E-mail: