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

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

E-mail: