Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.79
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.79 ! paf 8: static const char* IDENT_PARSER3ISAPI_C="$Date: 2002/08/01 11:41:21 $";
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.79 ! paf 385: bool fail_on_config_read_problem=entry_exists(config_filespec);
1.65 paf 386:
1.53 parser 387: // process the request
388: request.core(
1.79 ! paf 389: config_filespec, fail_on_config_read_problem, // /path/to/first/auto.p
1.53 parser 390: header_only);
391: }
392:
393: void call_real_parser_handler__do_SEH(Pool& pool,
394: LPEXTENSION_CONTROL_BLOCK lpECB,
395: bool header_only) {
1.54 parser 396: #if _MSC_VER & !defined(_DEBUG)
1.53 parser 397: LPEXCEPTION_POINTERS system_exception=0;
398: __try {
399: #endif
400: real_parser_handler(pool, lpECB, header_only);
401:
1.54 parser 402: #if _MSC_VER & !defined(_DEBUG)
1.53 parser 403: } __except (
404: (system_exception=GetExceptionInformation()),
405: EXCEPTION_EXECUTE_HANDLER) {
406:
407: if(system_exception)
408: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.67 paf 409: throw Exception(0,
1.53 parser 410: 0,
411: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
412: else
1.67 paf 413: throw Exception(0, 0, "Exception <no exception record>");
1.53 parser 414: else
1.67 paf 415: throw Exception(0, 0, "Exception <no exception information>");
1.53 parser 416: }
417: #endif
418: }
419:
1.71 paf 420: inline DWORD RealHttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13 paf 421: Pool_storage pool_storage;
1.15 paf 422: Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
423: SAPI_func_context ctx={
424: lpECB,
1.18 paf 425: 0, // filling later: so that if there would be error pool would have ctx
1.39 parser 426: 200 // default http_response_code
1.15 paf 427: };
428: pool.set_context(&ctx);// no allocations before this line!
1.71 paf 429:
1.1 paf 430: bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
1.53 parser 431: try { // global try
432: call_real_parser_handler__do_SEH(pool, lpECB, header_only);
1.2 paf 433: // successful finish
1.53 parser 434: } catch(const Exception& e) { // global problem
1.12 paf 435: // don't allocate anything on pool here:
436: // possible pool' exception not catch-ed now
1.31 paf 437: // and there could be out-of-memory exception
1.1 paf 438: const char *body=e.comment();
1.16 paf 439: // log it
440: SAPI::log(pool, "exception in request exception handler: %s", body);
441:
442: //
1.1 paf 443: int content_length=strlen(body);
444:
1.12 paf 445: // prepare header // not using SAPI func wich allocates on pool
446: char header_buf[MAX_STRING];
447: int header_len=snprintf(header_buf, MAX_STRING,
1.30 paf 448: "content-type: text/plain\r\n"
449: "content-length: %lu\r\n"
1.69 paf 450: // "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
1.30 paf 451: "\r\n",
1.12 paf 452: content_length);
453:
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:
1.1 paf 461: // send header
1.12 paf 462: lpECB->dwHttpStatusCode=200;
463: lpECB->ServerSupportFunction(lpECB->ConnID,
464: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 465:
466: // send body
467: if(!header_only)
1.9 paf 468: SAPI::send_body(pool, body, content_length);
1.1 paf 469:
470: // unsuccessful finish
471: }
1.65 paf 472: /*
473: const char *body="test";
474:
475: //
476: int content_length=strlen(body);
477:
478: // prepare header // not using SAPI func wich allocates on pool
479: char header_buf[MAX_STRING];
480: int header_len=snprintf(header_buf, MAX_STRING,
481: "content-type: text/plain\r\n"
482: "content-length: %lu\r\n"
483: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
484: "\r\n",
485: content_length);
486: HSE_SEND_HEADER_EX_INFO header_info;
487: header_info.pszStatus="200 OK";
488: header_info.cchStatus=strlen(header_info.pszStatus);
489: header_info.pszHeader=header_buf;
490: header_info.cchHeader=header_len;
491: header_info.fKeepConn=true;
492:
493: // send header
494: lpECB->dwHttpStatusCode=200;
495: lpECB->ServerSupportFunction(lpECB->ConnID,
496: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
497:
498: // send body
499: DWORD num_bytes=content_length;
500: lpECB->WriteClient(lpECB->ConnID,
501: (void *)body, &num_bytes, HSE_IO_SYNC);
502: */
1.71 paf 503: return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
504: }
1.66 paf 505:
1.71 paf 506: #ifdef _DEBUG
507: //for memory leaks detection only
508: #undef _WINDOWS_
509: #include <afx.h>
510: #endif
511: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
512: // Declare the variables needed
513: #ifdef _DEBUG
514: // _crtBreakAlloc=97779;//97777; //997;
515:
516: CMemoryState oldMemState, newMemState, diffMemState;
517: oldMemState.Checkpoint();
518: //global_pool_storagep->fbreak_on_alloc=true;
519: #endif
520:
521: DWORD result=RealHttpExtensionProc(lpECB);
522:
523: #ifdef _DEBUG
524: newMemState.Checkpoint();
525: if( diffMemState.Difference( oldMemState, newMemState ) )
526: {
527: TRACE( "Memory leaked!\n" );
528: diffMemState.DumpStatistics( );
529: diffMemState.DumpAllObjectsSince();
530: }
531: #endif
532: return result;
1.1 paf 533: }
1.65 paf 534:
535: BOOL WINAPI DllMain(
536: HINSTANCE hinstDLL, // handle to the DLL module
537: DWORD fdwReason, // reason for calling function
538: LPVOID lpvReserved // reserved
539: ) {
540:
541: GetModuleFileName(
542: hinstDLL, // handle to module
543: argv0, // file name of module
544: sizeof(argv0) // size of buffer
545: );
546:
547: return TRUE;
548: }
E-mail: