--- parser3/src/targets/cgi/parser3.C 2024/08/25 16:24:03 1.352 +++ parser3/src/targets/cgi/parser3.C 2024/12/11 18:35:31 1.366 @@ -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.352 2024/08/25 16:24:03 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.366 2024/12/11 18:35:31 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 @@ -46,9 +46,17 @@ 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() + +const char* parser3_mode = "cgi"; // $status:mode 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; @@ -58,52 +66,47 @@ static THREAD_LOCAL Request_info *reques static const char* filespec_4log = NULL; // null only if system-wide auto.p used template static T *dir_pos(T *fname){ - T *pos=fname; T *result=NULL; - while (pos=strpbrk(pos, "/\\")){ - result=pos; - pos++; + while (fname=strpbrk(fname, "/\\")){ + result=fname; + fname++; } - return !result || (result==fname+1 && *fname=='.') ? NULL : result; + return result; } -// 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; - } -#ifdef PA_DEBUG_CGI_ENTRY_EXIT - f=fopen(PA_DEBUG_CGI_ENTRY_EXIT, "at"); - opened=f!=0; -#endif +const char *parser3_log_filespec() { // $status:log-filename + const char* slog=log_filespec; - if(!opened && filespec_4log) { - char log_spec[MAX_STRING + 12 /* '/parser3.log' */]; + if(!slog) + slog=getenv(PARSER_LOG_ENV_NAME); + if(!slog) + slog=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME); + if(!slog) { + static char log_spec[MAX_STRING + 12 /* '/parser3.log' */]; pa_strncpy(log_spec, filespec_4log, MAX_STRING); - if(char *log_dir_pos=dir_pos(log_spec)){ + if(char* log_dir_pos=dir_pos(log_spec)){ strcpy(log_dir_pos, "/parser3.log"); } else { // no path, just filename strcpy(log_spec, "./parser3.log"); } - - f=fopen(log_spec, "at"); - opened=f!=0; + slog=log_spec; } + return slog; +} + +// SAPI + +static void pa_log(const char* fmt, va_list args) { + // use no memory [so that we could log out-of-memory error] + const char* slog=parser3_log_filespec(); + FILE *f=strcmp(slog,"-") ? fopen(slog, "at") : stderr; + // fallback to stderr - if(!opened) + if(!f) f=stderr; - // use no memory [so that we could log out-of-memory error] setbuf(f, 0); // stderr stream is unbuffered by default, but still... // prefix @@ -112,8 +115,8 @@ static void pa_log(const char* fmt, va_l if(size_t len=strlen(stamp)) // saw once stamp being ="" fprintf(f, "[%.*s] [%u] ", (int)len-1, stamp, (unsigned int)pa_get_thread_id() ); } - // message + // message char buf[MAX_LOG_STRING]; size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args); size=remove_crlf(buf, buf+size); @@ -124,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); @@ -158,11 +161,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); } @@ -175,24 +182,28 @@ const char* const *SAPI::Env::get(SAPI_I return info.get_env(); } -size_t SAPI::read_post(SAPI_Info& info, char *buf, size_t max_bytes) { +size_t SAPI::read_post(SAPI_Info& info, char* buf, size_t max_bytes) { return info.read_post(buf, max_bytes); } 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.clear_headers(); } size_t SAPI::send_body(SAPI_Info& info, const void *buf, size_t size) { return info.send_body(buf, size); } -static const char *full_disk_path(const char* file_name = "") { - char *result; +static const char* full_disk_path(const char* file_name = "") { + char* result; if(file_name[0]=='/' #ifdef WIN32 || file_name[0] && file_name[1]==':' @@ -210,7 +221,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 @@ -229,19 +240,28 @@ static void SIGPIPE_handler(int /*sig*/) #endif // requires pa_thread_request() in entry_exists() under Windows -static const char *locate_config(const char *config_filespec_option, const char *executable_path){ +static const char* locate_config(const char* config_filespec_option, const char* executable_path){ filespec_4log=config_filespec_option; if(!filespec_4log) filespec_4log=getenv(PARSER_CONFIG_ENV_NAME); if(!filespec_4log) filespec_4log=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME); if(!filespec_4log){ - // next to the executable - const char *exec_dir_pos = dir_pos(executable_path); - filespec_4log = exec_dir_pos ? pa_strcat(pa_strdup(executable_path, exec_dir_pos - executable_path), "/" AUTO_FILE_NAME) : full_disk_path(AUTO_FILE_NAME); - if(entry_exists(filespec_4log)) - return filespec_4log; + const char* exec_dir_pos = dir_pos(executable_path); +#ifdef SYSTEM_CONFIG_FILE + if(exec_dir_pos){ +#endif + // next to the executable + if(!exec_dir_pos || (exec_dir_pos==executable_path+1 && *executable_path=='.')){ + // when just parser3 or ./parser3 full path should be used to avoid "parser already configured" + filespec_4log=full_disk_path(AUTO_FILE_NAME); + } else { + filespec_4log=pa_strcat(pa_strdup(executable_path, exec_dir_pos - executable_path), "/" AUTO_FILE_NAME); + } + if(entry_exists(filespec_4log)) + return filespec_4log; #ifdef SYSTEM_CONFIG_FILE + } if(entry_exists(SYSTEM_CONFIG_FILE)){ filespec_4log=NULL; return SYSTEM_CONFIG_FILE; @@ -253,7 +273,7 @@ static const char *locate_config(const c } #ifdef WIN32 -const char* maybe_reconstruct_IIS_status_in_qs(const char* original) { +static const char* maybe_reconstruct_IIS_status_in_qs(const char* original) { // 404;http://servername/page[?param=value...] // ';' should be urlencoded by HTTP standard, so we shouldn't get it from browser // and can consider that as an indication that this is IIS way to report errors @@ -285,9 +305,20 @@ const char* maybe_reconstruct_IIS_status return original; } +static const char* maybe_back_slashes_to_slashes(const char* original){ + char *result=pa_strdup(original); + back_slashes_to_slashes(result); + return result; +} + #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) maybe_reconstruct_IIS_status_in_qs(s) +#define MAYBE_BACK_SLASHES_TO_SLASHES(s) maybe_back_slashes_to_slashes(s) + #else + #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) s +#define MAYBE_BACK_SLASHES_TO_SLASHES(s) s + #endif class RequestController { @@ -302,11 +333,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; } }; @@ -315,7 +348,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 = ""; @@ -331,7 +364,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()) @@ -353,13 +386,11 @@ 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){ - info.clear_response_headers(); + const char* status = info.exception_http_status(e.type()); + if(*status) SAPI::send_error(info, e.comment(), status); - } } } @@ -374,7 +405,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); @@ -382,7 +413,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); @@ -434,7 +465,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 @@ -459,11 +490,9 @@ static void real_parser_handler(bool cgi if(!filespec_to_process) SAPI::die("Parser/%s", PARSER_VERSION); - char document_root_buf[MAX_STRING]; - // 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"; @@ -526,11 +555,11 @@ 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 - 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); @@ -579,10 +608,14 @@ static void call_real_parser_handler__su #define REAL_PARSER_HANDLER real_parser_handler #endif -static void usage(const char* program) { +static void usage(const char* message=NULL) { + if(message){ + fprintf(stderr, message, parser3_filespec); + } + 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" @@ -591,88 +624,94 @@ 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" + " -l log_file Use this log file (/path/to/parser3.log)\n" " -p [host:]port Start web server on this port\n" " -h Display usage information (this message)\n", PARSER_VERSION, - program); + parser3_filespec ? parser3_filespec : "parser3" ); exit(EINVAL); } +#define ARG_REQUIRED \ + if(c[1] || !*(++carg)){ \ + fprintf(stderr, "%s: option '%c' requires an argument\n", parser3_filespec, *c); \ + usage(); \ + } 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] ? argv[0] : "parser3"; + if(!argc || !argv[0]) + usage(); + parser3_filespec = MAYBE_BACK_SLASHES_TO_SLASHES(argv[0]); + umask(2); // 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; + parser3_mode = "console"; + } #ifdef SIGPIPE signal(SIGPIPE, SIGPIPE_handler); #endif - - char *raw_filespec_to_process = NULL; + char* raw_filespec_to_process = NULL; if(cgi) { raw_filespec_to_process=getenv("PATH_TRANSLATED"); argv_extra=argv + 1; } else { - int optind=1; - while(optind < argc){ - char *carg = argv[optind]; - if(carg[0] != '-') + char** carg = argv + 1; + for(;*carg; carg++){ + if((*carg)[0] != '-') break; - for(size_t k = 1; k < strlen(carg); k++){ - char c = carg[k]; - switch (c) { + for(char* c=(*carg)+1; *c; c++){ + switch (*c) { case 'h': - usage(parser3_filespec); + usage(); break; case 'f': - if(optind < argc - 1){ - optind++; - config_filespec=argv[optind]; - } + ARG_REQUIRED; + config_filespec=*carg; + break; + case 'l': + ARG_REQUIRED; + log_filespec=*carg; break; case 'p': - if(optind < argc - 1){ - optind++; - httpd_host_port=argv[optind]; - } + ARG_REQUIRED; + httpd_host_port=*carg; + parser3_mode="httpd"; break; #ifdef WITH_MAILRECEIVE case 'm': mail_received=true; + parser3_mode="mail"; break; #endif default: - fprintf(stderr, "%s: invalid option '%c'\n", parser3_filespec, c); - usage(parser3_filespec); + fprintf(stderr, "%s: invalid option '%c'\n", parser3_filespec, *c); + usage(); break; } } - optind++; } - if (optind > argc - 1) { - if(!httpd_host_port) { - fprintf(stderr, "%s: file not specified\n", parser3_filespec); - usage(parser3_filespec); - } + if (*carg) { + raw_filespec_to_process=*carg; } else { - raw_filespec_to_process=argv[optind]; + if(!httpd_host_port) + usage("%s: file not specified\n"); } - if (httpd_host_port && mail_received) { - fprintf(stderr, "%s: -p and -m options should not be used together\n", parser3_filespec); - usage(parser3_filespec); - } + if (httpd_host_port && mail_received) + usage("%s: -p and -m options should not be used together\n"); - argv_extra=argv + optind; + argv_extra=carg; } #ifdef _MSC_VER @@ -701,12 +740,13 @@ int main(int argc, char *argv[]) { } REAL_PARSER_HANDLER(cgi); - } catch(const Exception& e) { // exception in unhandled exception - SAPI::die("%s", e.comment()); + } 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 sapiInfo->http_response_code < 100 ? sapiInfo->http_response_code : 0; + return sapi_info->http_response_code < 100 ? sapi_info->http_response_code : 0; }