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