--- parser3/src/targets/isapi/parser3isapi.C 2004/02/11 15:33:17 1.87 +++ parser3/src/targets/isapi/parser3isapi.C 2007/11/27 09:58:05 1.99 @@ -1,11 +1,11 @@ /** @file Parser: IIS extension. - Copyright (c) 2000,2001-2004 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2000,2001-2005 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_PARSER3ISAPI_C="$Date: 2004/02/11 15:33:17 $"; +static const char * const IDENT_PARSER3ISAPI_C="$Date: 2007/11/27 09:58:05 $"; #ifndef _MSC_VER # error compile ISAPI module with MSVC [no urge for now to make it autoconf-ed (PAF)] @@ -19,11 +19,19 @@ static const char * const IDENT_PARSER3I #include "pa_version.h" #include "pa_socks.h" +#define WINVER 0x0400 #include #include #include +// defines + +#if _MSC_VER && !defined(_DEBUG) +# define PA_SUPPRESS_SYSTEM_EXCEPTION +#endif + + #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION") // consts @@ -69,12 +77,12 @@ public: void SAPI::log(SAPI_Info& SAPI_info, const char* fmt, ...) { va_list args; va_start(args,fmt); - char buf[MAX_STRING]; + char buf[MAX_LOG_STRING]; const char* prefix="PARSER_ERROR:"; strcpy(buf, prefix); char *start=buf+strlen(prefix); - DWORD size=vsnprintf(start, MAX_STRING-strlen(prefix), fmt, args); - remove_crlf(start, start+size); + DWORD size=vsnprintf(start, MAX_LOG_STRING-strlen(prefix), fmt, args); + size=remove_crlf(start, start+size); SAPI_info.lpECB->ServerSupportFunction(SAPI_info.lpECB->ConnID, HSE_APPEND_LOG_PARAMETER, buf, &size, 0); @@ -93,14 +101,14 @@ void SAPI::die(const char* fmt, ...) { va_list args; va_start(args, fmt); ::abort(fmt, args); - va_end(args); +// va_end(args); } void SAPI::abort(const char* fmt, ...) { va_list args; va_start(args, fmt); ::abort(fmt, args); - va_end(args); +// va_end(args); } char* SAPI::get_env(SAPI_Info& SAPI_info, const char* name) { @@ -147,8 +155,8 @@ const char* const *SAPI::environment(SAP // we know this buf is writable char* all_http_vars=SAPI::get_env(info, "ALL_HTTP"); const int http_var_count=grep_char(all_http_vars, '\n')+1/*\n for theoretical(never saw) this \0*/; - - const char* *result=new const char*[IIS51var_count+http_var_count+1/*0*/]; + + const char* *result=new(PointerFreeGC) const char*[IIS51var_count+http_var_count+1/*0*/]; const char* *cur=result; // IIS5.1 vars @@ -243,10 +251,12 @@ void SAPI::send_header(SAPI_Info& SAPI_i HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL); } -void SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) { +size_t SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) { DWORD num_bytes=size; - SAPI_info.lpECB->WriteClient(SAPI_info.lpECB->ConnID, - const_cast(buf), &num_bytes, HSE_IO_SYNC); + if(!SAPI_info.lpECB->WriteClient(SAPI_info.lpECB->ConnID, + const_cast(buf), &num_bytes, HSE_IO_SYNC)) + return 0; + return (size_t)num_bytes; } @@ -258,13 +268,13 @@ static bool parser_init() { try { // init socks - pa_init_socks(); + pa_socks_init(); // init global variables pa_globals_init(); // successful finish return true; - } catch(const Exception& e) { // global problem + } catch(.../*const Exception& e*/) { // global problem //const char* body=e.comment(); // unsuccessful finish @@ -272,6 +282,14 @@ static bool parser_init() { } } +static void parser_done() { + // finalize global variables + pa_globals_done(); + + // + pa_socks_done(); +} + /// ISAPI // BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) { pVer->dwExtensionVersion = HSE_VERSION; @@ -279,6 +297,15 @@ BOOL WINAPI GetExtensionVersion(HSE_VERS pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0; return parser_init(); } +// dwFlags & HSE_TERM_MUST_UNLOAD means we can't return false +BOOL WINAPI TerminateExtension( + DWORD /*dwFlags*/ +) +{ + parser_done(); + + return TRUE; +} /** ISAPI // main workhorse @@ -291,8 +318,17 @@ BOOL WINAPI GetExtensionVersion(HSE_VERS @test PARSER_VERSION from outside */ - void real_parser_handler(SAPI_Info& SAPI_info, bool header_only) { + // collect garbage from prev request +#ifndef PA_DEBUG_DISABLE_GC + { + int saved=GC_dont_gc; + GC_dont_gc=0; + GC_gcollect(); + GC_dont_gc=saved; + } +#endif + SAPI_info.header=new String; LPEXTENSION_CONTROL_BLOCK lpECB=SAPI_info.lpECB; @@ -312,7 +348,7 @@ void real_parser_handler(SAPI_Info& SAPI strncpy(buf, filespec_to_process, len); buf[len]=0; request_info.document_root=buf; } else - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, 0, "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)"); @@ -362,30 +398,52 @@ void real_parser_handler(SAPI_Info& SAPI header_only); } -void call_real_parser_handler__do_SEH(SAPI_Info& SAPI_info, bool header_only) { -#if _MSC_VER & !defined(_DEBUG) +#ifdef PA_SUPPRESS_SYSTEM_EXCEPTION +static const Exception +call_real_parser_handler__do_PEH_return_it( + SAPI_Info& SAPI_info, bool header_only) +{ + try { + real_parser_handler(SAPI_info, header_only); + } catch(const Exception& e) { + return e; + } + + return Exception(); +} +static void call_real_parser_handler__supress_system_exception( + SAPI_Info& SAPI_info, bool header_only) +{ + Exception parser_exception; LPEXCEPTION_POINTERS system_exception=0; + __try { -#endif - real_parser_handler(SAPI_info, header_only); - -#if _MSC_VER & !defined(_DEBUG) + parser_exception=call_real_parser_handler__do_PEH_return_it( + SAPI_info, header_only); } __except ( (system_exception=GetExceptionInformation()), - EXCEPTION_EXECUTE_HANDLER) { - + EXCEPTION_EXECUTE_HANDLER) + { + if(system_exception) if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord) - throw Exception(0, - 0, - "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress); + throw Exception("system", + 0, + "0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress); else - throw Exception(0, 0, "Exception "); - else - throw Exception(0, 0, "Exception "); + throw Exception("system", + 0, + ""); + else + throw Exception("system", + 0, + ""); } -#endif + + if(parser_exception) + throw Exception(parser_exception); } +#endif DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) { //_asm int 3; @@ -397,7 +455,12 @@ DWORD WINAPI HttpExtensionProc(LPEXTENSI bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0; try { // global try - call_real_parser_handler__do_SEH(SAPI_info, header_only); +#ifdef PA_SUPPRESS_SYSTEM_EXCEPTION + call_real_parser_handler__supress_system_exception( +#else + real_parser_handler( +#endif + SAPI_info, header_only); // successful finish } catch(const Exception& e) { // global problem // don't allocate anything on pool here: