--- parser3/src/targets/cgi/parser3.C 2020/10/12 20:57:08 1.298 +++ parser3/src/targets/cgi/parser3.C 2020/10/28 22:32:02 1.306 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.298 2020/10/12 20:57:08 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.306 2020/10/28 22:32:02 moko Exp $"; #include "pa_config_includes.h" @@ -105,7 +105,7 @@ static void log(const char* fmt, va_list fwrite(buf, size, 1, f); if(request_info.method) { - fprintf(f, " [uri=%s, method=%s, cl=%lu]\n", request_info.uri ? request_info.uri : "", request_info.method, request_info.content_length); + fprintf(f, " [uri=%s, method=%s, cl=%lu]\n", request_info.uri ? request_info.uri : "", request_info.method, (unsigned long)request_info.content_length); } else fputs(" [no request info]\n", f); @@ -142,10 +142,10 @@ void SAPI::die(const char* fmt, ...) { // inform user, second vsnprintf va_start(args, fmt); - char body[MAX_STRING]; - int content_length=vsnprintf(body, MAX_STRING, fmt, args); + char message[MAX_STRING]; + vsnprintf(message, MAX_STRING, fmt, args); - sapiInfo->die(body, content_length); + SAPI::send_error(*sapiInfo, message); exit(1); // va_end(args); } @@ -154,6 +154,10 @@ char* SAPI::Env::get(SAPI_Info& info, co return info.get_env(name); } +bool SAPI::Env::set(SAPI_Info& info, const char* name, const char* value) { + return info.set_env(name, value); +} + const char* const *SAPI::Env::get(SAPI_Info& info) { return info.get_env(); } @@ -295,8 +299,8 @@ static void connection_handler(SAPI_Info connection.read_header(); info.populate_env(); - // connection request info - Request_info request_info; memset(&request_info, 0, sizeof(request_info)); + // connection request info, still global for correct log() reporting + memset(&request_info, 0, sizeof(request_info)); char document_root_buf[MAX_STRING]; full_file_spec("", document_root_buf, sizeof(document_root_buf)); @@ -316,9 +320,8 @@ static void connection_handler(SAPI_Info { // initing ::request ptr for signal handlers RequestController rc(&request); - bool fail_on_config_read_problem=locate_config(); // process the request - request.core(config_filespec_cstr, fail_on_config_read_problem, strcasecmp(request_info.method, "HEAD")==0); + request.core(locate_config() ? config_filespec_cstr : NULL, strcasecmp(request_info.method, "HEAD")==0); // clearing ::request in RequestController desctructor to prevent signal handlers from accessing invalid memory } } @@ -327,19 +330,23 @@ static void httpd_mode(const char* files int sock = HTTPD_Server::bind(httpd_host_port); while(1){ - HTTPD_Connection *connection = HTTPD_Server::accept(sock, 5); - if(!connection) - continue; - - SAPI_Info_HTTPD info(*connection); - - try { // connection try - connection_handler(info, *connection, filespec_to_process); - } catch(const Exception& e) { // exception in unhandled exception -// info.die(e); + try { + HTTPD_Connection connection; + if(!connection.accept(sock, 5)) + continue; + + SAPI_Info_HTTPD info(connection); + + try { // connection try + connection_handler(info, connection, filespec_to_process); + } catch(const Exception& e) { // exception in connection handling or unhandled exception + SAPI::log(info, "%s", e.comment()); + SAPI::send_error(info, e.comment(), info.exception_http_status(e.type())); + } + // closing connection socket in HTTPD_Connection destructor + } catch(const Exception& e) { // exception in accept or send_error + SAPI::log(*sapiInfo, "%s", e.comment()); } - - close(connection->sock); } } @@ -424,7 +431,7 @@ static void real_parser_handler(const ch } request_info.content_type = getenv("CONTENT_TYPE"); - request_info.content_length = pa_atoui(getenv("CONTENT_LENGTH"), 10); + request_info.content_length = pa_atoul(getenv("CONTENT_LENGTH")); request_info.cookie = getenv("HTTP_COOKIE"); request_info.mail_received = mail_received; @@ -449,10 +456,9 @@ static void real_parser_handler(const ch { // initing ::request ptr for signal handlers RequestController rc(&request); - bool fail_on_config_read_problem=locate_config(); // process the request - request.core(config_filespec_cstr, fail_on_config_read_problem, strcasecmp(request_info.method, "HEAD")==0); - // clearing ::request in RequestController desctructor to prevent signal handlers from accessing invalid memory + request.core(locate_config() ? config_filespec_cstr : NULL, strcasecmp(request_info.method, "HEAD")==0); + // clearing ::request in RequestController destructor to prevent signal handlers from accessing invalid memory } // finalize libraries @@ -536,7 +542,7 @@ int main(int argc, char *argv[]) { SAPI::die("Can not set handler for SIGPIPE"); #endif - char *raw_filespec_to_process; + char *raw_filespec_to_process = NULL; if(cgi) { raw_filespec_to_process=getenv("PATH_TRANSLATED"); if(raw_filespec_to_process && !*raw_filespec_to_process) @@ -628,5 +634,5 @@ int main(int argc, char *argv[]) { #ifdef PA_DEBUG_CGI_ENTRY_EXIT log("main: successful return"); #endif - return sapiInfo && sapiInfo->http_response_code < 100 ? sapiInfo->http_response_code : 0; + return sapiInfo->http_response_code < 100 ? sapiInfo->http_response_code : 0; }