--- parser3/src/main/pa_request.C 2026/10/13 16:14:53 1.461 +++ parser3/src/main/pa_request.C 2026/10/23 18:47:40 1.468 @@ -26,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" @@ -36,7 +37,7 @@ #include "pa_varray.h" #include "pa_code_cache.h" -volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.461 2026/10/13 16:14:53 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.468 2026/10/23 18:47:40 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -58,7 +59,8 @@ 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; +const size_t FILE_SIZE_LIMIT=512*0x400*0x400; // 512MB +const size_t POST_MAX_SIZE=10*0x400*0x400; // 10MB // defines for globals @@ -83,6 +85,7 @@ 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; +size_t pa_post_max_size=POST_MAX_SIZE; // defines for statics @@ -108,6 +111,7 @@ 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"); @@ -160,7 +164,6 @@ Request::Request(SAPI_Info& asapi_info, method_frame(0), rcontext(0), wcontext(0), - flang(adefault_lang), fconnection(0), fin_cycle(0), fskip(SKIP_NOTHING), @@ -173,16 +176,16 @@ 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 mime types - mime_types(0), - fheaders_sent(false) + mime_types(0) { pa_register_thread_request(*this); @@ -381,6 +384,15 @@ void Request::configure_admin(VStateless pa_file_size_limit=SSIZE_MAX; }, "LIMITS.%s must be number"); + pa_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) + pa_post_max_size=(size_t)limit; + }, "LIMITS.%s must be number"); + pa_lock_attempts=PA_LOCK_ATTEMPTS; CONF_OPTION(limits, lock_wait_timeout_name, { double limit=option->as_double(); @@ -427,7 +439,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, \ @@ -484,11 +496,6 @@ 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 @@ -533,8 +540,10 @@ 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()); - // ^response:send[] was used: only its parts are sent - if(fheaders_sent) + 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 @@ -555,7 +564,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 @@ -567,6 +576,7 @@ void Request::core(const char* config_fi // reset response response.fields().clear(); + response.lang=response.default_lang; SAPI::clear_headers(sapi_info); // this is what we'd return in $response:body @@ -599,15 +609,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)); - if(!fheaders_sent) + 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 if(!fheaders_sent) { + 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"); } @@ -975,7 +985,7 @@ static void output_pieces(Request& r, bo part_length = rg.end-rg.start+1; char buf[MAX_STRING]; - snprintf(buf, MAX_STRING, "bytes %s-%s/%s", pa_uitoa(rg.start), pa_uitoa(rg.end), pa_uitoa(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 { @@ -997,7 +1007,7 @@ static void output_pieces(Request& r, bo } void Request::output_result(VFile* body_file, bool header_only, bool as_attachment) { - if(fheaders_sent) + if(response.started) return; // header: cookies @@ -1072,21 +1082,29 @@ void Request::output_result(VFile* body_ } } -void Request::send(Value& part) { - // converted before the headers: a stat-ed file throws here +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(flang, 0, &charsets)); + 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(!fheaders_sent) { - fheaders_sent=true; - // the headers: cookies and $response:fields, without Content-Length - cookie.output_result(sapi_info); + if(!response.started) { response.fields().put_dont_replace(content_type_name_upper, new VString(*new String(DEFAULT_CONTENT_TYPE))); - Add_header_attribute_info info(*this); - response.fields().for_each(add_header_attribute, &info); - SAPI::send_headers(sapi_info); + start(VResponse::STREAM); } if(!is_header_only(request_info)) @@ -1098,17 +1116,24 @@ const String& Request::mime_type_of(cons } 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"); }