--- parser3/src/targets/cgi/parser3.C 2024/11/09 17:02:27 1.359 +++ parser3/src/targets/cgi/parser3.C 2024/12/02 02:50:40 1.362 @@ -5,7 +5,7 @@ Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.359 2024/11/09 17:02:27 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.362 2024/12/02 02:50:40 moko Exp $"; #include "pa_config_includes.h" @@ -34,7 +34,7 @@ extern "C" int GC_pthread_create(pthread // defines // comment remove me after debugging -//#define PA_DEBUG_CGI_ENTRY_EXIT "parser3-debug.log" +//#define PA_DEBUG_CGI_ENTRY_EXIT #if defined(_MSC_VER) && !defined(_DEBUG) # define PA_SUPPRESS_SYSTEM_EXCEPTION @@ -55,6 +55,7 @@ static THREAD_LOCAL SAPI_Info *sapi_info 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 +static const char* log_filespec = 0; // -l option static bool mail_received = false; // -m option? [asked to parse incoming message to $mail:received] static const char* parser3_filespec = 0; // argv[0] static char** argv_extra = NULL; @@ -75,22 +76,20 @@ template static T *dir_pos( // SAPI static void pa_log(const char* fmt, va_list args) { - bool opened=false; FILE *f=0; - const char* log_by_env=getenv(PARSER_LOG_ENV_NAME); - if(!log_by_env) - log_by_env=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME); - if(log_by_env) { - f=fopen(log_by_env, "at"); - opened=f!=0; + if(log_filespec) + f=fopen(log_filespec, "at"); + + if(!f) { + const char* log_by_env=getenv(PARSER_LOG_ENV_NAME); + if(!log_by_env) + log_by_env=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME); + if(log_by_env) + f=fopen(log_by_env, "at"); } -#ifdef PA_DEBUG_CGI_ENTRY_EXIT - f=fopen(PA_DEBUG_CGI_ENTRY_EXIT, "at"); - opened=f!=0; -#endif - if(!opened && filespec_4log) { + if(!f && filespec_4log) { char log_spec[MAX_STRING + 12 /* '/parser3.log' */]; pa_strncpy(log_spec, filespec_4log, MAX_STRING); @@ -102,10 +101,9 @@ static void pa_log(const char* fmt, va_l } f=fopen(log_spec, "at"); - opened=f!=0; } // fallback to stderr - if(!opened) + if(!f) f=stderr; // use no memory [so that we could log out-of-memory error] @@ -129,7 +127,7 @@ static void pa_log(const char* fmt, va_l } else fputs(" [no request info]\n", f); - if(opened) + if(f!=stderr) fclose(f); else fflush(f); @@ -192,10 +190,14 @@ void SAPI::add_header_attribute(SAPI_Inf info.add_header(dont_store_key, dont_store_value); } -void SAPI::send_header(SAPI_Info& info) { +void SAPI::send_headers(SAPI_Info& info) { info.send_headers(); } +void SAPI::clear_headers(SAPI_Info& info) { + info.clear_headers(); +} + size_t SAPI::send_body(SAPI_Info& info, const void *buf, size_t size) { return info.send_body(buf, size); } @@ -384,7 +386,7 @@ static void connection_handler(SAPI_Info Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)); // process the request r.core(config_filespec, strcasecmp(request_info.method, "HEAD")==0, main_method_name, &httpd_class_name); - } catch(const Exception& e) { // exception in connection handling or unhandled exception + } catch(const Exception& e) { // exception in connection handling SAPI::log(info, "%s", e.comment()); const char* status = info.exception_http_status(e.type()); if(*status) @@ -553,7 +555,7 @@ static void real_parser_handler(bool cgi request_info.argv = argv_extra; #ifdef PA_DEBUG_CGI_ENTRY_EXIT - log("request_info: method=%s, uri=%s, q=%s, dr=%s, pt=%s", request_info.method, request_info.uri, request_info.query_string, request_info.document_root, request_info.path_translated); + pa_log("request_info: method=%s, uri=%s, q=%s, dr=%s, pt=%s", request_info.method, request_info.uri, request_info.query_string, request_info.document_root, request_info.path_translated); #endif // prepare to process request @@ -628,7 +630,7 @@ static void usage(const char* program) { int main(int argc, char *argv[]) { #ifdef PA_DEBUG_CGI_ENTRY_EXIT - log("main: entry"); + pa_log("main: entry"); #endif parser3_filespec = argc && argv[0] ? MAYBE_BACK_SLASHES_TO_SLASHES(argv[0]) : "parser3"; @@ -666,6 +668,12 @@ int main(int argc, char *argv[]) { config_filespec=argv[optind]; } break; + case 'l': + if(optind < argc - 1){ + optind++; + log_filespec=argv[optind]; + } + break; case 'p': if(optind < argc - 1){ optind++; @@ -729,13 +737,13 @@ int main(int argc, char *argv[]) { } REAL_PARSER_HANDLER(cgi); - } catch(const Exception& e) { // exception in unhandled exception + } catch(const Exception& e) { // exception in config_handler 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"); + pa_log("main: successful return"); #endif return sapi_info->http_response_code < 100 ? sapi_info->http_response_code : 0; }