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