--- parser3/src/main/pa_request.C 2026/10/05 17:50:18 1.458 +++ parser3/src/main/pa_request.C 2026/10/13 20:02:00 1.462 @@ -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.462 2026/10/13 20:02:00 moko Exp $" IDENT_PA_REQUEST_H IDENT_PA_REQUEST_CHARSETS_H IDENT_PA_REQUEST_INFO_H IDENT_PA_VCONSOLE_H; // consts @@ -160,7 +160,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,17 +172,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), // 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 +490,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 +506,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 +532,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; @@ -546,7 +554,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 @@ -556,10 +564,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 @@ -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 { + output_result(body_vstring.as_vfile(response.lang, &charsets), header_only, false); + } 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(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); + 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); }