Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.77
1.29 paf 1: /** @file
2: Parser: IIS extension.
3:
1.62 paf 4: Copyright (c) 2000,2001, 2002 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.77 ! paf 8: static const char* IDENT_PARSER3ISAPI_C="$Id: zzz $";
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.13 paf 20: #include "pool_storage.h"
1.24 paf 21: #include "pa_socks.h"
1.1 paf 22:
1.64 paf 23: #include <windows.h>
24: #include <process.h>
25: #include <new.h>
26:
27: #include <httpext.h>
1.51 parser 28:
1.1 paf 29: #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")
1.44 parser 30:
31: // consts
1.1 paf 32:
1.73 paf 33: const char *IIS51vars[]={
34: "APPL_MD_PATH", "APPL_PHYSICAL_PATH",
35: "AUTH_PASSWORD", "AUTH_TYPE", "AUTH_USER",
36: "CERT_COOKIE", "CERT_FLAGS", "CERT_ISSUER", "CERT_KEYSIZE", "CERT_SECRETKEYSIZE",
37: "CERT_SERIALNUMBER", "CERT_SERVER_ISSUER", "CERT_SERVER_SUBJECT", "CERT_SUBJECT",
38: "CONTENT_LENGTH", "CONTENT_TYPE",
39: "LOGON_USER",
40: "HTTPS", "HTTPS_KEYSIZE", "HTTPS_SECRETKEYSIZE", "HTTPS_SERVER_ISSUER", "HTTPS_SERVER_SUBJECT",
41: "INSTANCE_ID", "INSTANCE_META_PATH",
42: "PATH_INFO", "PATH_TRANSLATED",
43: "QUERY_STRING",
44: "REMOTE_ADDR", "REMOTE_HOST", "REMOTE_USER", "REQUEST_METHOD",
45: "SCRIPT_NAME",
46: "SERVER_NAME", "SERVER_PORT", "SERVER_PORT_SECURE", "SERVER_PROTOCOL", "SERVER_SOFTWARE",
47: "URL",
48: };
49: const int IIS51var_count=sizeof(IIS51vars)/sizeof(*IIS51vars);
50:
1.65 paf 51: // globals
52:
53: char argv0[MAX_STRING]="";
54:
1.18 paf 55: // SAPI
56:
1.38 parser 57: #ifndef DOXYGEN
58: /*
1.18 paf 59: ISAPI SAPI functions receive this context information.
1.38 parser 60: see Pool::set_context
1.18 paf 61: */
1.15 paf 62: struct SAPI_func_context {
1.3 paf 63: LPEXTENSION_CONTROL_BLOCK lpECB;
64: String *header;
65: DWORD http_response_code;
66: };
1.38 parser 67: #endif
1.3 paf 68:
1.26 paf 69: // goes to 'cs-uri-query' log file field. webmaster: switch it ON[default OFF].
70: void SAPI::log(Pool& pool, const char *fmt, ...) {
1.53 parser 71: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.26 paf 72:
73: va_list args;
74: va_start(args,fmt);
75: char buf[MAX_STRING];
76: const char *prefix="PARSER_ERROR:";
77: strcpy(buf, prefix);
1.47 parser 78: char *start=buf+strlen(prefix);
1.51 parser 79: DWORD size=vsnprintf(start, MAX_STRING-strlen(prefix), fmt, args);
1.47 parser 80: remove_crlf(start, start+size);
81:
1.26 paf 82: ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID,
83: HSE_APPEND_LOG_PARAMETER, buf, &size, 0);
1.55 parser 84: }
85:
86: /// @todo event log
1.56 parser 87: void SAPI::die(const char *fmt, ...) {
1.65 paf 88: if(FILE *log=fopen("c:\\die.log", "at")) {
89: va_list args;
90: va_start(args,fmt);
91: vfprintf(log, fmt, args);
92: fclose(log);
93: }
1.55 parser 94: exit(1);
1.26 paf 95: }
96:
1.9 paf 97: const char *SAPI::get_env(Pool& pool, const char *name) {
1.53 parser 98: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1 paf 99:
100: char *variable_buf=(char *)pool.malloc(MAX_STRING);
101: DWORD variable_len = MAX_STRING-1;
102:
1.3 paf 103: if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name),
1.1 paf 104: variable_buf, &variable_len)) {
1.73 paf 105: if(*variable_buf) { // saw returning len=1 && *buf=0 :(
106: variable_buf[variable_len]=0;
107: return variable_buf;
108: }
1.7 paf 109: } else if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
110: variable_buf=(char *)pool.malloc(variable_len+1);
111:
112: if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name),
113: variable_buf, &variable_len)) {
1.73 paf 114: if(*variable_buf) {
115: variable_buf[variable_len]=0;
116: return variable_buf;
117: }
1.7 paf 118: }
1.1 paf 119: }
1.7 paf 120:
1.1 paf 121: return 0;
122: }
123:
1.72 paf 124: static int grep_char(const char *s, char c) {
125: int result=0;
126: if(s) {
127: while(s=strchr(s, c)) {
128: s++; // skip found c
129: result++;
130: }
131: }
132: return result;
133: }
134: static const char *mk_env_pair(Pool& pool, const char *key, const char *value) {
135: char *result=(char *)pool.malloc(strlen(key)+1/*=*/+strlen(value)+1/*0*/);
136: strcpy(result, key); strcat(result, "="); strcat(result, value);
137: return result;
138: }
139: const char *const *SAPI::environment(Pool& pool) {
140: // we know this buf is writable
141: char *all_http_vars=const_cast<char *>(SAPI::get_env(pool, "ALL_HTTP"));
142: const int http_var_count=grep_char(all_http_vars, '\n')+1/*\n for theoretical(never saw) this \0*/;
143:
144: const char **result=
145: (const char **)pool.malloc(sizeof(char *)*(IIS51var_count+http_var_count+1/*0*/));
146: const char **cur=result;
147:
148: // IIS5.1 vars
149: for(int i=0; i<IIS51var_count; i++) {
150: const char *key=IIS51vars[i];
151: if(const char *value=SAPI::get_env(pool, key))
152: *cur++=mk_env_pair(pool, key, value);
153: }
154:
155: // HTTP_* vars
156: if(char *s=all_http_vars) {
157: while(char *key=lsplit(&s, '\n'))
158: if(char *value=lsplit(key, ':'))
159: *cur++=mk_env_pair(pool, key, value);
160: }
161:
162: // mark EOE
163: *cur=0;
164:
165: return result;
166: }
167:
1.26 paf 168: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
1.53 parser 169: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1 paf 170:
171: DWORD read_from_buf=0;
172: DWORD read_from_input=0;
173: DWORD total_read=0;
174:
1.3 paf 175: read_from_buf=min(ctx.lpECB->cbAvailable, max_bytes);
176: memcpy(buf, ctx.lpECB->lpbData, read_from_buf);
1.1 paf 177: total_read+=read_from_buf;
178:
179: if(read_from_buf<max_bytes &&
1.3 paf 180: read_from_buf<ctx.lpECB->cbTotalBytes) {
1.1 paf 181: DWORD cbRead=0, cbSize;
182:
1.3 paf 183: read_from_input=min(max_bytes-read_from_buf,
184: ctx.lpECB->cbTotalBytes-read_from_buf);
1.1 paf 185: while(cbRead < read_from_input) {
186: cbSize=read_from_input - cbRead;
1.3 paf 187: if(!ctx.lpECB->ReadClient(ctx.lpECB->ConnID,
188: buf+read_from_buf+cbRead, &cbSize) ||
1.1 paf 189: cbSize==0)
190: break;
191: cbRead+=cbSize;
192: }
193: total_read+=cbRead;
194: }
195: return total_read;
196: }
197:
1.9 paf 198: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.53 parser 199: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.3 paf 200:
201: if(strcasecmp(key, "location")==0)
202: ctx.http_response_code=302;
203:
204: if(strcasecmp(key, "status")==0)
205: ctx.http_response_code=atoi(value);
206: else {
207: ctx.header->APPEND_CONST(key);
208: ctx.header->APPEND_CONST(": ");
209: ctx.header->APPEND_CONST(value);
1.30 paf 210: ctx.header->APPEND_CONST("\r\n");
1.3 paf 211: }
1.1 paf 212: }
213:
1.23 paf 214: /// @todo intelligent cache-control
1.9 paf 215: void SAPI::send_header(Pool& pool) {
1.53 parser 216: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1 paf 217:
1.64 paf 218: HSE_SEND_HEADER_EX_INFO header_info;
1.1 paf 219:
220: char status_buf[MAX_STATUS_LENGTH];
1.3 paf 221: switch(ctx.http_response_code) {
1.1 paf 222: case 200:
223: header_info.pszStatus="200 OK";
224: break;
225: case 302:
226: header_info.pszStatus="302 Moved Temporarily";
227: break;
1.8 paf 228: case 401:// useless untill parser auth mech
1.1 paf 229: header_info.pszStatus="401 Authorization Required";
1.8 paf 230: break;
1.1 paf 231: default:
1.3 paf 232: snprintf(status_buf, MAX_STATUS_LENGTH,
233: "%d Undescribed", ctx.http_response_code);
1.1 paf 234: header_info.pszStatus=status_buf;
235: break;
236: }
237: header_info.cchStatus=strlen(header_info.pszStatus);
1.66 paf 238: *ctx.header << "\r\n"; // ISAPI v<5 did quite well without it
1.3 paf 239: header_info.pszHeader=ctx.header->cstr();
240: header_info.cchHeader=ctx.header->size();
1.5 paf 241: header_info.fKeepConn=true;
1.1 paf 242:
1.3 paf 243: ctx.lpECB->dwHttpStatusCode=ctx.http_response_code;
1.1 paf 244:
1.3 paf 245: ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID,
246: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 247: }
248:
1.23 paf 249: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.53 parser 250: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1 paf 251:
252: DWORD num_bytes=size;
1.3 paf 253: ctx.lpECB->WriteClient(ctx.lpECB->ConnID,
1.23 paf 254: const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC);
1.1 paf 255: }
1.16 paf 256:
1.1 paf 257: //
258:
1.59 paf 259: int failed_new(size_t size) {
260: SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
261: return 0; // not reached
262: }
263:
1.71 paf 264: #ifdef _DEBUG
265: static Pool_storage *global_pool_storagep;
266: #endif
1.15 paf 267: static bool parser_init() {
1.1 paf 268: static bool globals_inited=false;
269: if(globals_inited)
1.15 paf 270: return true;
1.1 paf 271: globals_inited=true;
272:
1.59 paf 273: _set_new_handler(failed_new);
274:
1.65 paf 275: static Pool_storage pool_storage;
1.71 paf 276: #ifdef _DEBUG
277: global_pool_storagep=&pool_storage;
278: #endif
1.65 paf 279: static Pool pool(&pool_storage); // global pool
1.53 parser 280: try {
1.27 paf 281: // init socks
282: init_socks(pool);
1.32 paf 283: // init global classes
284: init_methoded_array(pool);
1.1 paf 285: // init global variables
1.9 paf 286: pa_globals_init(pool);
1.65 paf 287:
1.15 paf 288: // successful finish
289: return true;
1.53 parser 290: } catch(const Exception& e) { // global problem
291: const char *body=e.comment();
1.15 paf 292:
293: // unsuccessful finish
294: return false;
1.1 paf 295: }
296: }
297:
298: /// ISAPI //
299: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
300: pVer->dwExtensionVersion = HSE_VERSION;
1.36 parser 301: strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
302: pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15 paf 303: return parser_init();
1.1 paf 304: }
305:
1.6 paf 306: /**
307: ISAPI // main workhorse
308:
1.23 paf 309: @todo
1.10 paf 310: IIS: remove trailing default-document[index.html] from $request.uri.
311: to do that we need to consult metabase,
1.12 paf 312: wich is tested&works but seems slow runtime
313: and not could-be-quickly-implemented if prepared.
1.37 parser 314: @test
315: PARSER_VERSION from outside
1.6 paf 316: */
1.53 parser 317:
318: void real_parser_handler(Pool& pool, LPEXTENSION_CONTROL_BLOCK lpECB, bool header_only) {
319: static_cast<SAPI_func_context *>(pool.get_context())->header=new(pool) String(pool);
320:
321: // Request info
322: Request::Info request_info;
323:
324: size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
325: char *filespec_to_process=(char *)pool.malloc(path_translated_buf_size);
326: memcpy(filespec_to_process, lpECB->lpszPathTranslated, path_translated_buf_size);
327: #ifdef WIN32
328: back_slashes_to_slashes(filespec_to_process);
329: #endif
330:
331: if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
332: // IIS
333: size_t len=strlen(filespec_to_process)-strlen(path_info);
334: char *buf=(char *)pool.malloc(len+1);
335: strncpy(buf, filespec_to_process, len); buf[len]=0;
336: request_info.document_root=buf;
337: } else
1.67 paf 338: throw Exception("parser.runtime",
1.53 parser 339: 0,
340: "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
341:
342: request_info.path_translated=filespec_to_process;
343: request_info.method=lpECB->lpszMethod;
344: request_info.query_string=lpECB->lpszQueryString;
345: if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
346: char *reconstructed_uri=(char *)pool.malloc(
347: strlen(lpECB->lpszPathInfo)+1/*'?'*/+
348: strlen(lpECB->lpszQueryString)+1/*0*/);
349: strcpy(reconstructed_uri, lpECB->lpszPathInfo);
350: strcat(reconstructed_uri, "?");
351: strcat(reconstructed_uri, lpECB->lpszQueryString);
352: request_info.uri=reconstructed_uri;
353: } else
354: request_info.uri=lpECB->lpszPathInfo;
355:
356: request_info.content_type=lpECB->lpszContentType;
357: request_info.content_length=lpECB->cbTotalBytes;
358: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
1.76 paf 359: request_info.mail_received=false;
1.53 parser 360:
361: // prepare to process request
362: Request request(pool,
363: request_info,
1.60 paf 364: String::UL_HTML|String::UL_OPTIMIZE_BIT,
1.70 paf 365: #ifdef _DEBUG
366: true
367: #else
368: false
369: #endif
370: /* status_allowed */);
1.53 parser 371:
1.65 paf 372: // beside by binary
1.75 paf 373: static char beside_binary_path[MAX_STRING];
374: strncpy(beside_binary_path, argv0, MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
1.65 paf 375: if(!(
1.75 paf 376: rsplit(beside_binary_path, '/') ||
377: rsplit(beside_binary_path, '\\'))) { // strip filename
1.65 paf 378: // no path, just filename
1.75 paf 379: beside_binary_path[0]='.'; beside_binary_path[1]=0;
1.65 paf 380: }
1.74 paf 381: char config_filespec[MAX_STRING];
382: snprintf(config_filespec, MAX_STRING,
1.65 paf 383: "%s/%s",
1.75 paf 384: beside_binary_path, AUTO_FILE_NAME);
1.65 paf 385:
1.53 parser 386: // process the request
387: request.core(
1.75 paf 388: config_filespec, false /*fail_on_read_problem*/, // /path/to/first/auto.p
1.53 parser 389: header_only);
390: }
391:
392: void call_real_parser_handler__do_SEH(Pool& pool,
393: LPEXTENSION_CONTROL_BLOCK lpECB,
394: 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
399: real_parser_handler(pool, lpECB, header_only);
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.71 paf 419: inline DWORD RealHttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13 paf 420: Pool_storage pool_storage;
1.15 paf 421: Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
422: SAPI_func_context ctx={
423: lpECB,
1.18 paf 424: 0, // filling later: so that if there would be error pool would have ctx
1.39 parser 425: 200 // default http_response_code
1.15 paf 426: };
427: pool.set_context(&ctx);// no allocations before this line!
1.71 paf 428:
1.1 paf 429: bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
1.53 parser 430: try { // global try
431: call_real_parser_handler__do_SEH(pool, lpECB, header_only);
1.2 paf 432: // successful finish
1.53 parser 433: } catch(const Exception& e) { // global problem
1.12 paf 434: // don't allocate anything on pool here:
435: // possible pool' exception not catch-ed now
1.31 paf 436: // and there could be out-of-memory exception
1.1 paf 437: const char *body=e.comment();
1.16 paf 438: // log it
439: SAPI::log(pool, "exception in request exception handler: %s", body);
440:
441: //
1.1 paf 442: int content_length=strlen(body);
443:
1.12 paf 444: // prepare header // not using SAPI func wich allocates on pool
445: char header_buf[MAX_STRING];
446: int header_len=snprintf(header_buf, MAX_STRING,
1.30 paf 447: "content-type: text/plain\r\n"
448: "content-length: %lu\r\n"
1.69 paf 449: // "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
1.30 paf 450: "\r\n",
1.12 paf 451: content_length);
452:
453: HSE_SEND_HEADER_EX_INFO header_info;
454: header_info.pszStatus="200 OK";
455: header_info.cchStatus=strlen(header_info.pszStatus);
456: header_info.pszHeader=header_buf;
457: header_info.cchHeader=header_len;
458: header_info.fKeepConn=true;
459:
1.1 paf 460: // send header
1.12 paf 461: lpECB->dwHttpStatusCode=200;
462: lpECB->ServerSupportFunction(lpECB->ConnID,
463: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 464:
465: // send body
466: if(!header_only)
1.9 paf 467: SAPI::send_body(pool, body, content_length);
1.1 paf 468:
469: // unsuccessful finish
470: }
1.65 paf 471: /*
472: const char *body="test";
473:
474: //
475: int content_length=strlen(body);
476:
477: // prepare header // not using SAPI func wich allocates on pool
478: char header_buf[MAX_STRING];
479: int header_len=snprintf(header_buf, MAX_STRING,
480: "content-type: text/plain\r\n"
481: "content-length: %lu\r\n"
482: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
483: "\r\n",
484: content_length);
485: HSE_SEND_HEADER_EX_INFO header_info;
486: header_info.pszStatus="200 OK";
487: header_info.cchStatus=strlen(header_info.pszStatus);
488: header_info.pszHeader=header_buf;
489: header_info.cchHeader=header_len;
490: header_info.fKeepConn=true;
491:
492: // send header
493: lpECB->dwHttpStatusCode=200;
494: lpECB->ServerSupportFunction(lpECB->ConnID,
495: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
496:
497: // send body
498: DWORD num_bytes=content_length;
499: lpECB->WriteClient(lpECB->ConnID,
500: (void *)body, &num_bytes, HSE_IO_SYNC);
501: */
1.71 paf 502: return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
503: }
1.66 paf 504:
1.71 paf 505: #ifdef _DEBUG
506: //for memory leaks detection only
507: #undef _WINDOWS_
508: #include <afx.h>
509: #endif
510: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
511: // Declare the variables needed
512: #ifdef _DEBUG
513: // _crtBreakAlloc=97779;//97777; //997;
514:
515: CMemoryState oldMemState, newMemState, diffMemState;
516: oldMemState.Checkpoint();
517: //global_pool_storagep->fbreak_on_alloc=true;
518: #endif
519:
520: DWORD result=RealHttpExtensionProc(lpECB);
521:
522: #ifdef _DEBUG
523: newMemState.Checkpoint();
524: if( diffMemState.Difference( oldMemState, newMemState ) )
525: {
526: TRACE( "Memory leaked!\n" );
527: diffMemState.DumpStatistics( );
528: diffMemState.DumpAllObjectsSince();
529: }
530: #endif
531: return result;
1.1 paf 532: }
1.65 paf 533:
534: BOOL WINAPI DllMain(
535: HINSTANCE hinstDLL, // handle to the DLL module
536: DWORD fdwReason, // reason for calling function
537: LPVOID lpvReserved // reserved
538: ) {
539:
540: GetModuleFileName(
541: hinstDLL, // handle to module
542: argv0, // file name of module
543: sizeof(argv0) // size of buffer
544: );
545:
546: return TRUE;
547: }
E-mail: