|
|
| version 1.84, 2003/11/20 16:34:28 | version 1.92.8.3, 2005/11/22 12:49:58 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: IIS extension. | Parser: IIS extension. |
| Copyright (c) 2000,2001-2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2000,2001-2005 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| Line 24 static const char * const IDENT_PARSER3I | Line 24 static const char * const IDENT_PARSER3I |
| #include <httpext.h> | #include <httpext.h> |
| // defines | |
| #if _MSC_VER && !defined(_DEBUG) | |
| # define PA_SUPPRESS_SYSTEM_EXCEPTION | |
| #endif | |
| #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION") | #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION") |
| // consts | // consts |
| Line 48 const int IIS51var_count=sizeof(IIS51var | Line 55 const int IIS51var_count=sizeof(IIS51var |
| // globals | // globals |
| char argv0[MAX_STRING]=""; | char argv0buf[MAX_STRING]=""; |
| const char* argv0; | |
| // SAPI | // SAPI |
| Line 81 void SAPI::log(SAPI_Info& SAPI_info, con | Line 89 void SAPI::log(SAPI_Info& SAPI_info, con |
| } | } |
| /// @todo event log | /// @todo event log |
| static void die_or_abort(const char* fmt, va_list args, bool write_core) { | static void abort(const char* fmt, va_list args) { |
| if(FILE *log=fopen("c:\\parser3die.log", "at")) { | if(FILE *log=fopen("c:\\parser3die.log", "at")) { |
| vfprintf(log, fmt, args); | vfprintf(log, fmt, args); |
| fclose(log); | fclose(log); |
| Line 92 static void die_or_abort(const char* fmt | Line 100 static void die_or_abort(const char* fmt |
| void SAPI::die(const char* fmt, ...) { | void SAPI::die(const char* fmt, ...) { |
| va_list args; | va_list args; |
| va_start(args, fmt); | va_start(args, fmt); |
| die_or_abort(fmt, args, false/*write core?*/); | ::abort(fmt, args); |
| va_end(args); | // va_end(args); |
| } | } |
| void SAPI::abort(const char* fmt, ...) { | void SAPI::abort(const char* fmt, ...) { |
| va_list args; | va_list args; |
| va_start(args, fmt); | va_start(args, fmt); |
| die_or_abort(fmt, args, true/*write core?*/); | ::abort(fmt, args); |
| va_end(args); | // va_end(args); |
| } | } |
| char* SAPI::get_env(SAPI_Info& SAPI_info, const char* name) { | char* SAPI::get_env(SAPI_Info& SAPI_info, const char* name) { |
| Line 147 const char* const *SAPI::environment(SAP | Line 155 const char* const *SAPI::environment(SAP |
| // we know this buf is writable | // we know this buf is writable |
| char* all_http_vars=SAPI::get_env(info, "ALL_HTTP"); | 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 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; | const char* *cur=result; |
| // IIS5.1 vars | // IIS5.1 vars |
| Line 243 void SAPI::send_header(SAPI_Info& SAPI_i | Line 251 void SAPI::send_header(SAPI_Info& SAPI_i |
| HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL); | 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; | DWORD num_bytes=size; |
| SAPI_info.lpECB->WriteClient(SAPI_info.lpECB->ConnID, | if(!SAPI_info.lpECB->WriteClient(SAPI_info.lpECB->ConnID, |
| const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC); | const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC)) |
| return 0; | |
| return (size_t)num_bytes; | |
| } | } |
| Line 258 static bool parser_init() { | Line 268 static bool parser_init() { |
| try { | try { |
| // init socks | // init socks |
| pa_init_socks(); | pa_socks_init(); |
| // init global variables | // init global variables |
| pa_globals_init(); | pa_globals_init(); |
| // successful finish | // successful finish |
| return true; | return true; |
| } catch(const Exception& e) { // global problem | } catch(const Exception& e) { // global problem |
| const char* body=e.comment(); | //const char* body=e.comment(); |
| // unsuccessful finish | // unsuccessful finish |
| return false; | return false; |
| } | } |
| } | } |
| static void parser_done() { | |
| // finalize global variables | |
| pa_globals_done(); | |
| // | |
| pa_socks_done(); | |
| } | |
| /// ISAPI // | /// ISAPI // |
| BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) { | BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) { |
| pVer->dwExtensionVersion = HSE_VERSION; | pVer->dwExtensionVersion = HSE_VERSION; |
| Line 279 BOOL WINAPI GetExtensionVersion(HSE_VERS | Line 297 BOOL WINAPI GetExtensionVersion(HSE_VERS |
| pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0; | pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0; |
| return parser_init(); | 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 | ISAPI // main workhorse |
| Line 291 BOOL WINAPI GetExtensionVersion(HSE_VERS | Line 318 BOOL WINAPI GetExtensionVersion(HSE_VERS |
| @test | @test |
| PARSER_VERSION from outside | PARSER_VERSION from outside |
| */ | */ |
| #ifndef PA_DEBUG_DISABLE_GC | |
| extern long GC_large_alloc_warn_suppressed; | |
| #endif | |
| void real_parser_handler(SAPI_Info& SAPI_info, bool header_only) { | 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; | |
| GC_large_alloc_warn_suppressed=0; | |
| } | |
| #endif | |
| SAPI_info.header=new String; | SAPI_info.header=new String; |
| LPEXTENSION_CONTROL_BLOCK lpECB=SAPI_info.lpECB; | LPEXTENSION_CONTROL_BLOCK lpECB=SAPI_info.lpECB; |
| Line 362 void real_parser_handler(SAPI_Info& SAPI | Line 403 void real_parser_handler(SAPI_Info& SAPI |
| header_only); | header_only); |
| } | } |
| void call_real_parser_handler__do_SEH(SAPI_Info& SAPI_info, bool header_only) { | #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION |
| #if _MSC_VER & !defined(_DEBUG) | 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; | LPEXCEPTION_POINTERS system_exception=0; |
| __try { | __try { |
| #endif | parser_exception=call_real_parser_handler__do_PEH_return_it( |
| real_parser_handler(SAPI_info, header_only); | SAPI_info, header_only); |
| #if _MSC_VER & !defined(_DEBUG) | |
| } __except ( | } __except ( |
| (system_exception=GetExceptionInformation()), | (system_exception=GetExceptionInformation()), |
| EXCEPTION_EXECUTE_HANDLER) { | EXCEPTION_EXECUTE_HANDLER) |
| { | |
| if(system_exception) | if(system_exception) |
| if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord) | if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord) |
| throw Exception(0, | throw Exception("system", |
| 0, | 0, |
| "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress); | "0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress); |
| else | else |
| throw Exception(0, 0, "Exception <no exception record>"); | throw Exception("system", |
| else | 0, |
| throw Exception(0, 0, "Exception <no exception information>"); | "<no exception record>"); |
| else | |
| throw Exception("system", | |
| 0, | |
| "<no exception information>"); | |
| } | } |
| #endif | |
| if(parser_exception) | |
| throw Exception(parser_exception); | |
| } | } |
| #endif | |
| DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) { | DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) { |
| //_asm int 3; | //_asm int 3; |
| Line 397 DWORD WINAPI HttpExtensionProc(LPEXTENSI | Line 460 DWORD WINAPI HttpExtensionProc(LPEXTENSI |
| bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0; | bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0; |
| try { // global try | 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 | // successful finish |
| } catch(const Exception& e) { // global problem | } catch(const Exception& e) { // global problem |
| // don't allocate anything on pool here: | // don't allocate anything on pool here: |
| Line 414 DWORD WINAPI HttpExtensionProc(LPEXTENSI | Line 482 DWORD WINAPI HttpExtensionProc(LPEXTENSI |
| char header_buf[MAX_STRING]; | char header_buf[MAX_STRING]; |
| int header_len=snprintf(header_buf, MAX_STRING, | int header_len=snprintf(header_buf, MAX_STRING, |
| "content-type: text/plain\r\n" | "content-type: text/plain\r\n" |
| "content-length: %lu\r\n" | "content-length: %u\r\n" |
| // "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n" | // "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n" |
| "\r\n", | "\r\n", |
| content_length); | content_length); |
| Line 447 DWORD WINAPI HttpExtensionProc(LPEXTENSI | Line 515 DWORD WINAPI HttpExtensionProc(LPEXTENSI |
| char header_buf[MAX_STRING]; | char header_buf[MAX_STRING]; |
| int header_len=snprintf(header_buf, MAX_STRING, | int header_len=snprintf(header_buf, MAX_STRING, |
| "content-type: text/plain\r\n" | "content-type: text/plain\r\n" |
| "content-length: %lu\r\n" | "content-length: %u\r\n" |
| "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n" | "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n" |
| "\r\n", | "\r\n", |
| content_length); | content_length); |
| Line 473 DWORD WINAPI HttpExtensionProc(LPEXTENSI | Line 541 DWORD WINAPI HttpExtensionProc(LPEXTENSI |
| BOOL WINAPI DllMain( | BOOL WINAPI DllMain( |
| HINSTANCE hinstDLL, // handle to the DLL module | HINSTANCE hinstDLL, // handle to the DLL module |
| DWORD fdwReason, // reason for calling function | DWORD /*fdwReason*/, // reason for calling function |
| LPVOID lpvReserved // reserved | LPVOID /*lpvReserved*/ // reserved |
| ) { | ) { |
| GetModuleFileName( | GetModuleFileName( |
| hinstDLL, // handle to module | hinstDLL, // handle to module |
| argv0, // file name of module | argv0buf, // file name of module |
| sizeof(argv0) // size of buffer | sizeof(argv0buf) // size of buffer |
| ); | ); |
| argv0=argv0buf; | |
| if(strncmp(argv0buf, "\\\\?\\", 4)==0) | |
| argv0+=4; | |
| return TRUE; | return TRUE; |
| } | } |