--- parser3/src/main/pa_request.C 2026/09/26 17:41:10 1.445 +++ parser3/src/main/pa_request.C 2026/10/28 01:37:15 1.474 @@ -7,7 +7,6 @@ #include "pa_sapi.h" #include "pa_common.h" -#include "pa_dir.h" #include "pa_os.h" #include "pa_request.h" #include "pa_wwrapper.h" @@ -27,6 +26,7 @@ #include "pa_charsets.h" #include "pa_cache_managers.h" #include "pa_http.h" +#include "pa_websocket.h" #include "pa_vmail.h" #include "pa_vform.h" #include "pa_vcookie.h" @@ -35,8 +35,9 @@ #include "pa_vconsole.h" #include "pa_vdate.h" #include "pa_varray.h" +#include "pa_code_cache.h" -volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.445 2026/09/26 17:41:10 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; +volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.474 2026/10/28 01:37:15 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -45,12 +46,21 @@ volatile const char * IDENT_PA_REQUEST_C /// content type of response when no $MAIN:defaults.content-type defined const char* DEFAULT_CONTENT_TYPE="text/html"; -const uint LOOP_LIMIT=100000; -const uint ARRAY_LIMIT=1000000; -const uint EXECUTE_RECURSION_LIMIT=1000; -const uint EXECUTE_RECURSION_RESERVE=100; -const uint HTTPD_TIMEOUT=4; -const size_t FILE_SIZE_LIMIT=512*1024*1024; +// One recursion level uses ~0.85KB of stack under linux/gcc, ~1KB under win32 and ~2KB under win64. +// MSVC win64 exception handling requires an extra ~10KB !!! for each throw, and execute() catches and +// rethrows on every level to provide a stack trace. +// So 1000 levels need ~1MB under linux (default 8MB stack is enough) and ~2+10=12MB under win64. +// Worse, win64 keeps the unwound frames until the catch block returns, so a handler running away +// in its turn starts on top of them and needs one more such budget. The stack is set to 40MB in +// the MSVC project settings to allow nested three runaways. +const int EXECUTE_RECURSION_LIMIT=1000; +const int EXECUTE_RECURSION_RESERVE=100; + +const int LOOP_LIMIT=100000; +const int ARRAY_LIMIT=1000000; +const int HTTPD_TIMEOUT=4; +const size_t FILE_SIZE_LIMIT=512*0x400*0x400; // 512MB +const size_t POST_MAX_SIZE=10*0x400*0x400; // 10MB // defines for globals @@ -74,7 +84,8 @@ int pa_loop_limit=LOOP_LIMIT; int pa_array_limit=ARRAY_LIMIT; int pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT; int pa_httpd_timeout=HTTPD_TIMEOUT; -size_t pa_file_size_limit=FILE_SIZE_LIMIT; +THREAD_LOCAL size_t pa_file_size_limit=FILE_SIZE_LIMIT; +THREAD_LOCAL size_t pa_post_max_size=POST_MAX_SIZE; // defines for statics @@ -100,10 +111,15 @@ static const String loop_limit_name("max static const String array_limit_name("max_array_size"); static const String recursion_limit_name("max_recursion"); static const String file_size_limit_name("max_file_size"); +static const String post_max_size_name("post_max_size"); static const String lock_wait_timeout_name("lock_wait_timeout"); static const String httpd_name("HTTPD"); static const String httpd_timeout_name("timeout"); static const String httpd_mode_name("mode"); +static const String httpd_max_connections_name("max-connections"); +#ifdef CODE_CACHE +static const String httpd_code_cache_name("code-cache"); +#endif static const String conf_method_name("conf"); static const String post_process_method_name("postprocess"); @@ -139,6 +155,12 @@ void Request::recursion_limit_reached() throw Exception(PARSER_RUNTIME, 0, "call canceled - endless recursion detected"); } +void Request::throw_interrupted() { + finterrupted=0; + set_skip(SKIP_NOTHING); + throw Interrupted_exception(response.timeout); +} + Request::Request(SAPI_Info& asapi_info, Request_info& arequest_info, String::Language adefault_lang): // private anti_endless_execute_recursion(0), @@ -149,10 +171,10 @@ Request::Request(SAPI_Info& asapi_info, method_frame(0), rcontext(0), wcontext(0), - flang(adefault_lang), fconnection(0), fin_cycle(0), fskip(SKIP_NOTHING), + finterrupted(0), // public request_info(arequest_info), @@ -162,23 +184,23 @@ Request::Request(SAPI_Info& asapi_info, main_class(VClassMAIN_create()), form(*new VForm(charsets, arequest_info)), mail(*new VMail), - response(*new VResponse(arequest_info, charsets)), + response(*new VResponse(arequest_info, charsets, adefault_lang)), cookie(*new VCookie(charsets, arequest_info)), console(*new VConsole), + websocket(0), // private configure_admin_done(false), - // private defaults - fdefault_lang(adefault_lang), // private mime types mime_types(0) { - pa_register_thread_request(*this); - // file_no=0 => unknown file_list+=String::Body("UNKNOWN"); file_list+=String::Body("-body of process-"); // pseudo_file_no__process +#ifdef CODE_CACHE + Code_cache_manager::add_files(file_list); +#endif // maybe expire old caches cache_managers->maybe_expire(); @@ -215,9 +237,14 @@ Request::Request(SAPI_Info& asapi_info, put_class(new VMath); // memory class put_class(new VMemory); + + // the last: the destructor is not called if the constructor throws + thread_request=this; } Request::~Request() { + thread_request=0; + #ifdef XML // if for some strange reason xml generic errors failed to be reported, free them up if(const char* xml_generic_errors=xmlGenericErrors()) { @@ -297,95 +324,118 @@ void Request::configure_admin(VStateless } } + Value* conf=&conf_class; + #ifdef STRICT_VARS - VVoid::strict_vars=false; - if(Value* strict_vars=conf_class.get_element(strict_vars_name)) { - if(strict_vars->is_bool()) - VVoid::strict_vars=strict_vars->as_bool(); - else - throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", strict_vars_name.cstr()); - } + bool strict_vars=false; + CONF_OPTION(conf, strict_vars_name, { + strict_vars=option->as_bool(); + }, "%s must be bool"); + VVoid::strict_vars=strict_vars; #endif #ifdef OBJECT_PROTOTYPE - VClass::prototype=true; - if(Value* prototype=conf_class.get_element(prototype_name)) { - if(prototype->is_bool()) - VClass::prototype=prototype->as_bool(); - else - throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", prototype_name.cstr()); - } + bool prototype=true; + CONF_OPTION(conf, prototype_name, { + prototype=option->as_bool(); + }, "%s must be bool"); + VClass::prototype=prototype; #endif #ifdef CLASS_GETTER_UNPROTECTED - VClass::getter_protected=true; - if(Value* getter_protected=conf_class.get_element(getter_protected_name)) { - if(getter_protected->is_bool()) - VClass::getter_protected=getter_protected->as_bool(); - else - throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", getter_protected_name.cstr()); - } + bool getter_protected=true; + CONF_OPTION(conf, getter_protected_name, { + getter_protected=option->as_bool(); + }, "%s must be bool"); + VClass::getter_protected=getter_protected; // thread local: changes in Temp_getter_protected #endif - VStateless_class::gall_vars_local=false; - if(Value* locals=conf_class.get_element(locals_name)) { - if(locals->is_bool()){ - VStateless_class::gall_vars_local=locals->as_bool(); + bool all_vars_local=false; + CONF_OPTION(conf, locals_name, { + all_vars_local=option->as_bool(); + if(all_vars_local) main_class.set_all_vars_local(); - } else - throw Exception(PARSER_RUNTIME, 0, "$MAIN:%s must be bool", locals_name.cstr()); - } + }, "%s must be bool"); + VStateless_class::gall_vars_local=all_vars_local; Value* limits=conf_class.get_element(limits_name); - pa_loop_limit=LOOP_LIMIT; + int loop_limit=LOOP_LIMIT; CONF_OPTION(limits, loop_limit_name, { - pa_loop_limit=option->as_int(); - if(pa_loop_limit==0) pa_loop_limit=INT_MAX; + loop_limit=option->as_int(); + if(loop_limit==0) loop_limit=INT_MAX; }, "LIMITS.%s must be int"); + pa_loop_limit=loop_limit; - pa_array_limit=ARRAY_LIMIT; + int array_limit=ARRAY_LIMIT; CONF_OPTION(limits, array_limit_name, { - pa_array_limit=option->as_int(); - if(pa_array_limit==0) pa_array_limit=INT_MAX; + array_limit=option->as_int(); + if(array_limit==0) array_limit=INT_MAX; }, "LIMITS.%s must be int"); + pa_array_limit=array_limit; - pa_execute_recursion_limit=EXECUTE_RECURSION_LIMIT; + execute_recursion_limit=EXECUTE_RECURSION_LIMIT; CONF_OPTION(limits, recursion_limit_name, { - pa_execute_recursion_limit=option->as_int(); - if(pa_execute_recursion_limit<=0) pa_execute_recursion_limit=INT_MAX; - pa_execute_recursion_limit=min(pa_execute_recursion_limit, INT_MAX-EXECUTE_RECURSION_RESERVE); + execute_recursion_limit=option->as_int(); + if(execute_recursion_limit<=0) execute_recursion_limit=INT_MAX; + execute_recursion_limit=min(execute_recursion_limit, INT_MAX-EXECUTE_RECURSION_RESERVE); }, "LIMITS.%s must be int"); - execute_recursion_limit=pa_execute_recursion_limit; + pa_execute_recursion_limit=execute_recursion_limit; - pa_file_size_limit=FILE_SIZE_LIMIT; + size_t file_size_limit=FILE_SIZE_LIMIT; CONF_OPTION(limits, file_size_limit_name, { double limit=option->as_double(); if(limit >= (double)SSIZE_MAX) - throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %.15g", file_size_limit_name.cstr(), (double)SSIZE_MAX); - pa_file_size_limit=(size_t)limit; - if(pa_file_size_limit==0) - pa_file_size_limit=SSIZE_MAX; + throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %s", file_size_limit_name.cstr(), pa_itoa(SSIZE_MAX)); + file_size_limit=(size_t)limit; + if(file_size_limit==0) + file_size_limit=SSIZE_MAX; }, "LIMITS.%s must be number"); + pa_file_size_limit=file_size_limit; // thread local: url-dependent - pa_lock_attempts=PA_LOCK_ATTEMPTS; + size_t post_max_size=POST_MAX_SIZE; + CONF_OPTION(limits, post_max_size_name, { + double limit=option->as_double(); + if(limit >= (double)SSIZE_MAX) + throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %s", post_max_size_name.cstr(), pa_itoa(SSIZE_MAX)); + if(limit > 0) + post_max_size=(size_t)limit; + }, "LIMITS.%s must be number"); + pa_post_max_size=post_max_size; // thread local: url-dependent + + unsigned int lock_attempts=PA_LOCK_ATTEMPTS; CONF_OPTION(limits, lock_wait_timeout_name, { double limit=option->as_double(); if(limit >= 3600*24) throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %d", lock_wait_timeout_name.cstr(), 3600*24); - pa_lock_attempts=(unsigned int)(limit*2)+1; + lock_attempts=(unsigned int)(limit*2)+1; }, "LIMITS.%s must be number"); + pa_lock_attempts=lock_attempts; Value* httpd=conf_class.get_element(httpd_name); - pa_httpd_timeout=HTTPD_TIMEOUT; +#ifdef CODE_CACHE + CONF_OPTION(httpd, httpd_code_cache_name, { + Code_cache_manager::set_enabled(option->as_bool()); + }, "HTTPD.%s must be bool"); +#endif + + int httpd_timeout=HTTPD_TIMEOUT; CONF_OPTION(httpd, httpd_timeout_name, { - pa_httpd_timeout=option->as_int(); - if(pa_httpd_timeout==0) pa_httpd_timeout=INT_MAX; + httpd_timeout=option->as_int(); + if(httpd_timeout==0) httpd_timeout=INT_MAX; + }, "HTTPD.%s must be int"); + pa_httpd_timeout=httpd_timeout; + + int httpd_max_connections=INT_MAX; + CONF_OPTION(httpd, httpd_max_connections_name, { + httpd_max_connections=option->as_int(); + if(httpd_max_connections<=0) httpd_max_connections=INT_MAX; }, "HTTPD.%s must be int"); + HTTPD_Server::max_connections=httpd_max_connections; if(httpd) - if(Value* option=httpd->get_element( httpd_mode_name)) { + if(Value* option=httpd->get_element(httpd_mode_name)) { if(option->get_junction()) throw Exception(PARSER_RUNTIME, 0, "$MAIN:HTTPD:mode must be string"); HTTPD_Server::set_mode(option->as_string()); @@ -408,7 +458,7 @@ const char* Request::get_exception_cstr( #define PA_SOURCE_VALUE details.problem_source->cstr(), #define EXCEPTION_CSTR(f1,v1,f2,v2) \ - snprintf(result, MAX_STRING, \ + pa_snprintf(result, MAX_STRING, \ PA_URI_FORMAT \ f1 f2 \ PA_COMMENT_TYPE_FORMAT, \ @@ -465,11 +515,11 @@ void Request::configure() { // configure admin options if not configured yet if(!configure_admin_done) configure_admin(main_class); +} - // $MAIN:MIME-TYPES - if(Value* element=main_class.get_element(mime_types_name)) - if(Table *table=element->get_table()) - mime_types=table; +/// HEAD request: only the headers are sent +static bool is_header_only(Request_info& info) { + return info.method && strcasecmp(info.method, "HEAD")==0; } /** @@ -483,11 +533,12 @@ void Request::configure() { @test log stack trace */ -void Request::core(const char* config_filespec, bool header_only, const String &amain_method_name, const String* amain_class_name) { +void Request::core(const char* config_filespec, const String &amain_method_name, const String* amain_class_name) { + bool header_only=is_header_only(request_info); try { // loading config if(config_filespec) - use_file_directly(*new String(config_filespec)); + use_file_directly(*new String(backslashes_to_slashes(config_filespec))); // filling mail received mail.fill_received(*this); @@ -508,6 +559,12 @@ void Request::core(const char* config_fi if(!body_string) throw Exception(PARSER_RUNTIME, &amain_method_name, "method not found in class %s", main.type()); + if(response.started==VResponse::WEBSOCKET) + websocket->close(); // graceful: waits for the Close of the client + // ignore the result in websocket or stream mode + if(response.started) + return; + // extract response body Value* body_value=response.fields().get(download_name_upper); // $response:download? bool as_attachment=body_value!=0; @@ -526,7 +583,7 @@ void Request::core(const char* config_fi } // can throw exceptions while handling $response:download[] - output_result(body_value->as_vfile(flang, &charsets), header_only, as_attachment); + output_result(body_value->as_vfile(response.lang, &charsets), header_only, as_attachment); } catch(const Exception& e) { // request handling problem // OK, as native execution stack has unwound @@ -536,10 +593,9 @@ void Request::core(const char* config_fi VException& details=*new VException(*this, e); const char* exception_cstr=get_exception_cstr(e, details); - // reset language to default - flang=fdefault_lang; // reset response response.fields().clear(); + response.lang=response.default_lang; SAPI::clear_headers(sapi_info); // this is what we'd return in $response:body @@ -572,14 +628,15 @@ void Request::core(const char* config_fi // logging both initial and new exceptions SAPI::log(sapi_info, "%s", exception_cstr); SAPI::log(sapi_info, "Exception in @unhandled_exception at %s", get_exception_cstr(e, details)); - SAPI::send_error(sapi_info, "Exception in @unhandled_exception, details are available in Parser error log."); + if(!response.started) + SAPI::send_error(sapi_info, "Exception in @unhandled_exception, details are available in Parser error log."); return; } if(body_string) { // could report an error beautifully? VString body_vstring(*body_string); - output_result(body_vstring.as_vfile(flang, &charsets), header_only, false); - } else { + output_result(body_vstring.as_vfile(response.lang, &charsets), header_only, false); + } else if(!response.started) { // doing that ugly SAPI::send_error(sapi_info, exception_cstr, strcmp(e.type(), "file.missing") ? "500" : "404"); } @@ -601,7 +658,7 @@ void Request::use_file_directly(const St return; if(with_auto_p) { - // loading auto.p files from document_root/.. + // loading auto.p files from document_root/.. // to the one beside requested file. // all assigned bases from upper dir const char* target=file_spec.cstr(); @@ -610,12 +667,9 @@ void Request::use_file_directly(const St request_info.path_translated=target; const char* after=target; - size_t drlen=strlen(request_info.document_root); - if(memcmp(after, request_info.document_root, drlen)==0) { - after+=drlen; - if(after[-1]=='/') - --after; - } + size_t drlen=strlen(request_info.document_root_path); + if(path_cmp(after, request_info.document_root_path, drlen)==0) + after+=drlen; // to start from document_root/auto.p while(const char* before=strchr(after, '/')) { String& sfile_spec=*new String; @@ -629,21 +683,32 @@ void Request::use_file_directly(const St } } + Code_cache* code=0; +#ifdef CODE_CACHE + if(Code_cache* cached=Code_cache_manager::get(file_spec)) { + use_buf(main_class, 0, 0, cached->file_no, 0, cached); + return; + } + // to record the compilation; only while the httpd config runs + code=Code_cache_manager::create(file_spec); +#endif + if(const char* source=file_read_text(charsets, file_spec, true)) - use_buf(main_class, source, 0, register_file(file_spec)); + use_buf(main_class, source, 0, register_file(file_spec), 0, code); } -void Request::use_file(const String& file_name, const String* use_filespec/*absolute*/, bool with_auto_p) { - if(file_name.is_empty()) +void Request::use_file(const String& afile_name, const String* use_filespec/*absolute*/, bool with_auto_p) { + if(afile_name.is_empty()) throw Exception(PARSER_RUNTIME, 0, "usage failed - no filename was specified"); const String* filespec=0; + const String& file_name=backslashes_to_slashes(afile_name); - if(file_name.first_char()=='/') //absolute path? [no need to scan MAIN:CLASS_PATH] - filespec=&full_disk_path(file_name); + if(file_name.first_char()=='/' || path_scheme(afile_name) || is_os_absolute_path(afile_name)) // path is not relative, no need to scan MAIN:CLASS_PATH + filespec=&full_disk_path(afile_name); else if(use_filespec){ // search in current dir first - size_t last_slash_pos=use_filespec->strrpbrk("/"); + size_t last_slash_pos=use_filespec->strrchr('/'); if(last_slash_pos!=STRING_NOT_FOUND) filespec=file_exist(use_filespec->mid(0, last_slash_pos), file_name); // found in current dir? } @@ -693,14 +758,18 @@ void Request::use_file(const String& fil } } -void Request::use_buf(VStateless_class& aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset) { +void Request::use_buf(VStateless_class& aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset, Code_cache* code) { // temporary zero @conf to avoid it second execution Temp_method temp_method_conf(aclass, conf_method_name, 0); // temporary zero @auto to avoid it second execution Temp_method temp_method_auto(aclass, auto_method_name, 0); // compile loaded classes - ArrayClass& cclasses=compile(&aclass, source, main_alias, file_no, line_no_offset); + ArrayClass& cclasses= +#ifdef CODE_CACHE + !source ? code->replay(*this, aclass) : +#endif + compile(&aclass, source, main_alias, file_no, line_no_offset, code); VString* vfilespec= new VString(file_list[file_no]); @@ -720,6 +789,7 @@ void Request::use_buf(VStateless_class& } const String& Request::relative(const char* apath, const String& relative_name) { + // apath is a URI or a path already normalized by the target, always slash separated char *hpath=pa_strdup(apath); String& result=*new String; if(rsplit(hpath, '/')) // if something/splitted @@ -728,18 +798,54 @@ const String& Request::relative(const ch return result; } -const String& Request::full_disk_path(const String& relative_name) { - if(is_os_absolute_path(relative_name)) - return relative_name; - if(relative_name.first_char()=='/') { - String& result=*new String(pa_strdup(request_info.document_root)); - result << relative_name; - return result; +void Request_info::set_document_root(const char* adocument_root) { + document_root=adocument_root; + char* path=backslashes_to_slashes(adocument_root); + if(path) { + size_t length=strlen(path); + while(length && path[length-1]=='/') + path[--length]=0; + } + document_root_path=path; +} + +const String& Request::full_disk_path(const String& relative_name, uint allowed) { + if(relative_name.first_char()!='/') { + if(uint scheme=path_scheme(relative_name)) { + check_path_scheme(scheme, relative_name, allowed); + if(scheme!=PA_SCHEME_FILE) + return relative_name; // an allowed uri is passed through + if(const String* path=file_uri_to_path(relative_name)) + return backslashes_to_slashes(*path); +#ifdef WIN32 + throw Exception(PARSER_RUNTIME, &relative_name, "must be file:///path, file://localhost/path or file://server/share/path"); +#else + throw Exception(PARSER_RUNTIME, &relative_name, "must be file:///path or file://localhost/path"); +#endif + } + // before '\' becomes '/', to avoid mixing the UNC path \\server with web path //server + if(is_os_absolute_path(relative_name)) + return backslashes_to_slashes(relative_name); } - if(relative_name.starts_with("http://") || relative_name.starts_with("parser://")) - return relative_name; - return relative(request_info.path_translated ? request_info.path_translated : request_info.document_root, relative_name); + const String& path=backslashes_to_slashes(relative_name); + if(path.first_char()!='/') + return relative(request_info.path_translated ? request_info.path_translated : pa_strcat(request_info.document_root_path, "/"), path); + + String& result=*new String(request_info.document_root_path); + result << path; + return result; +} + +const String* Request::real_disk_path(const String& name) { + switch(path_scheme(name)) { + case PA_SCHEME_NONE: return &full_disk_path(name); + case PA_SCHEME_FILE: { + const String* path=clean_path_scheme(PA_SCHEME_FILE, name) ? file_uri_to_path(name) : 0; + return path ? &backslashes_to_slashes(*path) : 0; + } + default: return 0; // not a disk path + } } #ifndef DOXYGEN @@ -898,7 +1004,7 @@ static void output_pieces(Request& r, bo part_length = rg.end-rg.start+1; char buf[MAX_STRING]; - snprintf(buf, MAX_STRING, "bytes %.15g-%.15g/%.15g", (double)rg.start, (double)rg.end, (double)content_length); + pa_snprintf(buf, MAX_STRING, "bytes %s-%s/%s", pa_uitoa(rg.start), pa_uitoa(rg.end), pa_uitoa(content_length)); SAPI::add_header_attribute(r.sapi_info, HTTP_STATUS, "206"); SAPI::add_header_attribute(r.sapi_info, "content-range", buf); } else { @@ -920,6 +1026,9 @@ static void output_pieces(Request& r, bo } void Request::output_result(VFile* body_file, bool header_only, bool as_attachment) { + if(response.started) + return; + // header: cookies cookie.output_result(sapi_info); @@ -992,22 +1101,58 @@ void Request::output_result(VFile* body_ } } +void Request::start(VResponse::Started mode) { + // the headers: cookies and $response:fields, without Content-Length + // prepare before response.started is set: after that an exception is not reported to the client + cookie.output_result(sapi_info); + Add_header_attribute_info info(*this); + response.fields().for_each(add_header_attribute, &info); + response.started=mode; + SAPI::send_headers(sapi_info); +} + +void Request::stream(Value& part) { + if(response.started==VResponse::WEBSOCKET) + throw Exception("websocket", 0, "unable to stream: websocket is active"); + + // convert before response.started is set: after that an exception is not reported to the client + VFile* file=dynamic_cast(&part); + String::C output=file ? String::C(file->value_ptr(), file->value_size()) : String::C(part.get_string()->cstr_to_string_body_untaint(response.lang, 0, &charsets)); + if(!file || file->is_text_mode()) + output=Charset::transcode(output, charsets.source(), charsets.client()); + + if(!response.started) { + response.fields().put_dont_replace(content_type_name_upper, new VString(*new String(DEFAULT_CONTENT_TYPE))); + start(VResponse::STREAM); + } + + if(!is_header_only(request_info)) + SAPI::send_body(sapi_info, output.str, output.length); +} + const String& Request::mime_type_of(const String* file_name) { return mime_type_of(file_name?file_name->taint_cstr(String::L_FILE_SPEC):0); } const String& Request::mime_type_of(const char* user_file_name_cstr) { - if(mime_types) - if(const char* cext=strrchr(user_file_name_cstr, '.')) { - String sext(++cext); - Table::Action_options options; - if(mime_types->locate(0, sext.change_case(charsets.source(), String::CC_LOWER), options)) { - if(const String* result=mime_types->item(1)) - return *result; - else - throw Exception(PARSER_RUNTIME, 0, MIME_TYPES_NAME " table column elements must not be empty"); - } + // built at the first use: $MAIN:MIME-TYPES (table or @GET_MIME-TYPES[] property) + if(!mime_types) { + Value* element=main_class.get_element(mime_types_name); + TEMP_GETTER_PROTECTED(false); // @GET_MIME-TYPES[] assigns $MIME-TYPES, which replaces the getter with the table + Table *table=element ? process(*element).get_table() : 0; + mime_types=table ? table : new Table(0); // empty if nothing is defined + } + + if(const char* cext=strrchr(user_file_name_cstr, '.')) { + String sext(++cext); + Table::Action_options options; + if(mime_types->locate(0, sext.change_case(charsets.source(), String::CC_LOWER), options)) { + if(const String* result=mime_types->item(1)) + return *result; + else + throw Exception(PARSER_RUNTIME, 0, MIME_TYPES_NAME " table column elements must not be empty"); } + } return *new String("application/octet-stream"); }