--- parser3/src/targets/cgi/parser3.C 2024/08/26 20:47:06 1.355 +++ parser3/src/targets/cgi/parser3.C 2024/11/10 00:28:42 1.360 @@ -1,11 +1,11 @@ /** @file Parser: scripting and CGI main. - Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.355 2024/08/26 20:47:06 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.360 2024/11/10 00:28:42 moko Exp $"; #include "pa_config_includes.h" @@ -46,6 +46,12 @@ extern "C" int GC_pthread_create(pthread #define PARSER_CONFIG_ENV_NAME "CGI_PARSER_CONFIG" #define PARSER_LOG_ENV_NAME "CGI_PARSER_LOG" +SAPI_Info sapi_console; +SAPI_Info_CGI sapi_cgi; + +static SAPI_Info *sapi_info = &sapi_cgi; +static THREAD_LOCAL SAPI_Info *sapi_info_4log = NULL; // global for correct send error in die() + static const char* filespec_to_process = 0; // [file] static const char* httpd_host_port = 0; // -p option static const char* config_filespec = 0; // -f option or from env or next to the executable if exists @@ -157,11 +163,15 @@ void SAPI::die(const char* fmt, ...) { char message[MAX_STRING]; vsnprintf(message, MAX_STRING, fmt, args); - SAPI::send_error(*sapiInfo, message); + SAPI::send_error(sapi_info_4log ? *sapi_info_4log : *sapi_info, message); exit(1); // va_end(args); } +void SAPI::send_error(SAPI_Info& info, const char *exception_cstr, const char *status){ + info.send_error(exception_cstr, status); +} + char* SAPI::Env::get(SAPI_Info& info, const char* name) { return info.get_env(name); } @@ -179,11 +189,15 @@ size_t SAPI::read_post(SAPI_Info& info, } void SAPI::add_header_attribute(SAPI_Info& info, const char* dont_store_key, const char* dont_store_value) { - info.add_header_attribute(dont_store_key, dont_store_value); + info.add_header(dont_store_key, dont_store_value); } -void SAPI::send_header(SAPI_Info& info) { - info.send_header(); +void SAPI::send_headers(SAPI_Info& info) { + info.send_headers(); +} + +void SAPI::clear_headers(SAPI_Info& info) { + info.headers.clear(); } size_t SAPI::send_body(SAPI_Info& info, const void *buf, size_t size) { @@ -209,7 +223,7 @@ static const char* full_disk_path(const } static void log_signal(const char* signal_name) { - SAPI::log(*sapiInfo, "%s received %s processing request", signal_name, request ? "while" : "before or after"); + pa_log("%s received %s processing request", signal_name, request ? "while" : "before or after"); } #ifdef SIGPIPE @@ -321,11 +335,13 @@ public: class RequestInfoController { public: - RequestInfoController(Request_info* rinfo){ + RequestInfoController(Request_info* rinfo, SAPI_Info* sinfo){ request_info_4log=rinfo; + sapi_info_4log=sinfo; } ~RequestInfoController(){ request_info_4log=0; + sapi_info_4log=0; } }; @@ -334,7 +350,7 @@ static const String httpd_class_name("ht static void config_handler(SAPI_Info &info) { Request_info request_info; - RequestInfoController ric(&request_info); + RequestInfoController ric(&request_info, &info); request_info.document_root = full_disk_path(); request_info.uri = ""; @@ -350,7 +366,7 @@ static void config_handler(SAPI_Info &in static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection) { Request_info request_info; - RequestInfoController ric(&request_info); + RequestInfoController ric(&request_info, &info); try { if(!connection.read_header()) @@ -375,10 +391,8 @@ static void connection_handler(SAPI_Info } catch(const Exception& e) { // exception in connection handling or unhandled exception SAPI::log(info, "%s", e.comment()); const char* status = info.exception_http_status(e.type()); - if(*status){ - info.clear_response_headers(); + if(*status) SAPI::send_error(info, e.comment(), status); - } } } @@ -393,7 +407,7 @@ static void *connection_thread(void *arg try { connection_handler(info, connection); } catch(const Exception& e) { // exception in send_error - SAPI::log(*sapiInfo, "%s", e.comment()); + pa_log("%s", e.comment()); } delete(&connection); @@ -401,7 +415,7 @@ static void *connection_thread(void *arg } static void httpd_mode() { - config_handler(*sapiInfo); + config_handler(*sapi_info); SOCKET sock = HTTPD_Server::bind(httpd_host_port); @@ -453,7 +467,7 @@ static void httpd_mode() { } // closing connection socket in HTTPD_Connection destructor } catch(const Exception& e) { // exception in accept or send_error - SAPI::log(*sapiInfo, "%s", e.comment()); + pa_log("%s", e.comment()); } #ifndef _MSC_VER @@ -480,7 +494,7 @@ static void real_parser_handler(bool cgi // global request info Request_info request_info; - RequestInfoController ric(&request_info); + RequestInfoController ric(&request_info, sapi_info); request_info.path_translated = filespec_to_process; request_info.method = request_method ? request_method : "GET"; @@ -547,7 +561,7 @@ static void real_parser_handler(bool cgi #endif // prepare to process request - Request r(*sapiInfo, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS); + Request r(*sapi_info, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS); { // initing ::request ptr for signal handlers RequestController rc(&r); @@ -599,7 +613,7 @@ static void call_real_parser_handler__su static void usage(const char* program) { printf( "Parser/%s\n" - "Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)\n" + "Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)\n" "Authors: Konstantin Morshnev , Alexandr Petrosian \n" "\n" "Usage: %s [options] [file]\n" @@ -626,7 +640,8 @@ int main(int argc, char *argv[]) { // were we started as CGI? bool cgi=(getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) && !getenv("PARSER_VERSION"); - sapiInfo = cgi ? new SAPI_Info_CGI() : new SAPI_Info(); + if(!cgi) + sapi_info = &sapi_console; #ifdef SIGPIPE signal(SIGPIPE, SIGPIPE_handler); @@ -719,11 +734,12 @@ int main(int argc, char *argv[]) { REAL_PARSER_HANDLER(cgi); } catch(const Exception& e) { // exception in unhandled exception - SAPI::die("%s", e.comment()); + SAPI::log(*sapi_info, "%s", e.comment()); + SAPI::send_error(*sapi_info, e.comment(), strcmp(e.type(), "file.missing") ? "500" : "404"); } #ifdef PA_DEBUG_CGI_ENTRY_EXIT log("main: successful return"); #endif - return sapiInfo->http_response_code < 100 ? sapiInfo->http_response_code : 0; + return sapi_info->http_response_code < 100 ? sapi_info->http_response_code : 0; }