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

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

E-mail: