--- parser3/src/targets/cgi/parser3.C 2002/11/20 13:37:23 1.201 +++ parser3/src/targets/cgi/parser3.C 2003/03/21 07:08:28 1.217 @@ -1,11 +1,11 @@ /** @file Parser: scripting and CGI main. - Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_PARSER3_C="$Date: 2002/11/20 13:37:23 $"; +static const char* IDENT_PARSER3_C="$Date: 2003/03/21 07:08:28 $"; #include "pa_config_includes.h" @@ -20,6 +20,7 @@ static const char* IDENT_PARSER3_C="$Dat #include "pa_request.h" #include "pa_socks.h" #include "pa_version.h" + #include "pool_storage.h" #ifdef WIN32 @@ -87,6 +88,9 @@ static void log(const char *fmt, va_list if(!opened) 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 time_t t=time(0); if(const char *stamp=ctime(&t)) { // never saw that @@ -128,7 +132,7 @@ void SAPI::die(const char *fmt, ...) { // log // logging is more important than user - // she can cancel download, we'd get SIG_PIPE, + // she can cancel download, we'd get SIGPIPE, // nothing would be logged then ::log(fmt, args); @@ -153,7 +157,13 @@ void SAPI::die(const char *fmt, ...) { // body SAPI::send_body(global_pool, body, content_length); +#ifdef WIN32 + // IIS with abort failes to show STDOUT, it just barks "abnormal program termination" exit(1); +#else + // exit & try to produce core dump + abort(); +#endif } const char *SAPI::get_env(Pool& , const char *name) { @@ -208,9 +218,9 @@ static void full_file_spec(const char *f if(file_name) if(file_name[0]=='/' #ifdef WIN32 - || (file_name[0] && file_name[1]==':') + || file_name[0] && file_name[1]==':' #endif - ) + ) strncpy(buf, file_name, buf_size); else { char cwd[MAX_STRING]; getcwd(cwd, MAX_STRING); @@ -223,15 +233,23 @@ static void full_file_spec(const char *f #endif } +static void log_signal(const char *signal_name) { + SAPI::log(global_pool, request? "%s received. uri=%s, qs=%s" + :"%s received. no request being processed", + signal_name, + request && request->info.uri?request->info.uri:"-", + request && request->info.query_string?request->info.query_string:"-"); +} + #ifdef SIGUSR1 -void SIGUSR1_Handler(int /*sig*/){ - SAPI::log(global_pool, "SIGUSR1 received. url=", request?request->info.uri:""); +static void SIGUSR1_handler(int /*sig*/){ + log_signal("SIGUSR1"); } #endif #ifdef SIGPIPE -void SIGPIPE_Handler(int /*sig*/){ - SAPI::log(global_pool, "SIGPIPE received. url=", request?request->info.uri:""); +static void SIGPIPE_handler(int /*sig*/){ + log_signal("SIGPIPE"); if(request) request->interrupt(); } @@ -287,9 +305,18 @@ static void real_parser_handler( const char *query_string=SAPI::get_env(request_pool, "QUERY_STRING"); request_info.query_string=query_string; if(cgi) { - if(const char *env_request_uri=SAPI::get_env(request_pool, "REQUEST_URI")) + // few absolute obligatory + const char *path_info=SAPI::get_env(request_pool, "PATH_INFO"); + if(!path_info) + SAPI::die("CGI: illegal call (missing PATH_INFO)"); + const char *script_name=SAPI::get_env(request_pool, "SCRIPT_NAME"); + if(!script_name) + SAPI::die("CGI: illegal call (missing SCRIPT_NAME)"); + + const char *env_request_uri=SAPI::get_env(request_pool, "REQUEST_URI"); + if(env_request_uri) request_info.uri=env_request_uri; - else if(const char *path_info=SAPI::get_env(request_pool, "PATH_INFO")) + else if(query_string) { char *reconstructed_uri=(char *)request_pool.malloc( strlen(path_info)+1/*'?'*/+ @@ -300,19 +327,43 @@ static void real_parser_handler( request_info.uri=reconstructed_uri; } else request_info.uri=path_info; - else - throw Exception("parser.runtime", - 0, - "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)"); - - // they've changed this under IIS5. - if(const char *script_name=SAPI::get_env(request_pool, "SCRIPT_NAME")) { - size_t script_name_len=strlen(script_name); - size_t uri_len=strlen(request_info.uri); - if(strncmp(request_info.uri,script_name, script_name_len)==0 && - script_name_len != uri_len) // under IIS they are the same - SAPI::die("CGI: illegal call"); - } + + if(env_request_uri) { // apache & others stuck to standards + /* + http://parser3/env.html?123 =OK + $request:uri=/env.html?123 + REQUEST_URI='/env.html?123' + SCRIPT_NAME='/cgi-bin/parser3' + PATH_INFO='/env.html' + + http://parser3/cgi-bin/parser3/env.html?123 =ERROR + $request:uri=/cgi-bin/parser3/env.html?123 + REQUEST_URI='/cgi-bin/parser3/env.html?123' + SCRIPT_NAME='/cgi-bin/parser3' + PATH_INFO='/env.html' + */ + size_t script_name_len=strlen(script_name); + size_t uri_len=strlen(env_request_uri); + if(strncmp(env_request_uri, script_name, script_name_len)==0 && + script_name_len != uri_len) // under IIS they are the same + SAPI::die("CGI: illegal call (1)"); + } else { // seen on IIS5 + /* + http://nestle/env.html?123 =OK + $request:uri=/env.html?123 + REQUEST_URI='' + SCRIPT_NAME='/env.html' + PATH_INFO='/env.html' + + http://nestle/cgi-bin/parser3.exe/env.html =ERROR + $request:uri=/env.html + REQUEST_URI='' + SCRIPT_NAME='/cgi-bin/parser3.exe' + PATH_INFO='/env.html' + */ + if(strcmp(script_name, path_info)!=0) + SAPI::die("CGI: illegal call (2)"); + } } else request_info.uri=""; @@ -336,14 +387,6 @@ static void real_parser_handler( // get request ptr for signal handlers ::request=&request; -#ifdef SIGUSR1 - if(signal(SIGUSR1, SIGUSR1_Handler)==SIG_ERR) - SAPI::die("Can not set handler for SIGUSR1"); -#endif -#ifdef SIGPIPE - if(signal(SIGPIPE, SIGPIPE_Handler)==SIG_ERR) - SAPI::die("Can not set handler for SIGPIPE"); -#endif char config_filespec_buf[MAX_STRING]; if(!config_filespec_cstr) { @@ -442,7 +485,7 @@ static void failed_new() { static void usage(const char *program) { printf( - "Parser/%s Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)\n" + "Parser/%s Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)\n" "Author: Alexandr Petrosian (http://paf.design.ru)\n" "\n" "Usage: %s [options] file\n" @@ -457,7 +500,28 @@ static void usage(const char *program) { exit(EINVAL); } + +class C { +public: + C() {} +}; +C *trick() { + return new C; +} + int main(int argc, char *argv[]) { + delete trick(); + +#ifdef SIGUSR1 + if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR) + SAPI::die("Can not set handler for SIGUSR1"); +#endif +#ifdef SIGPIPE + if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR) + SAPI::die("Can not set handler for SIGPIPE"); +#endif + + #ifdef DEBUG_MAILRECEIVE if(FILE *fake_in=fopen(DEBUG_MAILRECEIVE, "rt")) { dup2(fake_in->_file, 0/*STDIN_FILENO*/); @@ -480,9 +544,11 @@ int main(int argc, char *argv[]) { getenv("REQUEST_METHOD"); char *raw_filespec_to_process; - if(cgi) + if(cgi) { raw_filespec_to_process=getenv("PATH_TRANSLATED"); - else { + if(raw_filespec_to_process && !*raw_filespec_to_process) + raw_filespec_to_process=0; + } else { optind = 1; opterr = 0; int c; @@ -566,13 +632,7 @@ int main(int argc, char *argv[]) { SAPI::die(""); #endif } - -#ifndef WIN32 - // - if(!cgi) - SAPI::send_body(global_pool, "\n", 1); -#endif -//_asm int 3; + //_asm int 3; return 0; }