--- parser3/src/main/pa_request.C 2026/09/26 17:41:10 1.445 +++ parser3/src/main/pa_request.C 2026/09/30 17:45:25 1.455 @@ -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" @@ -36,7 +35,7 @@ #include "pa_vdate.h" #include "pa_varray.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.455 2026/09/30 17:45:25 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -45,11 +44,19 @@ 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; +// 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*1024*1024; // defines for globals @@ -362,7 +369,7 @@ void Request::configure_admin(VStateless 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); + throw Exception(PARSER_RUNTIME, 0, "$MAIN:LIMITS.%s must be less than %s", file_size_limit_name.cstr(), pa_itoa(SSIZE_MAX)); pa_file_size_limit=(size_t)limit; if(pa_file_size_limit==0) pa_file_size_limit=SSIZE_MAX; @@ -487,7 +494,7 @@ void Request::core(const char* config_fi 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); @@ -601,7 +608,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 +617,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(memcmp(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; @@ -634,16 +638,17 @@ void Request::use_file_directly(const St } -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? } @@ -720,6 +725,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 +734,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=file_uri_to_path(name); + return path ? &backslashes_to_slashes(*path) : 0; + } + default: return 0; // not a disk path + } } #ifndef DOXYGEN @@ -898,7 +940,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); + 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 {