--- parser3/src/targets/cgi/parser3.C 2020/10/11 22:59:19 1.295 +++ parser3/src/targets/cgi/parser3.C 2020/10/14 00:14:45 1.302 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.295 2020/10/11 22:59:19 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.302 2020/10/14 00:14:45 moko Exp $"; #include "pa_config_includes.h" @@ -39,7 +39,7 @@ volatile const char * IDENT_PARSER3_C="$ #define PARSER_LOG_ENV_NAME "CGI_PARSER_LOG" static const char* config_filespec_cstr=0; // -f option -static const char* httpd_port=0; // -p option +static const char* httpd_host_port=0; // -p option static bool mail_received=false; // -m option? [asked to parse incoming message to $mail:received] static int args_skip=1; @@ -50,6 +50,8 @@ static bool cgi; ///< we were started as // for signal handlers Request *request=0; bool execution_canceled=false; +// for die error logging +Request_info request_info; // SAPI @@ -102,18 +104,10 @@ static void log(const char* fmt, va_list size=remove_crlf(buf, buf+size); fwrite(buf, size, 1, f); - if(request){ - Request_info& request_info = request->request_info; - fprintf(f, " [uri=%s, method=%s, cl=%lu]", - request_info.uri ? request_info.uri : "", - request_info.method ? request_info.method : "", - request_info.content_length); - } - else - fputs(" [no request info]", f); - - // newline - fputs("\n", f); + if(request_info.method) { + 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); if(opened) fclose(f); @@ -148,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); } @@ -301,8 +295,8 @@ static void connection_handler(SAPI_Info connection.read_header(); info.populate_env(); - // 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)); @@ -320,20 +314,20 @@ static void connection_handler(SAPI_Info // prepare to process request Request request(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)); { - // get ::request ptr for signal handlers + // 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 cleared in RequestController desctructor to prevent signal handlers from accessing invalid memory + // clearing ::request in RequestController desctructor to prevent signal handlers from accessing invalid memory } } static void httpd_mode(const char* filespec_to_process){ - int sock = HTTPD_Server::bind(/*"127.0.0.1"*/ NULL, pa_atoui(httpd_port, 10)); + int sock = HTTPD_Server::bind(httpd_host_port); - while(1 == 1){ - HTTPD_Connection *connection = HTTPD_Server::accept(sock,5); + while(1){ + HTTPD_Connection *connection = HTTPD_Server::accept(sock, 5); if(!connection) continue; @@ -341,11 +335,11 @@ static void httpd_mode(const char* files try { // connection try connection_handler(info, *connection, filespec_to_process); - } catch(const Exception& e) { // exception in unhandled exception -// info.die(e); + } 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())); } - - close(connection->sock); + connection->close(); } } @@ -355,7 +349,7 @@ static void real_parser_handler(const ch // init libraries pa_globals_init(); - if(httpd_port){ + if(httpd_host_port){ httpd_mode(filespec_to_process); } @@ -364,10 +358,9 @@ static void real_parser_handler(const ch if(!filespec_to_process || !*filespec_to_process) SAPI::die("Parser/%s", PARSER_VERSION); - // Request info - Request_info request_info; memset(&request_info, 0, sizeof(request_info)); char document_root_buf[MAX_STRING]; + // global request info request_info.path_translated = filespec_to_process; request_info.method = request_method ? request_method : "GET"; request_info.query_string = MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(getenv("QUERY_STRING")); @@ -441,7 +434,7 @@ static void real_parser_handler(const ch SAPI::die("Execution canceled"); #ifdef PA_DEBUG_CGI_ENTRY_EXIT - log("request_info: method=%s, uri=%s, q=%s, dr=%s, pt=%s, cookies=%s, cl=%u", + log("request_info: method=%s, uri=%s, q=%s, dr=%s, pt=%s, cookies=%s, cl=%u", request_info.method, request_info.uri, request_info.query_string, @@ -454,12 +447,12 @@ static void real_parser_handler(const ch // prepare to process request Request request(*sapiInfo, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS); { - // get ::request ptr for signal handlers + // 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 cleared in RequestController desctructor to prevent signal handlers from accessing invalid memory + // clearing ::request in RequestController desctructor to prevent signal handlers from accessing invalid memory } // finalize libraries @@ -514,7 +507,7 @@ static void usage(const char* program) { " -m Parse mail, put received letter to $mail:received\n" #endif " -f config_file Use this config file (/path/to/auto.p)\n" - " -p port Start web server on this port\n" + " -p [host:]port Start web server on this port\n" " -h Display usage information (this message)\n", PARSER_VERSION, program); @@ -543,7 +536,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) @@ -570,7 +563,7 @@ int main(int argc, char *argv[]) { case 'p': if(optind < argc - 1){ optind++; - httpd_port=argv[optind]; + httpd_host_port=argv[optind]; } break; #ifdef WITH_MAILRECEIVE @@ -588,7 +581,7 @@ int main(int argc, char *argv[]) { } if (optind > argc - 1) { - if(!httpd_port) { + if(!httpd_host_port) { fprintf(stderr, "%s: file not specified\n", argv[0]); usage(argv[0]); } @@ -597,7 +590,7 @@ int main(int argc, char *argv[]) { } args_skip=optind; - if (httpd_port && mail_received) { + if (httpd_host_port && mail_received) { fprintf(stderr, "%s: -p and -m options should not be used together\n", argv[0]); usage(argv[0]); } @@ -629,11 +622,11 @@ int main(int argc, char *argv[]) { try { // global try REAL_PARSER_HANDLER(filespec_to_process); } catch(const Exception& e) { // exception in unhandled exception - SAPI::die("Unhandled exception %s", e.comment()); + SAPI::die("%s", e.comment()); } #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; }