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

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

E-mail: