Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.119
1.29 paf 1: /** @file
2: Parser: IIS extension.
3:
1.109 moko 4: Copyright (c) 2000-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.63 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.77 paf 6: */
1.29 paf 7:
1.119 ! moko 8: volatile const char * IDENT_PARSER3ISAPI_C="$Id: parser3isapi.C,v 1.118 2020/10/26 23:15:52 moko Exp $";
1.29 paf 9:
1.1 paf 10: #ifndef _MSC_VER
1.10 paf 11: # error compile ISAPI module with MSVC [no urge for now to make it autoconf-ed (PAF)]
1.1 paf 12: #endif
1.43 parser 13:
14: #include "pa_config_includes.h"
1.1 paf 15:
1.9 paf 16: #include "pa_sapi.h"
1.1 paf 17: #include "pa_globals.h"
18: #include "pa_request.h"
19: #include "pa_version.h"
20:
1.64 paf 21: #include <windows.h>
22: #include <process.h>
23:
24: #include <httpext.h>
1.51 parser 25:
1.92 paf 26: // defines
27:
1.110 moko 28: #if defined(_MSC_VER) && !defined(_DEBUG)
1.92 paf 29: # define PA_SUPPRESS_SYSTEM_EXCEPTION
30: #endif
31:
32:
1.1 paf 33: #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")
1.44 parser 34:
35: // consts
1.1 paf 36:
1.83 paf 37: const char* IIS51vars[]={
1.73 paf 38: "APPL_MD_PATH", "APPL_PHYSICAL_PATH",
39: "AUTH_PASSWORD", "AUTH_TYPE", "AUTH_USER",
40: "CERT_COOKIE", "CERT_FLAGS", "CERT_ISSUER", "CERT_KEYSIZE", "CERT_SECRETKEYSIZE",
41: "CERT_SERIALNUMBER", "CERT_SERVER_ISSUER", "CERT_SERVER_SUBJECT", "CERT_SUBJECT",
42: "CONTENT_LENGTH", "CONTENT_TYPE",
1.107 moko 43: "GATEWAY_INTERFACE",
1.73 paf 44: "HTTPS", "HTTPS_KEYSIZE", "HTTPS_SECRETKEYSIZE", "HTTPS_SERVER_ISSUER", "HTTPS_SERVER_SUBJECT",
45: "INSTANCE_ID", "INSTANCE_META_PATH",
1.107 moko 46: "LOCAL_ADDR", "LOGON_USER",
1.73 paf 47: "PATH_INFO", "PATH_TRANSLATED",
48: "QUERY_STRING",
1.107 moko 49: "REMOTE_ADDR", "REMOTE_HOST", "REMOTE_PORT", "REMOTE_USER", "REQUEST_METHOD",
1.73 paf 50: "SCRIPT_NAME",
51: "SERVER_NAME", "SERVER_PORT", "SERVER_PORT_SECURE", "SERVER_PROTOCOL", "SERVER_SOFTWARE",
52: "URL",
53: };
54: const int IIS51var_count=sizeof(IIS51vars)/sizeof(*IIS51vars);
55:
1.65 paf 56: // globals
57:
58: char argv0[MAX_STRING]="";
59:
1.18 paf 60: // SAPI
61:
1.38 parser 62: #ifndef DOXYGEN
63: /*
1.18 paf 64: ISAPI SAPI functions receive this context information.
1.38 parser 65: see Pool::set_context
1.18 paf 66: */
1.83 paf 67: class SAPI_Info {
68: public:
1.3 paf 69: LPEXTENSION_CONTROL_BLOCK lpECB;
70: String *header;
71: DWORD http_response_code;
72: };
1.38 parser 73: #endif
1.3 paf 74:
1.26 paf 75: // goes to 'cs-uri-query' log file field. webmaster: switch it ON[default OFF].
1.83 paf 76: void SAPI::log(SAPI_Info& SAPI_info, const char* fmt, ...) {
1.26 paf 77: va_list args;
78: va_start(args,fmt);
1.99 misha 79: char buf[MAX_LOG_STRING];
1.83 paf 80: const char* prefix="PARSER_ERROR:";
1.26 paf 81: strcpy(buf, prefix);
1.47 parser 82: char *start=buf+strlen(prefix);
1.99 misha 83: DWORD size=vsnprintf(start, MAX_LOG_STRING-strlen(prefix), fmt, args);
84: size=remove_crlf(start, start+size);
1.47 parser 85:
1.114 moko 86: SAPI_info.lpECB->ServerSupportFunction(SAPI_info.lpECB->ConnID, HSE_APPEND_LOG_PARAMETER, buf, &size, 0);
1.55 parser 87: }
88:
89: /// @todo event log
1.114 moko 90: void SAPI::die(const char* fmt, ...) {
91: va_list args;
92: va_start(args, fmt);
1.83 paf 93: if(FILE *log=fopen("c:\\parser3die.log", "at")) {
1.65 paf 94: vfprintf(log, fmt, args);
95: fclose(log);
96: }
1.114 moko 97: // abnormal exit
1.81 paf 98: abort();
1.90 paf 99: // va_end(args);
1.83 paf 100: }
1.1 paf 101:
1.107 moko 102: char* SAPI::Env::get(SAPI_Info& SAPI_info, const char* name) {
1.83 paf 103: char *variable_buf=new(PointerFreeGC) char[MAX_STRING];
1.1 paf 104: DWORD variable_len = MAX_STRING-1;
105:
1.83 paf 106: if(SAPI_info.lpECB->GetServerVariable(SAPI_info.lpECB->ConnID, const_cast<char *>(name),
1.1 paf 107: variable_buf, &variable_len)) {
1.73 paf 108: if(*variable_buf) { // saw returning len=1 && *buf=0 :(
109: variable_buf[variable_len]=0;
110: return variable_buf;
111: }
1.7 paf 112: } else if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1.83 paf 113: variable_buf=new(PointerFreeGC) char[variable_len+1];
1.7 paf 114:
1.83 paf 115: if(SAPI_info.lpECB->GetServerVariable(SAPI_info.lpECB->ConnID, const_cast<char *>(name),
1.7 paf 116: variable_buf, &variable_len)) {
1.73 paf 117: if(*variable_buf) {
118: variable_buf[variable_len]=0;
119: return variable_buf;
120: }
1.7 paf 121: }
1.1 paf 122: }
1.7 paf 123:
1.1 paf 124: return 0;
125: }
126:
1.118 moko 127: bool SAPI::Env::set(SAPI_Info&, const char*, const char*) {
128: return false;
129: }
130:
1.83 paf 131: static int grep_char(const char* s, char c) {
1.72 paf 132: int result=0;
133: if(s) {
134: while(s=strchr(s, c)) {
135: s++; // skip found c
136: result++;
137: }
138: }
139: return result;
140: }
1.83 paf 141: static const char* mk_env_pair(const char* key, const char* value) {
142: char *result=new(PointerFreeGC) char[strlen(key)+1/*=*/+strlen(value)+1/*0*/];
1.72 paf 143: strcpy(result, key); strcat(result, "="); strcat(result, value);
144: return result;
145: }
1.107 moko 146: const char* const *SAPI::Env::get(SAPI_Info& info) {
1.72 paf 147: // we know this buf is writable
1.107 moko 148: char* all_http_vars=SAPI::Env::get(info, "ALL_HTTP");
1.72 paf 149: const int http_var_count=grep_char(all_http_vars, '\n')+1/*\n for theoretical(never saw) this \0*/;
1.90 paf 150:
1.92 paf 151: const char* *result=new(PointerFreeGC) const char*[IIS51var_count+http_var_count+1/*0*/];
1.83 paf 152: const char* *cur=result;
1.72 paf 153:
154: // IIS5.1 vars
155: for(int i=0; i<IIS51var_count; i++) {
1.83 paf 156: const char* key=IIS51vars[i];
1.107 moko 157: if(const char* value=SAPI::Env::get(info, key))
1.83 paf 158: *cur++=mk_env_pair(key, value);
1.72 paf 159: }
160:
161: // HTTP_* vars
162: if(char *s=all_http_vars) {
163: while(char *key=lsplit(&s, '\n'))
164: if(char *value=lsplit(key, ':'))
1.83 paf 165: *cur++=mk_env_pair(key, value);
1.72 paf 166: }
167:
168: // mark EOE
169: *cur=0;
170:
171: return result;
172: }
173:
1.83 paf 174: size_t SAPI::read_post(SAPI_Info& SAPI_info, char *buf, size_t max_bytes) {
1.1 paf 175: DWORD read_from_buf=0;
176: DWORD read_from_input=0;
177: DWORD total_read=0;
178:
1.83 paf 179: read_from_buf=min(SAPI_info.lpECB->cbAvailable, max_bytes);
180: memcpy(buf, SAPI_info.lpECB->lpbData, read_from_buf);
1.1 paf 181: total_read+=read_from_buf;
182:
183: if(read_from_buf<max_bytes &&
1.83 paf 184: read_from_buf<SAPI_info.lpECB->cbTotalBytes) {
1.1 paf 185: DWORD cbRead=0, cbSize;
186:
1.3 paf 187: read_from_input=min(max_bytes-read_from_buf,
1.83 paf 188: SAPI_info.lpECB->cbTotalBytes-read_from_buf);
1.1 paf 189: while(cbRead < read_from_input) {
190: cbSize=read_from_input - cbRead;
1.83 paf 191: if(!SAPI_info.lpECB->ReadClient(SAPI_info.lpECB->ConnID,
1.3 paf 192: buf+read_from_buf+cbRead, &cbSize) ||
1.1 paf 193: cbSize==0)
194: break;
195: cbRead+=cbSize;
196: }
197: total_read+=cbRead;
198: }
199: return total_read;
200: }
201:
1.117 moko 202: void SAPI::add_header_attribute(SAPI_Info& SAPI_info, const char* dont_store_key, const char* dont_store_value) {
203: if(strcasecmp(dont_store_key, "location")==0)
1.83 paf 204: SAPI_info.http_response_code=302;
205:
1.117 moko 206: if(strcasecmp(dont_store_key, HTTP_STATUS)==0)
207: SAPI_info.http_response_code=atoi(dont_store_value);
1.83 paf 208: else
1.102 misha 209: (*SAPI_info.header) << capitalize(dont_store_key) << ": " << pa_strdup(dont_store_value) << "\r\n";
1.1 paf 210: }
211:
1.23 paf 212: /// @todo intelligent cache-control
1.83 paf 213: void SAPI::send_header(SAPI_Info& SAPI_info) {
1.64 paf 214: HSE_SEND_HEADER_EX_INFO header_info;
1.1 paf 215:
216: char status_buf[MAX_STATUS_LENGTH];
1.83 paf 217: switch(SAPI_info.http_response_code) {
1.1 paf 218: case 200:
219: header_info.pszStatus="200 OK";
220: break;
221: case 302:
222: header_info.pszStatus="302 Moved Temporarily";
223: break;
1.8 paf 224: case 401:// useless untill parser auth mech
1.1 paf 225: header_info.pszStatus="401 Authorization Required";
1.8 paf 226: break;
1.1 paf 227: default:
1.3 paf 228: snprintf(status_buf, MAX_STATUS_LENGTH,
1.83 paf 229: "%d Undescribed", SAPI_info.http_response_code);
1.1 paf 230: header_info.pszStatus=status_buf;
231: break;
232: }
233: header_info.cchStatus=strlen(header_info.pszStatus);
1.83 paf 234: *SAPI_info.header << "\r\n"; // ISAPI v<5 did quite well without it
235: header_info.pszHeader=SAPI_info.header->cstr();
236: header_info.cchHeader=SAPI_info.header->length();
1.5 paf 237: header_info.fKeepConn=true;
1.1 paf 238:
1.83 paf 239: SAPI_info.lpECB->dwHttpStatusCode=SAPI_info.http_response_code;
1.1 paf 240:
1.83 paf 241: SAPI_info.lpECB->ServerSupportFunction(SAPI_info.lpECB->ConnID,
1.3 paf 242: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 243: }
244:
1.91 paf 245: size_t SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) {
1.1 paf 246: DWORD num_bytes=size;
1.91 paf 247: if(!SAPI_info.lpECB->WriteClient(SAPI_info.lpECB->ConnID,
248: const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC))
249: return 0;
250: return (size_t)num_bytes;
1.1 paf 251: }
1.16 paf 252:
1.1 paf 253:
1.15 paf 254: static bool parser_init() {
1.1 paf 255: static bool globals_inited=false;
256: if(globals_inited)
1.15 paf 257: return true;
1.1 paf 258: globals_inited=true;
259:
1.53 parser 260: try {
1.111 moko 261: // init libraries
1.83 paf 262: pa_globals_init();
1.15 paf 263: // successful finish
264: return true;
1.96 paf 265: } catch(.../*const Exception& e*/) { // global problem
1.15 paf 266: // unsuccessful finish
267: return false;
1.1 paf 268: }
269: }
270:
1.89 paf 271: static void parser_done() {
1.111 moko 272: // finalize libraries
1.89 paf 273: pa_globals_done();
274: }
275:
1.1 paf 276: /// ISAPI //
277: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
278: pVer->dwExtensionVersion = HSE_VERSION;
1.36 parser 279: strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
280: pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15 paf 281: return parser_init();
1.89 paf 282: }
283: // dwFlags & HSE_TERM_MUST_UNLOAD means we can't return false
284: BOOL WINAPI TerminateExtension(
285: DWORD /*dwFlags*/
286: )
287: {
288: parser_done();
289:
290: return TRUE;
1.1 paf 291: }
292:
1.6 paf 293: /**
294: ISAPI // main workhorse
295:
1.23 paf 296: @todo
1.10 paf 297: IIS: remove trailing default-document[index.html] from $request.uri.
298: to do that we need to consult metabase,
1.12 paf 299: wich is tested&works but seems slow runtime
300: and not could-be-quickly-implemented if prepared.
1.37 parser 301: @test
302: PARSER_VERSION from outside
1.6 paf 303: */
1.83 paf 304: void real_parser_handler(SAPI_Info& SAPI_info, bool header_only) {
1.88 paf 305: // collect garbage from prev request
306: #ifndef PA_DEBUG_DISABLE_GC
1.112 moko 307: GC_dont_gc=0;
308: GC_gcollect();
309: GC_dont_gc=1;
1.88 paf 310: #endif
311:
1.83 paf 312: SAPI_info.header=new String;
313: LPEXTENSION_CONTROL_BLOCK lpECB=SAPI_info.lpECB;
1.53 parser 314:
315: // Request info
1.83 paf 316: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
1.53 parser 317:
318: size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
1.83 paf 319: char *filespec_to_process=pa_strdup(lpECB->lpszPathTranslated, path_translated_buf_size);
1.53 parser 320: #ifdef WIN32
321: back_slashes_to_slashes(filespec_to_process);
322: #endif
323:
1.107 moko 324: if(const char* path_info=SAPI::Env::get(SAPI_info, "PATH_INFO")) {
1.53 parser 325: // IIS
326: size_t len=strlen(filespec_to_process)-strlen(path_info);
1.83 paf 327: char *buf=new(PointerFreeGC) char[len+1];
1.53 parser 328: strncpy(buf, filespec_to_process, len); buf[len]=0;
329: request_info.document_root=buf;
330: } else
1.98 misha 331: throw Exception(PARSER_RUNTIME,
1.53 parser 332: 0,
333: "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
334:
335: request_info.path_translated=filespec_to_process;
336: request_info.method=lpECB->lpszMethod;
337: request_info.query_string=lpECB->lpszQueryString;
338: if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
1.83 paf 339: char *reconstructed_uri=new(PointerFreeGC) char[
1.53 parser 340: strlen(lpECB->lpszPathInfo)+1/*'?'*/+
1.83 paf 341: strlen(lpECB->lpszQueryString)+1/*0*/];
1.53 parser 342: strcpy(reconstructed_uri, lpECB->lpszPathInfo);
343: strcat(reconstructed_uri, "?");
344: strcat(reconstructed_uri, lpECB->lpszQueryString);
345: request_info.uri=reconstructed_uri;
346: } else
347: request_info.uri=lpECB->lpszPathInfo;
348:
349: request_info.content_type=lpECB->lpszContentType;
350: request_info.content_length=lpECB->cbTotalBytes;
1.107 moko 351: request_info.cookie=SAPI::Env::get(SAPI_info, "HTTP_COOKIE");
1.76 paf 352: request_info.mail_received=false;
1.53 parser 353:
354: // prepare to process request
1.119 ! moko 355: Request request(SAPI_info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
1.53 parser 356:
1.65 paf 357: // beside by binary
1.75 paf 358: static char beside_binary_path[MAX_STRING];
359: strncpy(beside_binary_path, argv0, MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
1.119 ! moko 360: if(!(rsplit(beside_binary_path, '/') || rsplit(beside_binary_path, '\\'))) { // strip filename
1.65 paf 361: // no path, just filename
1.75 paf 362: beside_binary_path[0]='.'; beside_binary_path[1]=0;
1.119 ! moko 363: }
1.74 paf 364: char config_filespec[MAX_STRING];
1.119 ! moko 365: snprintf(config_filespec, MAX_STRING, "%s/%s", beside_binary_path, AUTO_FILE_NAME);
1.65 paf 366:
1.53 parser 367: // process the request
1.119 ! moko 368: request.core(entry_exists(config_filespec) ? config_filespec : NULL, header_only);
1.53 parser 369: }
370:
1.92 paf 371: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
1.113 moko 372: static const Exception call_real_parser_handler__do_PEH_return_it(SAPI_Info& SAPI_info, bool header_only) {
1.92 paf 373: try {
374: real_parser_handler(SAPI_info, header_only);
375: } catch(const Exception& e) {
376: return e;
377: }
378:
379: return Exception();
380: }
1.113 moko 381:
382: static void call_real_parser_handler__supress_system_exception(SAPI_Info& SAPI_info, bool header_only) {
1.92 paf 383: Exception parser_exception;
1.53 parser 384: LPEXCEPTION_POINTERS system_exception=0;
1.92 paf 385:
1.53 parser 386: __try {
1.113 moko 387: parser_exception=call_real_parser_handler__do_PEH_return_it(SAPI_info, header_only);
388: } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
1.53 parser 389: if(system_exception)
390: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.113 moko 391: throw Exception("system", 0, "0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
1.53 parser 392: else
1.113 moko 393: throw Exception("system", 0, "<no exception record>");
1.92 paf 394: else
1.113 moko 395: throw Exception("system", 0, "<no exception information>");
1.53 parser 396: }
1.92 paf 397:
398: if(parser_exception)
399: throw Exception(parser_exception);
400: }
1.53 parser 401: #endif
402:
1.83 paf 403: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
404: //_asm int 3;
405: SAPI_Info SAPI_info={
1.15 paf 406: lpECB,
1.83 paf 407: 0, // filling later: so that if there would be error pool would have SAPI_info
1.80 paf 408: 200 // default http_response_code [lpECB->dwHttpStatusCode seems to be always 0, even on 404 redirect to /404.html]
1.15 paf 409: };
1.71 paf 410:
1.1 paf 411: bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
1.53 parser 412: try { // global try
1.92 paf 413: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
414: call_real_parser_handler__supress_system_exception(
415: #else
416: real_parser_handler(
417: #endif
418: SAPI_info, header_only);
1.2 paf 419: // successful finish
1.116 moko 420: } catch(const Exception& e) { // exception in unhandled exception
1.16 paf 421: // log it
1.116 moko 422: SAPI::log(SAPI_info, "%s", e.comment());
1.12 paf 423:
424: HSE_SEND_HEADER_EX_INFO header_info;
425: header_info.pszStatus="200 OK";
426: header_info.cchStatus=strlen(header_info.pszStatus);
1.116 moko 427: header_info.pszHeader=HTTP_CONTENT_TYPE_CAPITALIZED ": text/plain\r\n\r\n";
428: header_info.cchHeader=strlen(header_info.pszHeader);
1.12 paf 429: header_info.fKeepConn=true;
430:
1.1 paf 431: // send header
1.12 paf 432: lpECB->dwHttpStatusCode=200;
1.116 moko 433: lpECB->ServerSupportFunction(lpECB->ConnID, HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 434:
435: // send body
436: if(!header_only)
1.116 moko 437: SAPI::send_body(SAPI_info, e.comment(), strlen(e.comment()));
1.1 paf 438:
439: // unsuccessful finish
440: }
1.71 paf 441: return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1 paf 442: }
1.65 paf 443:
444: BOOL WINAPI DllMain(
445: HINSTANCE hinstDLL, // handle to the DLL module
1.85 paf 446: DWORD /*fdwReason*/, // reason for calling function
447: LPVOID /*lpvReserved*/ // reserved
1.65 paf 448: ) {
449:
450: GetModuleFileName(
451: hinstDLL, // handle to module
452: argv0, // file name of module
453: sizeof(argv0) // size of buffer
454: );
455:
456: return TRUE;
457: }
E-mail: