--- parser3/src/main/pa_request.C 2026/10/05 17:50:18 1.458 +++ parser3/src/main/pa_request.C 2026/10/13 16:14:53 1.461 @@ -36,7 +36,7 @@ #include "pa_varray.h" #include "pa_code_cache.h" -volatile const char * IDENT_PA_REQUEST_C="$Id: pa_request.C,v 1.458 2026/10/05 17:50:18 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.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; // consts @@ -180,10 +180,9 @@ Request::Request(SAPI_Info& asapi_info, // private configure_admin_done(false), - // private defaults - fdefault_lang(adefault_lang), // private mime types - mime_types(0) + mime_types(0), + fheaders_sent(false) { pa_register_thread_request(*this); @@ -492,6 +491,11 @@ void Request::configure() { 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; +} + /** load MAIN class, execute @main. MAIN class consists of all the auto.p files we'd manage to find @@ -503,7 +507,8 @@ 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) @@ -528,6 +533,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) + return; + // extract response body Value* body_value=response.fields().get(download_name_upper); // $response:download? bool as_attachment=body_value!=0; @@ -556,8 +565,6 @@ 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(); SAPI::clear_headers(sapi_info); @@ -592,14 +599,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(!fheaders_sent) + 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 { + } else if(!fheaders_sent) { // doing that ugly SAPI::send_error(sapi_info, exception_cstr, strcmp(e.type(), "file.missing") ? "500" : "404"); } @@ -989,6 +997,9 @@ static void output_pieces(Request& r, bo } void Request::output_result(VFile* body_file, bool header_only, bool as_attachment) { + if(fheaders_sent) + return; + // header: cookies cookie.output_result(sapi_info); @@ -1061,6 +1072,27 @@ void Request::output_result(VFile* body_ } } +void Request::send(Value& part) { + // converted before the headers: a stat-ed file throws here + 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)); + 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); + 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); + } + + 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); }