Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.49
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.49 ! parser 7: $Id: parser3isapi.C,v 1.48 2001/10/09 14:25:30 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:
28: #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")
1.44 parser 29:
30: // consts
31:
32: extern const char *main_RCSIds[];
1.46 parser 33: #ifdef USE_SMTP
1.44 parser 34: extern const char *smtp_RCSIds[];
1.45 parser 35: #endif
1.44 parser 36: extern const char *gd_RCSIds[];
37: extern const char *classes_RCSIds[];
38: extern const char *types_RCSIds[];
39: extern const char *parser3isapi_RCSIds[];
1.49 ! parser 40: #ifdef XML
! 41: extern const char *xalan_patched_RCSIds[];
! 42: #endif
1.44 parser 43: const char **RCSIds[]={
44: main_RCSIds,
1.46 parser 45: #ifdef USE_SMTP
1.44 parser 46: smtp_RCSIds,
1.45 parser 47: #endif
1.44 parser 48: gd_RCSIds,
49: classes_RCSIds,
50: types_RCSIds,
51: parser3isapi_RCSIds,
1.48 parser 52: #ifdef XML
53: xalan_patched_RCSIds,
54: #endif
1.44 parser 55: 0
56: };
1.1 paf 57:
1.18 paf 58: // SAPI
59:
1.38 parser 60: #ifndef DOXYGEN
61: /*
1.18 paf 62: ISAPI SAPI functions receive this context information.
1.38 parser 63: see Pool::set_context
1.18 paf 64: */
1.15 paf 65: struct SAPI_func_context {
1.3 paf 66: LPEXTENSION_CONTROL_BLOCK lpECB;
67: String *header;
68: DWORD http_response_code;
69: };
1.38 parser 70: #endif
1.3 paf 71:
1.26 paf 72: // goes to 'cs-uri-query' log file field. webmaster: switch it ON[default OFF].
73: void SAPI::log(Pool& pool, const char *fmt, ...) {
74: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
75:
76: va_list args;
77: va_start(args,fmt);
78: char buf[MAX_STRING];
79: const char *prefix="PARSER_ERROR:";
80: strcpy(buf, prefix);
1.47 parser 81: char *start=buf+strlen(prefix);
82: size_t size=vsnprintf(start, MAX_STRING-strlen(prefix), fmt, args);
83: remove_crlf(start, start+size);
84:
1.26 paf 85: ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID,
86: HSE_APPEND_LOG_PARAMETER, buf, &size, 0);
87: }
88:
1.9 paf 89: const char *SAPI::get_env(Pool& pool, const char *name) {
1.15 paf 90: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.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.15 paf 113: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.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.15 paf 143: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.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.15 paf 160: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1 paf 161:
1.6 paf 162: ctx.header->APPEND_CONST(
1.30 paf 163: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
164: "\r\n");
1.1 paf 165: HSE_SEND_HEADER_EX_INFO header_info;
166:
167: char status_buf[MAX_STATUS_LENGTH];
1.3 paf 168: switch(ctx.http_response_code) {
1.1 paf 169: case 200:
170: header_info.pszStatus="200 OK";
171: break;
172: case 302:
173: header_info.pszStatus="302 Moved Temporarily";
174: break;
1.8 paf 175: case 401:// useless untill parser auth mech
1.1 paf 176: header_info.pszStatus="401 Authorization Required";
1.8 paf 177: break;
1.1 paf 178: default:
1.3 paf 179: snprintf(status_buf, MAX_STATUS_LENGTH,
180: "%d Undescribed", ctx.http_response_code);
1.1 paf 181: header_info.pszStatus=status_buf;
182: break;
183: }
184: header_info.cchStatus=strlen(header_info.pszStatus);
1.3 paf 185: header_info.pszHeader=ctx.header->cstr();
186: header_info.cchHeader=ctx.header->size();
1.5 paf 187: header_info.fKeepConn=true;
1.1 paf 188:
1.3 paf 189: ctx.lpECB->dwHttpStatusCode=ctx.http_response_code;
1.1 paf 190:
1.3 paf 191: ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID,
192: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 193: }
194:
1.23 paf 195: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.15 paf 196: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1 paf 197:
198: DWORD num_bytes=size;
1.3 paf 199: ctx.lpECB->WriteClient(ctx.lpECB->ConnID,
1.23 paf 200: const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC);
1.1 paf 201: }
1.16 paf 202:
1.1 paf 203: //
204:
1.15 paf 205: static bool parser_init() {
1.1 paf 206: static bool globals_inited=false;
207: if(globals_inited)
1.15 paf 208: return true;
1.1 paf 209: globals_inited=true;
210:
1.13 paf 211: static Pool pool(0); // global pool
1.1 paf 212: PTRY {
1.27 paf 213: // init socks
214: init_socks(pool);
215:
1.32 paf 216: // init global classes
217: init_methoded_array(pool);
1.1 paf 218: // init global variables
1.9 paf 219: pa_globals_init(pool);
1.1 paf 220:
1.15 paf 221: // successful finish
222: return true;
1.1 paf 223: } PCATCH(e) { // global problem
1.15 paf 224: //const char *body=e.comment();
225:
226: // unsuccessful finish
227: return false;
1.1 paf 228: }
229: PEND_CATCH
230: }
231:
232: /// ISAPI //
233: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
234: pVer->dwExtensionVersion = HSE_VERSION;
1.36 parser 235: strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN-1);
236: pVer->lpszExtensionDesc[HSE_MAX_EXT_DLL_NAME_LEN-1]=0;
1.15 paf 237: return parser_init();
1.1 paf 238: }
239:
1.6 paf 240: /**
241: ISAPI // main workhorse
242:
1.23 paf 243: @todo
1.10 paf 244: IIS: remove trailing default-document[index.html] from $request.uri.
245: to do that we need to consult metabase,
1.12 paf 246: wich is tested&works but seems slow runtime
247: and not could-be-quickly-implemented if prepared.
1.37 parser 248: @test
249: PARSER_VERSION from outside
1.6 paf 250: */
1.1 paf 251: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13 paf 252: Pool_storage pool_storage;
1.15 paf 253: Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
254: SAPI_func_context ctx={
255: lpECB,
1.18 paf 256: 0, // filling later: so that if there would be error pool would have ctx
1.39 parser 257: 200 // default http_response_code
1.15 paf 258: };
1.41 parser 259: _asm nop; // int 3;
1.15 paf 260: pool.set_context(&ctx);// no allocations before this line!
1.2 paf 261:
1.1 paf 262: bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
263: PTRY { // global try
1.12 paf 264: ctx.header=new(pool) String(pool);
1.3 paf 265:
1.2 paf 266: // Request info
267: Request::Info request_info;
1.14 paf 268:
269: size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
270: char *filespec_to_process=(char *)malloc(path_translated_buf_size);
271: memcpy(filespec_to_process, lpECB->lpszPathTranslated, path_translated_buf_size);
272: #ifdef WIN32
273: back_slashes_to_slashes(filespec_to_process);
274: #endif
275:
1.11 paf 276: if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
277: // IIS
278: size_t len=strlen(filespec_to_process)-strlen(path_info);
279: char *buf=(char *)pool.malloc(len+1);
1.36 parser 280: strncpy(buf, filespec_to_process, len); buf[len]=0;
1.11 paf 281: request_info.document_root=buf;
282: } else
1.6 paf 283: PTHROW(0, 0,
284: 0,
1.11 paf 285: "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
1.6 paf 286:
1.11 paf 287: request_info.path_translated=filespec_to_process;
1.2 paf 288: request_info.method=lpECB->lpszMethod;
289: request_info.query_string=lpECB->lpszQueryString;
290: if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
1.7 paf 291: char *reconstructed_uri=(char *)malloc(
292: strlen(lpECB->lpszPathInfo)+1/*'?'*/+
293: strlen(lpECB->lpszQueryString)+1/*0*/);
294: strcpy(reconstructed_uri, lpECB->lpszPathInfo);
1.2 paf 295: strcat(reconstructed_uri, "?");
1.7 paf 296: strcat(reconstructed_uri, lpECB->lpszQueryString);
1.2 paf 297: request_info.uri=reconstructed_uri;
298: } else
299: request_info.uri=lpECB->lpszPathInfo;
300:
301: request_info.content_type=lpECB->lpszContentType;
302: request_info.content_length=lpECB->cbTotalBytes;
1.9 paf 303: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
1.20 paf 304: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
305:
1.2 paf 306:
307: // prepare to process request
308: Request request(pool,
309: request_info,
1.33 paf 310: String::UL_USER_HTML
1.2 paf 311: );
1.15 paf 312:
1.2 paf 313: // some root-controlled location
1.7 paf 314: // c:\windows
1.42 parser 315: char root_config_path[MAX_STRING];
316: GetWindowsDirectory(root_config_path, MAX_STRING);
1.7 paf 317: // must be dynamic: rethrowing from request.core
318: // may return 'source' which can be inside of 'root auto.p@exeception'
1.42 parser 319: char *root_config_filespec=(char *)pool.malloc(MAX_STRING);
320: snprintf(root_config_filespec, MAX_STRING,
321: "%s/%s",
322: root_config_path, CONFIG_FILE_NAME);
1.4 paf 323:
1.2 paf 324: // process the request
325: request.core(
1.42 parser 326: root_config_filespec, false/*may be abcent*/, // /path/to/admin/auto.p
1.2 paf 327: 0/*parser_site_auto_path*/, false, // /path/to/site/auto.p
328: header_only);
329: // successful finish
1.12 paf 330: } PCATCH(e) { // global problem
331: // don't allocate anything on pool here:
332: // possible pool' exception not catch-ed now
1.31 paf 333: // and there could be out-of-memory exception
1.1 paf 334: const char *body=e.comment();
1.16 paf 335: // log it
336: SAPI::log(pool, "exception in request exception handler: %s", body);
337:
338: //
1.1 paf 339: int content_length=strlen(body);
340:
1.12 paf 341: // prepare header // not using SAPI func wich allocates on pool
342: char header_buf[MAX_STRING];
343: int header_len=snprintf(header_buf, MAX_STRING,
1.30 paf 344: "content-type: text/plain\r\n"
345: "content-length: %lu\r\n"
346: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
347: "\r\n",
1.12 paf 348: content_length);
349:
350: HSE_SEND_HEADER_EX_INFO header_info;
351: header_info.pszStatus="200 OK";
352: header_info.cchStatus=strlen(header_info.pszStatus);
353: header_info.pszHeader=header_buf;
354: header_info.cchHeader=header_len;
355: header_info.fKeepConn=true;
356:
1.1 paf 357: // send header
1.12 paf 358: lpECB->dwHttpStatusCode=200;
359: lpECB->ServerSupportFunction(lpECB->ConnID,
360: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 361:
362: // send body
363: if(!header_only)
1.9 paf 364: SAPI::send_body(pool, body, content_length);
1.1 paf 365:
366: // unsuccessful finish
367: }
368: PEND_CATCH
369:
1.6 paf 370: return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1 paf 371: }
E-mail: