Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.91
1.29 paf 1: /** @file
2: Parser: IIS extension.
3:
1.87 paf 4: Copyright (c) 2000,2001-2004 ArtLebedev Group (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.91 ! paf 8: static const char * const IDENT_PARSER3ISAPI_C="$Date: 2004/04/05 13:37:06 $";
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"
1.24 paf 20: #include "pa_socks.h"
1.1 paf 21:
1.64 paf 22: #include <windows.h>
23: #include <process.h>
24:
25: #include <httpext.h>
1.51 parser 26:
1.1 paf 27: #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")
1.44 parser 28:
29: // consts
1.1 paf 30:
1.83 paf 31: const char* IIS51vars[]={
1.73 paf 32: "APPL_MD_PATH", "APPL_PHYSICAL_PATH",
33: "AUTH_PASSWORD", "AUTH_TYPE", "AUTH_USER",
34: "CERT_COOKIE", "CERT_FLAGS", "CERT_ISSUER", "CERT_KEYSIZE", "CERT_SECRETKEYSIZE",
35: "CERT_SERIALNUMBER", "CERT_SERVER_ISSUER", "CERT_SERVER_SUBJECT", "CERT_SUBJECT",
36: "CONTENT_LENGTH", "CONTENT_TYPE",
37: "LOGON_USER",
38: "HTTPS", "HTTPS_KEYSIZE", "HTTPS_SECRETKEYSIZE", "HTTPS_SERVER_ISSUER", "HTTPS_SERVER_SUBJECT",
39: "INSTANCE_ID", "INSTANCE_META_PATH",
40: "PATH_INFO", "PATH_TRANSLATED",
41: "QUERY_STRING",
42: "REMOTE_ADDR", "REMOTE_HOST", "REMOTE_USER", "REQUEST_METHOD",
43: "SCRIPT_NAME",
44: "SERVER_NAME", "SERVER_PORT", "SERVER_PORT_SECURE", "SERVER_PROTOCOL", "SERVER_SOFTWARE",
45: "URL",
46: };
47: const int IIS51var_count=sizeof(IIS51vars)/sizeof(*IIS51vars);
48:
1.65 paf 49: // globals
50:
51: char argv0[MAX_STRING]="";
52:
1.18 paf 53: // SAPI
54:
1.38 parser 55: #ifndef DOXYGEN
56: /*
1.18 paf 57: ISAPI SAPI functions receive this context information.
1.38 parser 58: see Pool::set_context
1.18 paf 59: */
1.83 paf 60: class SAPI_Info {
61: public:
1.3 paf 62: LPEXTENSION_CONTROL_BLOCK lpECB;
63: String *header;
64: DWORD http_response_code;
65: };
1.38 parser 66: #endif
1.3 paf 67:
1.26 paf 68: // goes to 'cs-uri-query' log file field. webmaster: switch it ON[default OFF].
1.83 paf 69: void SAPI::log(SAPI_Info& SAPI_info, const char* fmt, ...) {
1.26 paf 70: va_list args;
71: va_start(args,fmt);
72: char buf[MAX_STRING];
1.83 paf 73: const char* prefix="PARSER_ERROR:";
1.26 paf 74: strcpy(buf, prefix);
1.47 parser 75: char *start=buf+strlen(prefix);
1.51 parser 76: DWORD size=vsnprintf(start, MAX_STRING-strlen(prefix), fmt, args);
1.47 parser 77: remove_crlf(start, start+size);
78:
1.83 paf 79: SAPI_info.lpECB->ServerSupportFunction(SAPI_info.lpECB->ConnID,
1.26 paf 80: HSE_APPEND_LOG_PARAMETER, buf, &size, 0);
1.55 parser 81: }
82:
83: /// @todo event log
1.85 paf 84: static void abort(const char* fmt, va_list args) {
1.83 paf 85: if(FILE *log=fopen("c:\\parser3die.log", "at")) {
1.65 paf 86: vfprintf(log, fmt, args);
87: fclose(log);
88: }
1.81 paf 89: // exit & try to produce core dump
90: abort();
1.26 paf 91: }
1.83 paf 92: void SAPI::die(const char* fmt, ...) {
93: va_list args;
94: va_start(args, fmt);
1.85 paf 95: ::abort(fmt, args);
1.90 paf 96: // va_end(args);
1.83 paf 97: }
1.26 paf 98:
1.83 paf 99: void SAPI::abort(const char* fmt, ...) {
100: va_list args;
101: va_start(args, fmt);
1.85 paf 102: ::abort(fmt, args);
1.90 paf 103: // va_end(args);
1.83 paf 104: }
1.1 paf 105:
1.83 paf 106: char* SAPI::get_env(SAPI_Info& SAPI_info, const char* name) {
107: char *variable_buf=new(PointerFreeGC) char[MAX_STRING];
1.1 paf 108: DWORD variable_len = MAX_STRING-1;
109:
1.83 paf 110: if(SAPI_info.lpECB->GetServerVariable(SAPI_info.lpECB->ConnID, const_cast<char *>(name),
1.1 paf 111: variable_buf, &variable_len)) {
1.73 paf 112: if(*variable_buf) { // saw returning len=1 && *buf=0 :(
113: variable_buf[variable_len]=0;
114: return variable_buf;
115: }
1.7 paf 116: } else if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1.83 paf 117: variable_buf=new(PointerFreeGC) char[variable_len+1];
1.7 paf 118:
1.83 paf 119: if(SAPI_info.lpECB->GetServerVariable(SAPI_info.lpECB->ConnID, const_cast<char *>(name),
1.7 paf 120: variable_buf, &variable_len)) {
1.73 paf 121: if(*variable_buf) {
122: variable_buf[variable_len]=0;
123: return variable_buf;
124: }
1.7 paf 125: }
1.1 paf 126: }
1.7 paf 127:
1.1 paf 128: return 0;
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.83 paf 146: const char* const *SAPI::environment(SAPI_Info& info) {
1.72 paf 147: // we know this buf is writable
1.83 paf 148: char* all_http_vars=SAPI::get_env(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.83 paf 151: const char* *result=new const char*[IIS51var_count+http_var_count+1/*0*/];
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];
157: if(const char* value=SAPI::get_env(info, key))
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.83 paf 202: void SAPI::add_header_attribute(SAPI_Info& SAPI_info,
203: const char* dont_store_key, const char* dont_store_value) {
204: if(strcasecmp(dont_store_key, "location")==0)
205: SAPI_info.http_response_code=302;
206:
207: if(strcasecmp(dont_store_key, "status")==0)
208: SAPI_info.http_response_code=atoi(dont_store_value);
209: else
210: (*SAPI_info.header) << pa_strdup(dont_store_key) << ": " << pa_strdup(dont_store_value) << "\r\n";
1.1 paf 211: }
212:
1.23 paf 213: /// @todo intelligent cache-control
1.83 paf 214: void SAPI::send_header(SAPI_Info& SAPI_info) {
1.64 paf 215: HSE_SEND_HEADER_EX_INFO header_info;
1.1 paf 216:
217: char status_buf[MAX_STATUS_LENGTH];
1.83 paf 218: switch(SAPI_info.http_response_code) {
1.1 paf 219: case 200:
220: header_info.pszStatus="200 OK";
221: break;
222: case 302:
223: header_info.pszStatus="302 Moved Temporarily";
224: break;
1.8 paf 225: case 401:// useless untill parser auth mech
1.1 paf 226: header_info.pszStatus="401 Authorization Required";
1.8 paf 227: break;
1.1 paf 228: default:
1.3 paf 229: snprintf(status_buf, MAX_STATUS_LENGTH,
1.83 paf 230: "%d Undescribed", SAPI_info.http_response_code);
1.1 paf 231: header_info.pszStatus=status_buf;
232: break;
233: }
234: header_info.cchStatus=strlen(header_info.pszStatus);
1.83 paf 235: *SAPI_info.header << "\r\n"; // ISAPI v<5 did quite well without it
236: header_info.pszHeader=SAPI_info.header->cstr();
237: header_info.cchHeader=SAPI_info.header->length();
1.5 paf 238: header_info.fKeepConn=true;
1.1 paf 239:
1.83 paf 240: SAPI_info.lpECB->dwHttpStatusCode=SAPI_info.http_response_code;
1.1 paf 241:
1.83 paf 242: SAPI_info.lpECB->ServerSupportFunction(SAPI_info.lpECB->ConnID,
1.3 paf 243: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 244: }
245:
1.91 ! paf 246: size_t SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) {
1.1 paf 247: DWORD num_bytes=size;
1.91 ! paf 248: if(!SAPI_info.lpECB->WriteClient(SAPI_info.lpECB->ConnID,
! 249: const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC))
! 250: return 0;
! 251: return (size_t)num_bytes;
1.1 paf 252: }
1.16 paf 253:
1.1 paf 254:
1.15 paf 255: static bool parser_init() {
1.1 paf 256: static bool globals_inited=false;
257: if(globals_inited)
1.15 paf 258: return true;
1.1 paf 259: globals_inited=true;
260:
1.53 parser 261: try {
1.27 paf 262: // init socks
1.89 paf 263: pa_socks_init();
1.1 paf 264: // init global variables
1.83 paf 265: pa_globals_init();
1.65 paf 266:
1.15 paf 267: // successful finish
268: return true;
1.53 parser 269: } catch(const Exception& e) { // global problem
1.85 paf 270: //const char* body=e.comment();
1.15 paf 271:
272: // unsuccessful finish
273: return false;
1.1 paf 274: }
275: }
276:
1.89 paf 277: static void parser_done() {
278: // finalize global variables
279: pa_globals_done();
280:
281: //
282: pa_socks_done();
283: }
284:
1.1 paf 285: /// ISAPI //
286: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
287: pVer->dwExtensionVersion = HSE_VERSION;
1.36 parser 288: strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
289: pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15 paf 290: return parser_init();
1.89 paf 291: }
292: // dwFlags & HSE_TERM_MUST_UNLOAD means we can't return false
293: BOOL WINAPI TerminateExtension(
294: DWORD /*dwFlags*/
295: )
296: {
297: parser_done();
298:
299: return TRUE;
1.1 paf 300: }
301:
1.6 paf 302: /**
303: ISAPI // main workhorse
304:
1.23 paf 305: @todo
1.10 paf 306: IIS: remove trailing default-document[index.html] from $request.uri.
307: to do that we need to consult metabase,
1.12 paf 308: wich is tested&works but seems slow runtime
309: and not could-be-quickly-implemented if prepared.
1.37 parser 310: @test
311: PARSER_VERSION from outside
1.6 paf 312: */
1.53 parser 313:
1.83 paf 314: void real_parser_handler(SAPI_Info& SAPI_info, bool header_only) {
1.88 paf 315: // collect garbage from prev request
316: #ifndef PA_DEBUG_DISABLE_GC
317: {
318: int saved=GC_dont_gc;
319: GC_dont_gc=0;
320: GC_gcollect();
321: GC_dont_gc=saved;
322: }
323: #endif
324:
1.83 paf 325: SAPI_info.header=new String;
326: LPEXTENSION_CONTROL_BLOCK lpECB=SAPI_info.lpECB;
1.53 parser 327:
328: // Request info
1.83 paf 329: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
1.53 parser 330:
331: size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
1.83 paf 332: char *filespec_to_process=pa_strdup(lpECB->lpszPathTranslated, path_translated_buf_size);
1.53 parser 333: #ifdef WIN32
334: back_slashes_to_slashes(filespec_to_process);
335: #endif
336:
1.83 paf 337: if(const char* path_info=SAPI::get_env(SAPI_info, "PATH_INFO")) {
1.53 parser 338: // IIS
339: size_t len=strlen(filespec_to_process)-strlen(path_info);
1.83 paf 340: char *buf=new(PointerFreeGC) char[len+1];
1.53 parser 341: strncpy(buf, filespec_to_process, len); buf[len]=0;
342: request_info.document_root=buf;
343: } else
1.67 paf 344: throw Exception("parser.runtime",
1.53 parser 345: 0,
346: "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
347:
348: request_info.path_translated=filespec_to_process;
349: request_info.method=lpECB->lpszMethod;
350: request_info.query_string=lpECB->lpszQueryString;
351: if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
1.83 paf 352: char *reconstructed_uri=new(PointerFreeGC) char[
1.53 parser 353: strlen(lpECB->lpszPathInfo)+1/*'?'*/+
1.83 paf 354: strlen(lpECB->lpszQueryString)+1/*0*/];
1.53 parser 355: strcpy(reconstructed_uri, lpECB->lpszPathInfo);
356: strcat(reconstructed_uri, "?");
357: strcat(reconstructed_uri, lpECB->lpszQueryString);
358: request_info.uri=reconstructed_uri;
359: } else
360: request_info.uri=lpECB->lpszPathInfo;
361:
362: request_info.content_type=lpECB->lpszContentType;
363: request_info.content_length=lpECB->cbTotalBytes;
1.83 paf 364: request_info.cookie=SAPI::get_env(SAPI_info, "HTTP_COOKIE");
1.76 paf 365: request_info.mail_received=false;
1.53 parser 366:
367: // prepare to process request
1.83 paf 368: Request request(SAPI_info,
1.53 parser 369: request_info,
1.83 paf 370: String::Language(String::L_HTML|String::L_OPTIMIZE_BIT),
371: true /* status_allowed */);
1.53 parser 372:
1.65 paf 373: // beside by binary
1.75 paf 374: static char beside_binary_path[MAX_STRING];
375: strncpy(beside_binary_path, argv0, MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
1.65 paf 376: if(!(
1.75 paf 377: rsplit(beside_binary_path, '/') ||
378: rsplit(beside_binary_path, '\\'))) { // strip filename
1.65 paf 379: // no path, just filename
1.75 paf 380: beside_binary_path[0]='.'; beside_binary_path[1]=0;
1.65 paf 381: }
1.74 paf 382: char config_filespec[MAX_STRING];
383: snprintf(config_filespec, MAX_STRING,
1.65 paf 384: "%s/%s",
1.75 paf 385: beside_binary_path, AUTO_FILE_NAME);
1.79 paf 386: bool fail_on_config_read_problem=entry_exists(config_filespec);
1.65 paf 387:
1.53 parser 388: // process the request
389: request.core(
1.79 paf 390: config_filespec, fail_on_config_read_problem, // /path/to/first/auto.p
1.53 parser 391: header_only);
392: }
393:
1.83 paf 394: void call_real_parser_handler__do_SEH(SAPI_Info& SAPI_info, bool header_only) {
1.54 parser 395: #if _MSC_VER & !defined(_DEBUG)
1.53 parser 396: LPEXCEPTION_POINTERS system_exception=0;
397: __try {
398: #endif
1.83 paf 399: real_parser_handler(SAPI_info, header_only);
1.53 parser 400:
1.54 parser 401: #if _MSC_VER & !defined(_DEBUG)
1.53 parser 402: } __except (
403: (system_exception=GetExceptionInformation()),
404: EXCEPTION_EXECUTE_HANDLER) {
405:
406: if(system_exception)
407: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.67 paf 408: throw Exception(0,
1.53 parser 409: 0,
410: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
411: else
1.67 paf 412: throw Exception(0, 0, "Exception <no exception record>");
1.53 parser 413: else
1.67 paf 414: throw Exception(0, 0, "Exception <no exception information>");
1.53 parser 415: }
416: #endif
417: }
418:
1.83 paf 419: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
420: //_asm int 3;
421: SAPI_Info SAPI_info={
1.15 paf 422: lpECB,
1.83 paf 423: 0, // filling later: so that if there would be error pool would have SAPI_info
1.80 paf 424: 200 // default http_response_code [lpECB->dwHttpStatusCode seems to be always 0, even on 404 redirect to /404.html]
1.15 paf 425: };
1.71 paf 426:
1.1 paf 427: bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
1.53 parser 428: try { // global try
1.83 paf 429: call_real_parser_handler__do_SEH(SAPI_info, header_only);
1.2 paf 430: // successful finish
1.53 parser 431: } catch(const Exception& e) { // global problem
1.12 paf 432: // don't allocate anything on pool here:
433: // possible pool' exception not catch-ed now
1.31 paf 434: // and there could be out-of-memory exception
1.83 paf 435: const char* body=e.comment();
1.16 paf 436: // log it
1.83 paf 437: SAPI::log(SAPI_info, "exception in request exception handler: %s", body);
1.16 paf 438:
439: //
1.1 paf 440: int content_length=strlen(body);
441:
1.12 paf 442: // prepare header // not using SAPI func wich allocates on pool
443: char header_buf[MAX_STRING];
444: int header_len=snprintf(header_buf, MAX_STRING,
1.30 paf 445: "content-type: text/plain\r\n"
1.86 paf 446: "content-length: %u\r\n"
1.69 paf 447: // "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
1.30 paf 448: "\r\n",
1.12 paf 449: content_length);
450:
451: HSE_SEND_HEADER_EX_INFO header_info;
452: header_info.pszStatus="200 OK";
453: header_info.cchStatus=strlen(header_info.pszStatus);
454: header_info.pszHeader=header_buf;
455: header_info.cchHeader=header_len;
456: header_info.fKeepConn=true;
457:
1.1 paf 458: // send header
1.12 paf 459: lpECB->dwHttpStatusCode=200;
460: lpECB->ServerSupportFunction(lpECB->ConnID,
461: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 462:
463: // send body
464: if(!header_only)
1.83 paf 465: SAPI::send_body(SAPI_info, body, content_length);
1.1 paf 466:
467: // unsuccessful finish
468: }
1.65 paf 469: /*
1.83 paf 470: const char* body="test";
1.65 paf 471:
472: //
473: int content_length=strlen(body);
474:
475: // prepare header // not using SAPI func wich allocates on pool
476: char header_buf[MAX_STRING];
477: int header_len=snprintf(header_buf, MAX_STRING,
478: "content-type: text/plain\r\n"
1.86 paf 479: "content-length: %u\r\n"
1.65 paf 480: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
481: "\r\n",
482: content_length);
483: HSE_SEND_HEADER_EX_INFO header_info;
484: header_info.pszStatus="200 OK";
485: header_info.cchStatus=strlen(header_info.pszStatus);
486: header_info.pszHeader=header_buf;
487: header_info.cchHeader=header_len;
488: header_info.fKeepConn=true;
489:
490: // send header
491: lpECB->dwHttpStatusCode=200;
492: lpECB->ServerSupportFunction(lpECB->ConnID,
493: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
494:
495: // send body
496: DWORD num_bytes=content_length;
497: lpECB->WriteClient(lpECB->ConnID,
498: (void *)body, &num_bytes, HSE_IO_SYNC);
499: */
1.71 paf 500: return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1 paf 501: }
1.65 paf 502:
503: BOOL WINAPI DllMain(
504: HINSTANCE hinstDLL, // handle to the DLL module
1.85 paf 505: DWORD /*fdwReason*/, // reason for calling function
506: LPVOID /*lpvReserved*/ // reserved
1.65 paf 507: ) {
508:
509: GetModuleFileName(
510: hinstDLL, // handle to module
511: argv0, // file name of module
512: sizeof(argv0) // size of buffer
513: );
514:
515: return TRUE;
516: }
E-mail: