|
|
| version 1.330, 2020/12/15 17:23:56 | version 1.346, 2021/12/22 21:52:49 |
|---|---|
| Line 14 volatile const char * IDENT_PARSER3_C="$ | Line 14 volatile const char * IDENT_PARSER3_C="$ |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_version.h" | #include "pa_version.h" |
| #include "pa_threads.h" | |
| #include "pa_vconsole.h" | #include "pa_vconsole.h" |
| #include "pa_sapi_info.h" | #include "pa_sapi_info.h" |
| Line 53 static char* parser3_filespec = 0; // ar | Line 54 static char* parser3_filespec = 0; // ar |
| static char** argv_extra = NULL; | static char** argv_extra = NULL; |
| // for error logging | // for error logging |
| static THREAD_LOCAL Request_info request_info; // global for correct log() reporting | static THREAD_LOCAL Request_info *request_info_4log = NULL; // global for correct log() reporting |
| static const char* filespec_4log = NULL; // null only if system-wide auto.p used | static const char* filespec_4log = NULL; // null only if system-wide auto.p used |
| // SAPI | // SAPI |
| static void log(const char* fmt, va_list args) { | static void pa_log(const char* fmt, va_list args) { |
| bool opened=false; | bool opened=false; |
| FILE *f=0; | FILE *f=0; |
| Line 76 static void log(const char* fmt, va_list | Line 77 static void log(const char* fmt, va_list |
| if(!opened && filespec_4log) { | if(!opened && filespec_4log) { |
| char beside_config_path[MAX_STRING]; | char beside_config_path[MAX_STRING]; |
| strncpy(beside_config_path, filespec_4log, MAX_STRING-1); beside_config_path[MAX_STRING-1]=0; | pa_strncpy(beside_config_path, filespec_4log, MAX_STRING); |
| if(!(rsplit(beside_config_path, '/') || rsplit(beside_config_path, '\\'))) { // strip filename | if(!(rsplit(beside_config_path, '/') || rsplit(beside_config_path, '\\'))) { // strip filename |
| // no path, just filename | // no path, just filename |
| strcpy(beside_config_path, "."); | strcpy(beside_config_path, "."); |
| Line 98 static void log(const char* fmt, va_list | Line 99 static void log(const char* fmt, va_list |
| time_t t=time(0); | time_t t=time(0); |
| if(const char* stamp=ctime(&t)) { // never saw that | if(const char* stamp=ctime(&t)) { // never saw that |
| if(size_t len=strlen(stamp)) // saw once stamp being ="" | if(size_t len=strlen(stamp)) // saw once stamp being ="" |
| fprintf(f, "[%.*s] [%u] ", (int)len-1, stamp, (unsigned int)getpid() ); | fprintf(f, "[%.*s] [%u] ", (int)len-1, stamp, (unsigned int)pa_get_thread_id() ); |
| } | } |
| // message | // message |
| Line 107 static void log(const char* fmt, va_list | Line 108 static void log(const char* fmt, va_list |
| size=remove_crlf(buf, buf+size); | size=remove_crlf(buf, buf+size); |
| fwrite(buf, size, 1, f); | fwrite(buf, size, 1, f); |
| if(request_info.method) { | if(request_info_4log && request_info_4log->method) { |
| fprintf(f, " [uri=%s, method=%s, cl=%lu]\n", request_info.uri ? request_info.uri : "<unknown>", request_info.method, (unsigned long)request_info.content_length); | fprintf(f, " [uri=%s, method=%s, cl=%lu]\n", request_info_4log->uri ? request_info_4log->uri : "<unknown>", request_info_4log->method, (unsigned long)request_info_4log->content_length); |
| } else | } else |
| fputs(" [no request info]\n", f); | fputs(" [no request info]\n", f); |
| Line 118 static void log(const char* fmt, va_list | Line 119 static void log(const char* fmt, va_list |
| fflush(f); | fflush(f); |
| } | } |
| #ifdef PA_DEBUG_CGI_ENTRY_EXIT | void pa_log(const char* fmt, ...) { |
| static void log(const char* fmt, ...) { | |
| va_list args; | va_list args; |
| va_start(args,fmt); | va_start(args,fmt); |
| log(fmt, args); | pa_log(fmt, args); |
| va_end(args); | va_end(args); |
| } | } |
| #endif | |
| // appends to parser3.log located next to the config file if openable, to stderr otherwize | // appends to parser3.log located next to the config file if openable, to stderr otherwize |
| void SAPI::log(SAPI_Info&, const char* fmt, ...) { | void SAPI::log(SAPI_Info&, const char* fmt, ...) { |
| va_list args; | va_list args; |
| va_start(args,fmt); | va_start(args,fmt); |
| ::log(fmt, args); | pa_log(fmt, args); |
| va_end(args); | va_end(args); |
| } | } |
| Line 140 void SAPI::die(const char* fmt, ...) { | Line 139 void SAPI::die(const char* fmt, ...) { |
| // logging first, first vsnprintf | // logging first, first vsnprintf |
| va_start(args,fmt); | va_start(args,fmt); |
| ::log(fmt, args); | pa_log(fmt, args); |
| va_end(args); | va_end(args); |
| // inform user, second vsnprintf | // inform user, second vsnprintf |
| Line 187 static void full_disk_path(const char* f | Line 186 static void full_disk_path(const char* f |
| || file_name[0] && file_name[1]==':' | || file_name[0] && file_name[1]==':' |
| #endif | #endif |
| ){ | ){ |
| strncpy(buf, file_name, buf_size-1); buf[buf_size-1]=0; | pa_strncpy(buf, file_name, buf_size); |
| } else { | } else { |
| char cwd[MAX_STRING]; | char cwd[MAX_STRING]; |
| snprintf(buf, buf_size, "%s/%s", getcwd(cwd, MAX_STRING) ? cwd : "", file_name); | snprintf(buf, buf_size, "%s/%s", getcwd(cwd, MAX_STRING) ? cwd : "", file_name); |
| Line 201 static void log_signal(const char* signa | Line 200 static void log_signal(const char* signa |
| SAPI::log(*sapiInfo, "%s received %s processing request", signal_name, request ? "while" : "before or after"); | SAPI::log(*sapiInfo, "%s received %s processing request", signal_name, request ? "while" : "before or after"); |
| } | } |
| #ifdef SIGUSR1 | |
| static void SIGUSR1_handler(int /*sig*/){ | |
| log_signal("SIGUSR1"); | |
| } | |
| #endif | |
| #ifdef SIGPIPE | #ifdef SIGPIPE |
| #define SIGPIPE_NAME "SIGPIPE" | #define SIGPIPE_NAME "SIGPIPE" |
| static const String sigpipe_name(SIGPIPE_NAME); | static const String sigpipe_name(SIGPIPE_NAME); |
| Line 281 const char* maybe_reconstruct_IIS_status | Line 274 const char* maybe_reconstruct_IIS_status |
| } | } |
| #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) maybe_reconstruct_IIS_status_in_qs(s) | #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) maybe_reconstruct_IIS_status_in_qs(s) |
| #else | #else |
| #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) s | #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) s |
| #endif | #endif |
| class RequestController { | class RequestController { |
| public: | public: |
| RequestController(Request* r){ | RequestController(Request* r){ |
| ::request=r; | request=r; |
| } | } |
| ~RequestController(){ | ~RequestController(){ |
| ::request=0; | request=0; |
| } | |
| }; | |
| class RequestInfoController { | |
| public: | |
| RequestInfoController(Request_info* rinfo){ | |
| request_info_4log=rinfo; | |
| } | |
| ~RequestInfoController(){ | |
| request_info_4log=0; | |
| } | } |
| }; | }; |
| /** httpd support */ | |
| static const String httpd_class_name("httpd"); | |
| static void config_handler(SAPI_Info &info) { | static void config_handler(SAPI_Info &info) { |
| char document_root_buf[MAX_STRING]; | char document_root_buf[MAX_STRING]; |
| full_disk_path("", document_root_buf, sizeof(document_root_buf)); | full_disk_path("", document_root_buf, sizeof(document_root_buf)); |
| Request_info request_info; | |
| RequestInfoController ric(&request_info); | |
| request_info.document_root = document_root_buf; | request_info.document_root = document_root_buf; |
| request_info.uri = ""; | request_info.uri = ""; |
| request_info.argv = argv_extra; | request_info.argv = argv_extra; |
| Line 313 static void config_handler(SAPI_Info &in | Line 321 static void config_handler(SAPI_Info &in |
| } | } |
| static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection) { | static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection) { |
| Request_info request_info; | |
| RequestInfoController ric(&request_info); | |
| try { | try { |
| connection.read_header(); | if(!connection.read_header()) |
| return; // ignore "void" connections | |
| info.populate_env(); | info.populate_env(); |
| char document_root_buf[MAX_STRING]; | char document_root_buf[MAX_STRING]; |
| full_disk_path("", document_root_buf, sizeof(document_root_buf)); | full_disk_path("", document_root_buf, sizeof(document_root_buf)); |
| memset(&request_info, 0, sizeof(request_info)); | |
| request_info.document_root = document_root_buf; | request_info.document_root = document_root_buf; |
| request_info.path_translated = filespec_to_process; | request_info.path_translated = filespec_to_process; |
| request_info.method = connection.method(); | request_info.method = connection.method(); |
| Line 335 static void connection_handler(SAPI_Info | Line 346 static void connection_handler(SAPI_Info |
| // prepare to process request | // prepare to process request |
| Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)); | Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)); |
| // process the request | // process the request |
| r.core(config_filespec, strcasecmp(request_info.method, "HEAD")==0, String("httpd-main")); | 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 or unhandled exception |
| SAPI::log(info, "%s", e.comment()); | SAPI::log(info, "%s", e.comment()); |
| SAPI::send_error(info, e.comment(), info.exception_http_status(e.type())); | const char *status = info.exception_http_status(e.type()); |
| if(*status){ | |
| info.clear_response_headers(); | |
| SAPI::send_error(info, e.comment(), status); | |
| } | |
| } | } |
| } | } |
| Line 363 static void *connection_thread(void *arg | Line 378 static void *connection_thread(void *arg |
| static void httpd_mode() { | static void httpd_mode() { |
| config_handler(*sapiInfo); | config_handler(*sapiInfo); |
| int sock = HTTPD_Server::bind(httpd_host_port); | SOCKET sock = HTTPD_Server::bind(httpd_host_port); |
| #ifdef SIGCHLD | |
| signal(SIGCHLD, SIG_IGN); | |
| #endif | |
| #ifdef SIGPIPE | #ifdef SIGPIPE |
| signal(SIGPIPE, SIG_IGN); | signal(SIGPIPE, SIG_IGN); |
| #endif | #endif |
| Line 375 static void httpd_mode() { | Line 387 static void httpd_mode() { |
| while(1){ | while(1){ |
| #ifndef _MSC_VER | #ifndef _MSC_VER |
| pid_t pid=1; | pid_t pid=1; |
| if(HTTPD_Server::mode == HTTPD_Server::PARALLEL) | |
| while (waitpid((pid_t)(-1), 0, WNOHANG) > 0); | |
| #endif | #endif |
| try { | try { |
| HTTPD_Connection connection; | HTTPD_Connection connection; |
| if(!connection.accept(sock, 5)) | if(!connection.accept(sock, 500)) |
| continue; | continue; |
| switch (HTTPD_Server::mode) { | switch (HTTPD_Server::mode) { |
| Line 386 static void httpd_mode() { | Line 400 static void httpd_mode() { |
| #ifdef _MSC_VER | #ifdef _MSC_VER |
| if (!GC_CreateThread(0, 0, connection_thread, new HTTPD_Connection(connection), 0, 0)) | if (!GC_CreateThread(0, 0, connection_thread, new HTTPD_Connection(connection), 0, 0)) |
| throw Exception("httpd.fork", 0, "thread creation failed"); | throw Exception("httpd.fork", 0, "thread creation failed"); |
| connection.sock = -1; | connection.sock=INVALID_SOCKET; |
| break; | break; |
| #else | #else |
| #ifdef HAVE_TLS | |
| pthread_t thread; | pthread_t thread; |
| pthread_attr_t attr; | pthread_attr_t attr; |
| pthread_attr_init(&attr); | pthread_attr_init(&attr); |
| Line 396 static void httpd_mode() { | Line 411 static void httpd_mode() { |
| if(int result=GC_pthread_create(&thread, &attr, connection_thread, new HTTPD_Connection(connection))) | if(int result=GC_pthread_create(&thread, &attr, connection_thread, new HTTPD_Connection(connection))) |
| throw Exception("httpd.fork", 0, "thread creation failed (%d)", result); | throw Exception("httpd.fork", 0, "thread creation failed (%d)", result); |
| connection.sock=-1; | connection.sock=INVALID_SOCKET; |
| break; | break; |
| #endif | |
| case HTTPD_Server::PARALLEL: | case HTTPD_Server::PARALLEL: |
| pid=fork(); | pid=fork(); |
| if(pid<0) | if(pid<0) |
| Line 440 static void real_parser_handler(bool cgi | Line 456 static void real_parser_handler(bool cgi |
| char document_root_buf[MAX_STRING]; | char document_root_buf[MAX_STRING]; |
| // global request info | // global request info |
| Request_info request_info; | |
| RequestInfoController ric(&request_info); | |
| request_info.path_translated = filespec_to_process; | request_info.path_translated = filespec_to_process; |
| request_info.method = request_method ? request_method : "GET"; | request_info.method = request_method ? request_method : "GET"; |
| request_info.query_string = MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(getenv("QUERY_STRING")); | request_info.query_string = MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(getenv("QUERY_STRING")); |
| Line 587 int main(int argc, char *argv[]) { | Line 606 int main(int argc, char *argv[]) { |
| bool cgi=(getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) && !getenv("PARSER_VERSION"); | 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(); | sapiInfo = cgi ? new SAPI_Info_CGI() : new SAPI_Info(); |
| #ifdef SIGUSR1 | |
| signal(SIGUSR1, SIGUSR1_handler); | |
| #endif | |
| #ifdef SIGPIPE | #ifdef SIGPIPE |
| signal(SIGPIPE, SIGPIPE_handler); | signal(SIGPIPE, SIGPIPE_handler); |
| #endif | #endif |