Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.66
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.66 ! paf 7: $Id: parser3isapi.C,v 1.65 2002/03/05 07:48:07 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.15 paf 218: static bool parser_init() {
1.1 paf 219: static bool globals_inited=false;
220: if(globals_inited)
1.15 paf 221: return true;
1.1 paf 222: globals_inited=true;
223:
1.59 paf 224: _set_new_handler(failed_new);
225:
1.65 paf 226: static Pool_storage pool_storage;
227: static Pool pool(&pool_storage); // global pool
1.53 parser 228: try {
1.27 paf 229: // init socks
230: init_socks(pool);
1.32 paf 231: // init global classes
232: init_methoded_array(pool);
1.1 paf 233: // init global variables
1.9 paf 234: pa_globals_init(pool);
1.65 paf 235:
1.15 paf 236: // successful finish
237: return true;
1.53 parser 238: } catch(const Exception& e) { // global problem
239: const char *body=e.comment();
1.15 paf 240:
241: // unsuccessful finish
242: return false;
1.1 paf 243: }
244: }
245:
246: /// ISAPI //
247: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
248: pVer->dwExtensionVersion = HSE_VERSION;
1.36 parser 249: strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
250: pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15 paf 251: return parser_init();
1.1 paf 252: }
253:
1.6 paf 254: /**
255: ISAPI // main workhorse
256:
1.23 paf 257: @todo
1.10 paf 258: IIS: remove trailing default-document[index.html] from $request.uri.
259: to do that we need to consult metabase,
1.12 paf 260: wich is tested&works but seems slow runtime
261: and not could-be-quickly-implemented if prepared.
1.37 parser 262: @test
263: PARSER_VERSION from outside
1.6 paf 264: */
1.53 parser 265:
266: void real_parser_handler(Pool& pool, LPEXTENSION_CONTROL_BLOCK lpECB, bool header_only) {
267: static_cast<SAPI_func_context *>(pool.get_context())->header=new(pool) String(pool);
268:
269: // Request info
270: Request::Info request_info;
271:
272: size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
273: char *filespec_to_process=(char *)pool.malloc(path_translated_buf_size);
274: memcpy(filespec_to_process, lpECB->lpszPathTranslated, path_translated_buf_size);
275: #ifdef WIN32
276: back_slashes_to_slashes(filespec_to_process);
277: #endif
278:
279: if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
280: // IIS
281: size_t len=strlen(filespec_to_process)-strlen(path_info);
282: char *buf=(char *)pool.malloc(len+1);
283: strncpy(buf, filespec_to_process, len); buf[len]=0;
284: request_info.document_root=buf;
285: } else
286: throw Exception(0, 0,
287: 0,
288: "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
289:
290: request_info.path_translated=filespec_to_process;
291: request_info.method=lpECB->lpszMethod;
292: request_info.query_string=lpECB->lpszQueryString;
293: if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
294: char *reconstructed_uri=(char *)pool.malloc(
295: strlen(lpECB->lpszPathInfo)+1/*'?'*/+
296: strlen(lpECB->lpszQueryString)+1/*0*/);
297: strcpy(reconstructed_uri, lpECB->lpszPathInfo);
298: strcat(reconstructed_uri, "?");
299: strcat(reconstructed_uri, lpECB->lpszQueryString);
300: request_info.uri=reconstructed_uri;
301: } else
302: request_info.uri=lpECB->lpszPathInfo;
303:
304: request_info.content_type=lpECB->lpszContentType;
305: request_info.content_length=lpECB->cbTotalBytes;
306: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
307: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
308:
309:
310: // prepare to process request
311: Request request(pool,
312: request_info,
1.60 paf 313: String::UL_HTML|String::UL_OPTIMIZE_BIT,
1.58 paf 314: false /* status_allowed */);
1.53 parser 315:
316: // some root-controlled location
317: // c:\windows
318: char root_config_path[MAX_STRING];
319: GetWindowsDirectory(root_config_path, MAX_STRING);
320: // must be dynamic: rethrowing from request.core
321: // may return 'source' which can be inside of 'root auto.p@exeception'
322: char *root_config_filespec=(char *)pool.malloc(MAX_STRING);
323: snprintf(root_config_filespec, MAX_STRING,
324: "%s/%s",
325: root_config_path, CONFIG_FILE_NAME);
326:
1.65 paf 327: // beside by binary
328: static char site_config_path[MAX_STRING];
329: strncpy(site_config_path, argv0, MAX_STRING-1); site_config_path[MAX_STRING-1]=0; // filespec of my binary
330: if(!(
331: rsplit(site_config_path, '/') ||
332: rsplit(site_config_path, '\\'))) { // strip filename
333: // no path, just filename
334: site_config_path[0]='.'; site_config_path[1]=0;
335: }
336: char site_config_filespec[MAX_STRING];
337: snprintf(site_config_filespec, MAX_STRING,
338: "%s/%s",
339: site_config_path, CONFIG_FILE_NAME);
340:
1.53 parser 341: // process the request
342: request.core(
1.66 ! paf 343: root_config_filespec, false /*don't fail_on_read_problem*/, // /path/to/root/parser3.conf
1.65 paf 344: site_config_filespec, false /*don't fail_on_read_problem*/, // /path/to/site/parser3.conf
1.53 parser 345: header_only);
346: }
347:
348: void call_real_parser_handler__do_SEH(Pool& pool,
349: LPEXTENSION_CONTROL_BLOCK lpECB,
350: bool header_only) {
1.54 parser 351: #if _MSC_VER & !defined(_DEBUG)
1.53 parser 352: LPEXCEPTION_POINTERS system_exception=0;
353: __try {
354: #endif
355: real_parser_handler(pool, lpECB, header_only);
356:
1.54 parser 357: #if _MSC_VER & !defined(_DEBUG)
1.53 parser 358: } __except (
359: (system_exception=GetExceptionInformation()),
360: EXCEPTION_EXECUTE_HANDLER) {
361:
362: if(system_exception)
363: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
364: throw Exception(0, 0,
365: 0,
366: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
367: else
368: throw Exception(0, 0, 0, "Exception <no exception record>");
369: else
370: throw Exception(0, 0, 0, "Exception <no exception information>");
371: }
372: #endif
373: }
374:
375:
1.1 paf 376: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13 paf 377: Pool_storage pool_storage;
1.15 paf 378: Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
379: SAPI_func_context ctx={
380: lpECB,
1.18 paf 381: 0, // filling later: so that if there would be error pool would have ctx
1.39 parser 382: 200 // default http_response_code
1.15 paf 383: };
384: pool.set_context(&ctx);// no allocations before this line!
1.2 paf 385:
1.1 paf 386: bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
1.53 parser 387: try { // global try
388: call_real_parser_handler__do_SEH(pool, lpECB, header_only);
1.2 paf 389: // successful finish
1.53 parser 390: } catch(const Exception& e) { // global problem
1.12 paf 391: // don't allocate anything on pool here:
392: // possible pool' exception not catch-ed now
1.31 paf 393: // and there could be out-of-memory exception
1.1 paf 394: const char *body=e.comment();
1.16 paf 395: // log it
396: SAPI::log(pool, "exception in request exception handler: %s", body);
397:
398: //
1.1 paf 399: int content_length=strlen(body);
400:
1.12 paf 401: // prepare header // not using SAPI func wich allocates on pool
402: char header_buf[MAX_STRING];
403: int header_len=snprintf(header_buf, MAX_STRING,
1.30 paf 404: "content-type: text/plain\r\n"
405: "content-length: %lu\r\n"
406: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
407: "\r\n",
1.12 paf 408: content_length);
409:
410: HSE_SEND_HEADER_EX_INFO header_info;
411: header_info.pszStatus="200 OK";
412: header_info.cchStatus=strlen(header_info.pszStatus);
413: header_info.pszHeader=header_buf;
414: header_info.cchHeader=header_len;
415: header_info.fKeepConn=true;
416:
1.1 paf 417: // send header
1.12 paf 418: lpECB->dwHttpStatusCode=200;
419: lpECB->ServerSupportFunction(lpECB->ConnID,
420: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 421:
422: // send body
423: if(!header_only)
1.9 paf 424: SAPI::send_body(pool, body, content_length);
1.1 paf 425:
426: // unsuccessful finish
427: }
1.65 paf 428: /*
429: const char *body="test";
430:
431: //
432: int content_length=strlen(body);
433:
434: // prepare header // not using SAPI func wich allocates on pool
435: char header_buf[MAX_STRING];
436: int header_len=snprintf(header_buf, MAX_STRING,
437: "content-type: text/plain\r\n"
438: "content-length: %lu\r\n"
439: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
440: "\r\n",
441: content_length);
442: HSE_SEND_HEADER_EX_INFO header_info;
443: header_info.pszStatus="200 OK";
444: header_info.cchStatus=strlen(header_info.pszStatus);
445: header_info.pszHeader=header_buf;
446: header_info.cchHeader=header_len;
447: header_info.fKeepConn=true;
448:
449: // send header
450: lpECB->dwHttpStatusCode=200;
451: lpECB->ServerSupportFunction(lpECB->ConnID,
452: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
453:
454: // send body
455: DWORD num_bytes=content_length;
456: lpECB->WriteClient(lpECB->ConnID,
457: (void *)body, &num_bytes, HSE_IO_SYNC);
458: */
1.66 ! paf 459:
1.6 paf 460: return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1 paf 461: }
1.65 paf 462:
463: BOOL WINAPI DllMain(
464: HINSTANCE hinstDLL, // handle to the DLL module
465: DWORD fdwReason, // reason for calling function
466: LPVOID lpvReserved // reserved
467: ) {
468:
469: GetModuleFileName(
470: hinstDLL, // handle to module
471: argv0, // file name of module
472: sizeof(argv0) // size of buffer
473: );
474:
475: return TRUE;
476: }
E-mail: