Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.87
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.87 ! paf 8: static const char * const IDENT_PARSER3ISAPI_C="$Date: 2004/02/03 16:45:25 $";
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.83 paf 96: va_end(args);
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.83 paf 103: va_end(args);
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*/;
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.83 paf 246: void SAPI::send_body(SAPI_Info& SAPI_info, const void *buf, size_t size) {
1.1 paf 247: DWORD num_bytes=size;
1.83 paf 248: SAPI_info.lpECB->WriteClient(SAPI_info.lpECB->ConnID,
1.23 paf 249: const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC);
1.1 paf 250: }
1.16 paf 251:
1.1 paf 252:
1.15 paf 253: static bool parser_init() {
1.1 paf 254: static bool globals_inited=false;
255: if(globals_inited)
1.15 paf 256: return true;
1.1 paf 257: globals_inited=true;
258:
1.53 parser 259: try {
1.27 paf 260: // init socks
1.83 paf 261: pa_init_socks();
1.1 paf 262: // init global variables
1.83 paf 263: pa_globals_init();
1.65 paf 264:
1.15 paf 265: // successful finish
266: return true;
1.53 parser 267: } catch(const Exception& e) { // global problem
1.85 paf 268: //const char* body=e.comment();
1.15 paf 269:
270: // unsuccessful finish
271: return false;
1.1 paf 272: }
273: }
274:
275: /// ISAPI //
276: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
277: pVer->dwExtensionVersion = HSE_VERSION;
1.36 parser 278: strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
279: pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15 paf 280: return parser_init();
1.1 paf 281: }
282:
1.6 paf 283: /**
284: ISAPI // main workhorse
285:
1.23 paf 286: @todo
1.10 paf 287: IIS: remove trailing default-document[index.html] from $request.uri.
288: to do that we need to consult metabase,
1.12 paf 289: wich is tested&works but seems slow runtime
290: and not could-be-quickly-implemented if prepared.
1.37 parser 291: @test
292: PARSER_VERSION from outside
1.6 paf 293: */
1.53 parser 294:
1.83 paf 295: void real_parser_handler(SAPI_Info& SAPI_info, bool header_only) {
296: SAPI_info.header=new String;
297: LPEXTENSION_CONTROL_BLOCK lpECB=SAPI_info.lpECB;
1.53 parser 298:
299: // Request info
1.83 paf 300: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
1.53 parser 301:
302: size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
1.83 paf 303: char *filespec_to_process=pa_strdup(lpECB->lpszPathTranslated, path_translated_buf_size);
1.53 parser 304: #ifdef WIN32
305: back_slashes_to_slashes(filespec_to_process);
306: #endif
307:
1.83 paf 308: if(const char* path_info=SAPI::get_env(SAPI_info, "PATH_INFO")) {
1.53 parser 309: // IIS
310: size_t len=strlen(filespec_to_process)-strlen(path_info);
1.83 paf 311: char *buf=new(PointerFreeGC) char[len+1];
1.53 parser 312: strncpy(buf, filespec_to_process, len); buf[len]=0;
313: request_info.document_root=buf;
314: } else
1.67 paf 315: throw Exception("parser.runtime",
1.53 parser 316: 0,
317: "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
318:
319: request_info.path_translated=filespec_to_process;
320: request_info.method=lpECB->lpszMethod;
321: request_info.query_string=lpECB->lpszQueryString;
322: if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
1.83 paf 323: char *reconstructed_uri=new(PointerFreeGC) char[
1.53 parser 324: strlen(lpECB->lpszPathInfo)+1/*'?'*/+
1.83 paf 325: strlen(lpECB->lpszQueryString)+1/*0*/];
1.53 parser 326: strcpy(reconstructed_uri, lpECB->lpszPathInfo);
327: strcat(reconstructed_uri, "?");
328: strcat(reconstructed_uri, lpECB->lpszQueryString);
329: request_info.uri=reconstructed_uri;
330: } else
331: request_info.uri=lpECB->lpszPathInfo;
332:
333: request_info.content_type=lpECB->lpszContentType;
334: request_info.content_length=lpECB->cbTotalBytes;
1.83 paf 335: request_info.cookie=SAPI::get_env(SAPI_info, "HTTP_COOKIE");
1.76 paf 336: request_info.mail_received=false;
1.53 parser 337:
338: // prepare to process request
1.83 paf 339: Request request(SAPI_info,
1.53 parser 340: request_info,
1.83 paf 341: String::Language(String::L_HTML|String::L_OPTIMIZE_BIT),
342: true /* status_allowed */);
1.53 parser 343:
1.65 paf 344: // beside by binary
1.75 paf 345: static char beside_binary_path[MAX_STRING];
346: strncpy(beside_binary_path, argv0, MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
1.65 paf 347: if(!(
1.75 paf 348: rsplit(beside_binary_path, '/') ||
349: rsplit(beside_binary_path, '\\'))) { // strip filename
1.65 paf 350: // no path, just filename
1.75 paf 351: beside_binary_path[0]='.'; beside_binary_path[1]=0;
1.65 paf 352: }
1.74 paf 353: char config_filespec[MAX_STRING];
354: snprintf(config_filespec, MAX_STRING,
1.65 paf 355: "%s/%s",
1.75 paf 356: beside_binary_path, AUTO_FILE_NAME);
1.79 paf 357: bool fail_on_config_read_problem=entry_exists(config_filespec);
1.65 paf 358:
1.53 parser 359: // process the request
360: request.core(
1.79 paf 361: config_filespec, fail_on_config_read_problem, // /path/to/first/auto.p
1.53 parser 362: header_only);
363: }
364:
1.83 paf 365: void call_real_parser_handler__do_SEH(SAPI_Info& SAPI_info, bool header_only) {
1.54 parser 366: #if _MSC_VER & !defined(_DEBUG)
1.53 parser 367: LPEXCEPTION_POINTERS system_exception=0;
368: __try {
369: #endif
1.83 paf 370: real_parser_handler(SAPI_info, header_only);
1.53 parser 371:
1.54 parser 372: #if _MSC_VER & !defined(_DEBUG)
1.53 parser 373: } __except (
374: (system_exception=GetExceptionInformation()),
375: EXCEPTION_EXECUTE_HANDLER) {
376:
377: if(system_exception)
378: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.67 paf 379: throw Exception(0,
1.53 parser 380: 0,
381: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
382: else
1.67 paf 383: throw Exception(0, 0, "Exception <no exception record>");
1.53 parser 384: else
1.67 paf 385: throw Exception(0, 0, "Exception <no exception information>");
1.53 parser 386: }
387: #endif
388: }
389:
1.83 paf 390: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
391: //_asm int 3;
392: SAPI_Info SAPI_info={
1.15 paf 393: lpECB,
1.83 paf 394: 0, // filling later: so that if there would be error pool would have SAPI_info
1.80 paf 395: 200 // default http_response_code [lpECB->dwHttpStatusCode seems to be always 0, even on 404 redirect to /404.html]
1.15 paf 396: };
1.71 paf 397:
1.1 paf 398: bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
1.53 parser 399: try { // global try
1.83 paf 400: call_real_parser_handler__do_SEH(SAPI_info, header_only);
1.2 paf 401: // successful finish
1.53 parser 402: } catch(const Exception& e) { // global problem
1.12 paf 403: // don't allocate anything on pool here:
404: // possible pool' exception not catch-ed now
1.31 paf 405: // and there could be out-of-memory exception
1.83 paf 406: const char* body=e.comment();
1.16 paf 407: // log it
1.83 paf 408: SAPI::log(SAPI_info, "exception in request exception handler: %s", body);
1.16 paf 409:
410: //
1.1 paf 411: int content_length=strlen(body);
412:
1.12 paf 413: // prepare header // not using SAPI func wich allocates on pool
414: char header_buf[MAX_STRING];
415: int header_len=snprintf(header_buf, MAX_STRING,
1.30 paf 416: "content-type: text/plain\r\n"
1.86 paf 417: "content-length: %u\r\n"
1.69 paf 418: // "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
1.30 paf 419: "\r\n",
1.12 paf 420: content_length);
421:
422: HSE_SEND_HEADER_EX_INFO header_info;
423: header_info.pszStatus="200 OK";
424: header_info.cchStatus=strlen(header_info.pszStatus);
425: header_info.pszHeader=header_buf;
426: header_info.cchHeader=header_len;
427: header_info.fKeepConn=true;
428:
1.1 paf 429: // send header
1.12 paf 430: lpECB->dwHttpStatusCode=200;
431: lpECB->ServerSupportFunction(lpECB->ConnID,
432: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 433:
434: // send body
435: if(!header_only)
1.83 paf 436: SAPI::send_body(SAPI_info, body, content_length);
1.1 paf 437:
438: // unsuccessful finish
439: }
1.65 paf 440: /*
1.83 paf 441: const char* body="test";
1.65 paf 442:
443: //
444: int content_length=strlen(body);
445:
446: // prepare header // not using SAPI func wich allocates on pool
447: char header_buf[MAX_STRING];
448: int header_len=snprintf(header_buf, MAX_STRING,
449: "content-type: text/plain\r\n"
1.86 paf 450: "content-length: %u\r\n"
1.65 paf 451: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
452: "\r\n",
453: content_length);
454: HSE_SEND_HEADER_EX_INFO header_info;
455: header_info.pszStatus="200 OK";
456: header_info.cchStatus=strlen(header_info.pszStatus);
457: header_info.pszHeader=header_buf;
458: header_info.cchHeader=header_len;
459: header_info.fKeepConn=true;
460:
461: // send header
462: lpECB->dwHttpStatusCode=200;
463: lpECB->ServerSupportFunction(lpECB->ConnID,
464: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
465:
466: // send body
467: DWORD num_bytes=content_length;
468: lpECB->WriteClient(lpECB->ConnID,
469: (void *)body, &num_bytes, HSE_IO_SYNC);
470: */
1.71 paf 471: return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1 paf 472: }
1.65 paf 473:
474: BOOL WINAPI DllMain(
475: HINSTANCE hinstDLL, // handle to the DLL module
1.85 paf 476: DWORD /*fdwReason*/, // reason for calling function
477: LPVOID /*lpvReserved*/ // reserved
1.65 paf 478: ) {
479:
480: GetModuleFileName(
481: hinstDLL, // handle to module
482: argv0, // file name of module
483: sizeof(argv0) // size of buffer
484: );
485:
486: return TRUE;
487: }
E-mail: