--- parser3/src/targets/cgi/parser3.C 2020/10/10 06:08:37 1.294 +++ parser3/src/targets/cgi/parser3.C 2020/11/10 22:32:13 1.307 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.294 2020/10/10 06:08:37 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.307 2020/11/10 22:32:13 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); } @@ -160,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(); } @@ -299,52 +297,56 @@ static bool locate_config(){ static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection, const char* filespec_to_process){ 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)); request_info.document_root = document_root_buf; request_info.path_translated = filespec_to_process; request_info.method = connection.method(); - request_info.query_string=NULL; - request_info.uri=request_info.strip_absolute_uri(connection.uri()); - request_info.content_type=connection.content_type(); - request_info.content_length=connection.content_length(); - request_info.cookie=info.get_env("HTTP_COOKIE"); - request_info.mail_received=false; + request_info.query_string = connection.query(); + request_info.uri = request_info.strip_absolute_uri(connection.uri()); + request_info.content_type = connection.content_type(); + request_info.content_length = connection.content_length(); + request_info.cookie = info.get_env("HTTP_COOKIE"); + request_info.mail_received = false; request_info.argv = argv_all + args_skip; // 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 + // process the request, we need @httpd-main in auto.p if filespec_to_process not specified + request.core(locate_config() || !filespec_to_process ? config_filespec_cstr : NULL, strcasecmp(request_info.method, "HEAD")==0); + // 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); - 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); + while(1){ + 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); } } @@ -354,7 +356,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); } @@ -363,10 +365,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")); @@ -430,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; @@ -440,7 +441,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, @@ -453,12 +454,11 @@ 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 + 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 @@ -507,13 +507,13 @@ static void usage(const char* program) { "Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)\n" "Author: Alexandr Petrosian (http://paf.design.ru)\n" "\n" - "Usage: %s [options] file\n" + "Usage: %s [options] [file]\n" "Options are:\n" #ifdef WITH_MAILRECEIVE " -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); @@ -542,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) @@ -569,7 +569,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 @@ -587,7 +587,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]); } @@ -596,7 +596,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]); } @@ -628,11 +628,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; }