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

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

E-mail: