--- parser3/src/targets/cgi/parser3.C 2001/11/05 15:18:15 1.129 +++ parser3/src/targets/cgi/parser3.C 2002/01/31 11:11:45 1.152 @@ -4,13 +4,16 @@ Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) Author: Alexander Petrosyan (http://paf.design.ru) - $Id: parser3.C,v 1.129 2001/11/05 15:18:15 paf Exp $ + $Id: parser3.C,v 1.152 2002/01/31 11:11:45 paf Exp $ */ #include "pa_config_includes.h" -#ifdef WIN32 -# include +#if _MSC_VER +# include +# include +#else +# include "pa_config_paths.h" #endif #include "pa_sapi.h" @@ -21,8 +24,8 @@ #include "pa_version.h" #include "pool_storage.h" -#ifdef XML -#include +#ifdef WIN32 +# include #endif //#define DEBUG_POOL_MALLOC @@ -37,9 +40,6 @@ extern const char *gd_RCSIds[]; extern const char *classes_RCSIds[]; extern const char *types_RCSIds[]; extern const char *parser3_RCSIds[]; -#ifdef XML -extern const char *xalan_patched_RCSIds[]; -#endif const char **RCSIds[]={ main_RCSIds, #ifdef USE_SMTP @@ -49,9 +49,6 @@ const char **RCSIds[]={ classes_RCSIds, types_RCSIds, parser3_RCSIds, -#ifdef XML - xalan_patched_RCSIds, -#endif 0 }; @@ -110,11 +107,41 @@ void SAPI::log(Pool& , const char *fmt, } void SAPI::die(const char *fmt, ...) { +#ifdef DEBUG_POOL_MALLOC + extern void log_pool_stats(Pool& pool); + log_pool_stats(pool); +#endif + va_list args; va_start(args,fmt); + // log + + // logging is more important than user + // she can cancel download, we'd get SIG_PIPE, + // nothing would be logged then ::log(fmt, args); + + // inform user + + char body[MAX_STRING]; + int content_length=vsnprintf(body, MAX_STRING, fmt, args); + va_end(args); + // prepare header + // let's be honest, that's bad we couldn't produce valid output + SAPI::add_header_attribute(pool, "status", "500"); + SAPI::add_header_attribute(pool, "content-type", "text/plain"); + char content_length_cstr[MAX_NUMBER]; + snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length); + SAPI::add_header_attribute(pool, "content-length", content_length_cstr); + + // send header + SAPI::send_header(pool); + + // body + SAPI::send_body(pool, body, content_length); + exit(1); } @@ -143,7 +170,7 @@ void SAPI::add_header_attribute(Pool& , /// @todo intelligent cache-control void SAPI::send_header(Pool& ) { if(cgi) { - puts("expires: Fri, 23 Mar 2001 09:32:23 GMT"); +// puts("expires: Fri, 23 Mar 2001 09:32:23 GMT"); // header | body delimiter puts(""); @@ -173,6 +200,7 @@ main workhorse IIS: remove trailing default-document[index.html] from $request.uri. to do that we need to consult metabase, wich is tested but seems slow. + IIS5 todo find out proper 'illegal call' check */ void real_parser_handler( const char *filespec_to_process, @@ -180,26 +208,13 @@ void real_parser_handler( // init socks init_socks(pool); -#ifdef XML - /** - * Initialize Xerces and Xalan. - * - * Should be called only once per process before making - * any other API calls. - */ - //_asm int 3; - XalanInitialize(); -#endif - // init global classes init_methoded_array(pool); // init global variables pa_globals_init(pool); if(!filespec_to_process) - throw Exception(0, 0, - 0, - "Parser/%s", PARSER_VERSION); + SAPI::die("Parser/%s", PARSER_VERSION); // Request info Request::Info request_info; @@ -247,15 +262,16 @@ void real_parser_handler( 0, "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)"); +#ifndef WIN32 + // they've changed this under IIS5. if(const char *script_name=SAPI::get_env(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 - throw Exception(0, 0, - 0, - "CGI: illegal call"); + SAPI::die("CGI: illegal call"); } +#endif } else request_info.uri=0; @@ -268,12 +284,17 @@ void real_parser_handler( // prepare to process request Request request(pool, request_info, - cgi ? String::UL_USER_HTML : String::UL_AS_IS - ); +#ifdef _DEBUG + String::UL_HTML|String::UL_OPTIMIZE_BIT +#else + cgi ? String::UL_HTML|String::UL_OPTIMIZE_BIT : String::UL_AS_IS +#endif + , + true /* status_allowed */); // some root-controlled location -#ifdef SYSCONFDIR - const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME; +#ifdef ROOT_CONFIG_DIR + const char *root_config_filespec=ROOT_CONFIG_DIR "/" CONFIG_FILE_NAME; #else # ifdef WIN32 // c:\windows @@ -323,7 +344,7 @@ void real_parser_handler( void call_real_parser_handler__do_SEH( const char *filespec_to_process, const char *request_method, bool header_only) { -#if _MSC_VER & !defined(_DEBUG) +#if _MSC_VER && !defined(_DEBUG) LPEXCEPTION_POINTERS system_exception=0; __try { #endif @@ -331,7 +352,7 @@ void call_real_parser_handler__do_SEH( filespec_to_process, request_method, header_only); -#if _MSC_VER & !defined(_DEBUG) +#if _MSC_VER && !defined(_DEBUG) } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) { @@ -349,8 +370,21 @@ void call_real_parser_handler__do_SEH( #endif } +#if _MSC_VER +int failed_new(size_t size) { + SAPI::die("out of memory in 'new', failed to allocated %u bytes", size); + return 0; // not reached +} +#endif + +#ifdef HAVE_SET_NEW_HANDLER +void failed_new() { + SAPI::die("out of memory in 'new'"); +} +#endif + int main(int argc, char *argv[]) { - int result; +// _asm int 3; argv0=argv[0]; umask(2); @@ -381,6 +415,28 @@ int main(int argc, char *argv[]) { setmode(fileno(stderr), _O_BINARY); #endif +#if _MSC_VER + _set_new_handler(failed_new); + +#ifdef _DEBUG + // Get current flag + int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); + + // Turn on leak-checking bit + tmpFlag |= _CRTDBG_LEAK_CHECK_DF; + + // Set flag to the new value + _CrtSetDbgFlag( tmpFlag ); +// _CrtSetBreakAlloc(471); + +#endif + +#endif + +#ifdef HAVE_SET_NEW_HANDLER + std::set_new_handler(failed_new); +#endif + char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1]; #ifdef WIN32 back_slashes_to_slashes(filespec_to_process); @@ -389,40 +445,21 @@ int main(int argc, char *argv[]) { const char *request_method=getenv("REQUEST_METHOD"); bool header_only=request_method && strcasecmp(request_method, "HEAD")==0; + try { // global try call_real_parser_handler__do_SEH( filespec_to_process, request_method, header_only); - - // successful finish - result=0; } catch(const Exception& e) { // global problem // don't allocate anything on pool here: // possible pool' exception not catch-ed now // and there could be out-of-memory exception - const char *body=e.comment(); - // log it - SAPI::log(pool, "exception in request exception handler: %s", body); - - // - int content_length=strlen(body); - - // prepare header - SAPI::add_header_attribute(pool, "content-type", "text/plain"); - char content_length_cstr[MAX_NUMBER]; - snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length); - SAPI::add_header_attribute(pool, "content-length", content_length_cstr); - - // send header - SAPI::send_header(pool); - - // body - if(!header_only) - SAPI::send_body(pool, body, content_length); - - // unsuccessful finish - result=1; + SAPI::die("exception in request exception handler: %s", e.comment()); +#ifndef _DEBUG + } catch(...) { + SAPI::die(""); +#endif } @@ -431,5 +468,5 @@ int main(int argc, char *argv[]) { if(!cgi) SAPI::send_body(pool, "\n", 1); #endif - return result; + return 0; }