Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.53
1.29 paf 1: /** @file
2: Parser: IIS extension.
3:
4: Copyright (c) 2000,2001 ArtLebedev Group (http://www.artlebedev.com)
1.44 parser 5: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.29 paf 6:
1.53 ! parser 7: $Id: parser3isapi.C,v 1.52 2001/10/13 17:42:52 parser 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:
16: #include <windows.h>
17: #include <process.h>
18:
19: #include <httpext.h>
20:
1.9 paf 21: #include "pa_sapi.h"
1.1 paf 22: #include "pa_globals.h"
23: #include "pa_request.h"
24: #include "pa_version.h"
1.13 paf 25: #include "pool_storage.h"
1.24 paf 26: #include "pa_socks.h"
1.1 paf 27:
1.51 parser 28: #ifdef XML
29: #include <XalanTransformer/XalanCAPI.h>
30: #endif
31:
1.1 paf 32: #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")
1.44 parser 33:
34: // consts
35:
36: extern const char *main_RCSIds[];
1.46 parser 37: #ifdef USE_SMTP
1.44 parser 38: extern const char *smtp_RCSIds[];
1.45 parser 39: #endif
1.44 parser 40: extern const char *gd_RCSIds[];
41: extern const char *classes_RCSIds[];
42: extern const char *types_RCSIds[];
43: extern const char *parser3isapi_RCSIds[];
1.49 parser 44: #ifdef XML
45: extern const char *xalan_patched_RCSIds[];
46: #endif
1.44 parser 47: const char **RCSIds[]={
48: main_RCSIds,
1.46 parser 49: #ifdef USE_SMTP
1.44 parser 50: smtp_RCSIds,
1.45 parser 51: #endif
1.44 parser 52: gd_RCSIds,
53: classes_RCSIds,
54: types_RCSIds,
55: parser3isapi_RCSIds,
1.48 parser 56: #ifdef XML
57: xalan_patched_RCSIds,
58: #endif
1.44 parser 59: 0
60: };
1.1 paf 61:
1.18 paf 62: // SAPI
63:
1.38 parser 64: #ifndef DOXYGEN
65: /*
1.18 paf 66: ISAPI SAPI functions receive this context information.
1.38 parser 67: see Pool::set_context
1.18 paf 68: */
1.15 paf 69: struct SAPI_func_context {
1.3 paf 70: LPEXTENSION_CONTROL_BLOCK lpECB;
71: String *header;
72: DWORD http_response_code;
73: };
1.38 parser 74: #endif
1.3 paf 75:
1.50 parser 76: #ifdef XML
77: /**
78: * Terminate Xalan and Xerces.
79: *
80: * Should be called only once per process after deleting all
81: * instances of XalanTransformer. Once a process has called
82: * this function, it cannot use the API for the remaining
83: * lifetime of the process.
84:
85:
86: this requirement is fullfilled by using Pool::register_cleanup
87: */
88: void callXalanTerminate(void *) {
89: //_asm int 3;
90: XalanTerminate();
91: }
92: #endif
93:
1.26 paf 94: // goes to 'cs-uri-query' log file field. webmaster: switch it ON[default OFF].
95: void SAPI::log(Pool& pool, const char *fmt, ...) {
1.53 ! parser 96: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.26 paf 97:
98: va_list args;
99: va_start(args,fmt);
100: char buf[MAX_STRING];
101: const char *prefix="PARSER_ERROR:";
102: strcpy(buf, prefix);
1.47 parser 103: char *start=buf+strlen(prefix);
1.51 parser 104: DWORD size=vsnprintf(start, MAX_STRING-strlen(prefix), fmt, args);
1.47 parser 105: remove_crlf(start, start+size);
106:
1.26 paf 107: ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID,
108: HSE_APPEND_LOG_PARAMETER, buf, &size, 0);
109: }
110:
1.9 paf 111: const char *SAPI::get_env(Pool& pool, const char *name) {
1.53 ! parser 112: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1 paf 113:
114: char *variable_buf=(char *)pool.malloc(MAX_STRING);
115: DWORD variable_len = MAX_STRING-1;
116:
1.3 paf 117: if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name),
1.1 paf 118: variable_buf, &variable_len)) {
119: variable_buf[variable_len]=0;
120: return variable_buf;
1.7 paf 121: } else if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
122: variable_buf=(char *)pool.malloc(variable_len+1);
123:
124: if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name),
125: variable_buf, &variable_len)) {
126: variable_buf[variable_len]=0;
127: return variable_buf;
128: }
1.1 paf 129: }
1.7 paf 130:
1.1 paf 131: return 0;
132: }
133:
1.26 paf 134: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
1.53 ! parser 135: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1 paf 136:
137: DWORD read_from_buf=0;
138: DWORD read_from_input=0;
139: DWORD total_read=0;
140:
1.3 paf 141: read_from_buf=min(ctx.lpECB->cbAvailable, max_bytes);
142: memcpy(buf, ctx.lpECB->lpbData, read_from_buf);
1.1 paf 143: total_read+=read_from_buf;
144:
145: if(read_from_buf<max_bytes &&
1.3 paf 146: read_from_buf<ctx.lpECB->cbTotalBytes) {
1.1 paf 147: DWORD cbRead=0, cbSize;
148:
1.3 paf 149: read_from_input=min(max_bytes-read_from_buf,
150: ctx.lpECB->cbTotalBytes-read_from_buf);
1.1 paf 151: while(cbRead < read_from_input) {
152: cbSize=read_from_input - cbRead;
1.3 paf 153: if(!ctx.lpECB->ReadClient(ctx.lpECB->ConnID,
154: buf+read_from_buf+cbRead, &cbSize) ||
1.1 paf 155: cbSize==0)
156: break;
157: cbRead+=cbSize;
158: }
159: total_read+=cbRead;
160: }
161: return total_read;
162: }
163:
1.9 paf 164: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.53 ! parser 165: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.3 paf 166:
167: if(strcasecmp(key, "location")==0)
168: ctx.http_response_code=302;
169:
170: if(strcasecmp(key, "status")==0)
171: ctx.http_response_code=atoi(value);
172: else {
173: ctx.header->APPEND_CONST(key);
174: ctx.header->APPEND_CONST(": ");
175: ctx.header->APPEND_CONST(value);
1.30 paf 176: ctx.header->APPEND_CONST("\r\n");
1.3 paf 177: }
1.1 paf 178: }
179:
1.23 paf 180: /// @todo intelligent cache-control
1.9 paf 181: void SAPI::send_header(Pool& pool) {
1.53 ! parser 182: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1 paf 183:
1.6 paf 184: ctx.header->APPEND_CONST(
1.30 paf 185: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
186: "\r\n");
1.1 paf 187: HSE_SEND_HEADER_EX_INFO header_info;
188:
189: char status_buf[MAX_STATUS_LENGTH];
1.3 paf 190: switch(ctx.http_response_code) {
1.1 paf 191: case 200:
192: header_info.pszStatus="200 OK";
193: break;
194: case 302:
195: header_info.pszStatus="302 Moved Temporarily";
196: break;
1.8 paf 197: case 401:// useless untill parser auth mech
1.1 paf 198: header_info.pszStatus="401 Authorization Required";
1.8 paf 199: break;
1.1 paf 200: default:
1.3 paf 201: snprintf(status_buf, MAX_STATUS_LENGTH,
202: "%d Undescribed", ctx.http_response_code);
1.1 paf 203: header_info.pszStatus=status_buf;
204: break;
205: }
206: header_info.cchStatus=strlen(header_info.pszStatus);
1.3 paf 207: header_info.pszHeader=ctx.header->cstr();
208: header_info.cchHeader=ctx.header->size();
1.5 paf 209: header_info.fKeepConn=true;
1.1 paf 210:
1.3 paf 211: ctx.lpECB->dwHttpStatusCode=ctx.http_response_code;
1.1 paf 212:
1.3 paf 213: ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID,
214: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 215: }
216:
1.23 paf 217: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.53 ! parser 218: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.get_context());
1.1 paf 219:
220: DWORD num_bytes=size;
1.3 paf 221: ctx.lpECB->WriteClient(ctx.lpECB->ConnID,
1.23 paf 222: const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC);
1.1 paf 223: }
1.16 paf 224:
1.1 paf 225: //
226:
1.15 paf 227: static bool parser_init() {
1.1 paf 228: static bool globals_inited=false;
229: if(globals_inited)
1.15 paf 230: return true;
1.1 paf 231: globals_inited=true;
232:
1.13 paf 233: static Pool pool(0); // global pool
1.53 ! parser 234: try {
1.27 paf 235: // init socks
236: init_socks(pool);
237:
1.50 parser 238: #ifdef XML
239: /**
240: * Initialize Xerces and Xalan.
241: *
242: * Should be called only once per process before making
243: * any other API calls.
244: */
245: //_asm int 3;
246: XalanInitialize();
247: pool.register_cleanup(callXalanTerminate, 0);
248: #endif
249:
1.32 paf 250: // init global classes
251: init_methoded_array(pool);
1.1 paf 252: // init global variables
1.9 paf 253: pa_globals_init(pool);
1.1 paf 254:
1.15 paf 255: // successful finish
256: return true;
1.53 ! parser 257: } catch(const Exception& e) { // global problem
! 258: const char *body=e.comment();
1.15 paf 259:
260: // unsuccessful finish
261: return false;
1.1 paf 262: }
263: }
264:
265: /// ISAPI //
266: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
267: pVer->dwExtensionVersion = HSE_VERSION;
1.36 parser 268: strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
269: pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15 paf 270: return parser_init();
1.1 paf 271: }
272:
1.6 paf 273: /**
274: ISAPI // main workhorse
275:
1.23 paf 276: @todo
1.10 paf 277: IIS: remove trailing default-document[index.html] from $request.uri.
278: to do that we need to consult metabase,
1.12 paf 279: wich is tested&works but seems slow runtime
280: and not could-be-quickly-implemented if prepared.
1.37 parser 281: @test
282: PARSER_VERSION from outside
1.6 paf 283: */
1.53 ! parser 284:
! 285: void real_parser_handler(Pool& pool, LPEXTENSION_CONTROL_BLOCK lpECB, bool header_only) {
! 286: static_cast<SAPI_func_context *>(pool.get_context())->header=new(pool) String(pool);
! 287:
! 288: // Request info
! 289: Request::Info request_info;
! 290:
! 291: size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
! 292: char *filespec_to_process=(char *)pool.malloc(path_translated_buf_size);
! 293: memcpy(filespec_to_process, lpECB->lpszPathTranslated, path_translated_buf_size);
! 294: #ifdef WIN32
! 295: back_slashes_to_slashes(filespec_to_process);
! 296: #endif
! 297:
! 298: if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
! 299: // IIS
! 300: size_t len=strlen(filespec_to_process)-strlen(path_info);
! 301: char *buf=(char *)pool.malloc(len+1);
! 302: strncpy(buf, filespec_to_process, len); buf[len]=0;
! 303: request_info.document_root=buf;
! 304: } else
! 305: throw Exception(0, 0,
! 306: 0,
! 307: "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
! 308:
! 309: request_info.path_translated=filespec_to_process;
! 310: request_info.method=lpECB->lpszMethod;
! 311: request_info.query_string=lpECB->lpszQueryString;
! 312: if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
! 313: char *reconstructed_uri=(char *)pool.malloc(
! 314: strlen(lpECB->lpszPathInfo)+1/*'?'*/+
! 315: strlen(lpECB->lpszQueryString)+1/*0*/);
! 316: strcpy(reconstructed_uri, lpECB->lpszPathInfo);
! 317: strcat(reconstructed_uri, "?");
! 318: strcat(reconstructed_uri, lpECB->lpszQueryString);
! 319: request_info.uri=reconstructed_uri;
! 320: } else
! 321: request_info.uri=lpECB->lpszPathInfo;
! 322:
! 323: request_info.content_type=lpECB->lpszContentType;
! 324: request_info.content_length=lpECB->cbTotalBytes;
! 325: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
! 326: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
! 327:
! 328:
! 329: // prepare to process request
! 330: Request request(pool,
! 331: request_info,
! 332: String::UL_USER_HTML
! 333: );
! 334:
! 335: // some root-controlled location
! 336: // c:\windows
! 337: char root_config_path[MAX_STRING];
! 338: GetWindowsDirectory(root_config_path, MAX_STRING);
! 339: // must be dynamic: rethrowing from request.core
! 340: // may return 'source' which can be inside of 'root auto.p@exeception'
! 341: char *root_config_filespec=(char *)pool.malloc(MAX_STRING);
! 342: snprintf(root_config_filespec, MAX_STRING,
! 343: "%s/%s",
! 344: root_config_path, CONFIG_FILE_NAME);
! 345:
! 346: // process the request
! 347: request.core(
! 348: root_config_filespec, false/*may be abcent*/, // /path/to/admin/auto.p
! 349: 0/*parser_site_auto_path*/, false, // /path/to/site/auto.p
! 350: header_only);
! 351: }
! 352:
! 353: void call_real_parser_handler__do_SEH(Pool& pool,
! 354: LPEXTENSION_CONTROL_BLOCK lpECB,
! 355: bool header_only) {
! 356: #ifdef WIN32
! 357: LPEXCEPTION_POINTERS system_exception=0;
! 358: __try {
! 359: #endif
! 360: real_parser_handler(pool, lpECB, header_only);
! 361:
! 362: #if _MSC_VER
! 363: } __except (
! 364: (system_exception=GetExceptionInformation()),
! 365: EXCEPTION_EXECUTE_HANDLER) {
! 366:
! 367: if(system_exception)
! 368: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
! 369: throw Exception(0, 0,
! 370: 0,
! 371: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
! 372: else
! 373: throw Exception(0, 0, 0, "Exception <no exception record>");
! 374: else
! 375: throw Exception(0, 0, 0, "Exception <no exception information>");
! 376: }
! 377: #endif
! 378: }
! 379:
! 380:
1.1 paf 381: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13 paf 382: Pool_storage pool_storage;
1.15 paf 383: Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
384: SAPI_func_context ctx={
385: lpECB,
1.18 paf 386: 0, // filling later: so that if there would be error pool would have ctx
1.39 parser 387: 200 // default http_response_code
1.15 paf 388: };
389: pool.set_context(&ctx);// no allocations before this line!
1.2 paf 390:
1.50 parser 391: #ifdef XML
392: /**
393: * Initialize Xerces and Xalan.
394: *
395: * Should be called only once per process before making
396: * any other API calls.
397: */
398: //_asm int 3;
399: XalanInitialize();
400: pool.register_cleanup(callXalanTerminate, 0);
401: #endif
402:
403:
1.1 paf 404: bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
1.53 ! parser 405: try { // global try
! 406: call_real_parser_handler__do_SEH(pool, lpECB, header_only);
1.2 paf 407: // successful finish
1.53 ! parser 408: } catch(const Exception& e) { // global problem
1.12 paf 409: // don't allocate anything on pool here:
410: // possible pool' exception not catch-ed now
1.31 paf 411: // and there could be out-of-memory exception
1.1 paf 412: const char *body=e.comment();
1.16 paf 413: // log it
414: SAPI::log(pool, "exception in request exception handler: %s", body);
415:
416: //
1.1 paf 417: int content_length=strlen(body);
418:
1.12 paf 419: // prepare header // not using SAPI func wich allocates on pool
420: char header_buf[MAX_STRING];
421: int header_len=snprintf(header_buf, MAX_STRING,
1.30 paf 422: "content-type: text/plain\r\n"
423: "content-length: %lu\r\n"
424: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
425: "\r\n",
1.12 paf 426: content_length);
427:
428: HSE_SEND_HEADER_EX_INFO header_info;
429: header_info.pszStatus="200 OK";
430: header_info.cchStatus=strlen(header_info.pszStatus);
431: header_info.pszHeader=header_buf;
432: header_info.cchHeader=header_len;
433: header_info.fKeepConn=true;
434:
1.1 paf 435: // send header
1.12 paf 436: lpECB->dwHttpStatusCode=200;
437: lpECB->ServerSupportFunction(lpECB->ConnID,
438: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 439:
440: // send body
441: if(!header_only)
1.9 paf 442: SAPI::send_body(pool, body, content_length);
1.1 paf 443:
444: // unsuccessful finish
445: }
446:
1.6 paf 447: return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1 paf 448: }
E-mail: