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