Annotation of parser3/src/targets/isapi/parser3isapi.C, revision 1.30
1.29 paf 1: /** @file
2: Parser: IIS extension.
3:
4: Copyright (c) 2000,2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.30 ! paf 8: $Id: parser3isapi.C,v 1.29 2001/04/19 11:57:05 paf Exp $
1.29 paf 9: */
10:
1.1 paf 11: #ifndef _MSC_VER
1.10 paf 12: # error compile ISAPI module with MSVC [no urge for now to make it autoconf-ed (PAF)]
1.1 paf 13: #endif
14:
15: #include <windows.h>
16: #include <process.h>
17:
18: #include <httpext.h>
19:
1.9 paf 20: #include "pa_sapi.h"
1.1 paf 21: #include "pa_globals.h"
22: #include "pa_request.h"
23: #include "pa_version.h"
1.13 paf 24: #include "pool_storage.h"
1.24 paf 25: #include "pa_socks.h"
1.1 paf 26:
27: #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST STATUS DESCRIPTION")
28:
1.18 paf 29: // SAPI
30:
31: /**
32: ISAPI SAPI functions receive this context information.
33:
34: @see Pool::set_context
35: */
1.15 paf 36: struct SAPI_func_context {
1.3 paf 37: LPEXTENSION_CONTROL_BLOCK lpECB;
38: String *header;
39: DWORD http_response_code;
40: };
41:
1.26 paf 42: // goes to 'cs-uri-query' log file field. webmaster: switch it ON[default OFF].
43: void SAPI::log(Pool& pool, const char *fmt, ...) {
44: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
45:
46: va_list args;
47: va_start(args,fmt);
48: char buf[MAX_STRING];
49: const char *prefix="PARSER_ERROR:";
50: strcpy(buf, prefix);
51: DWORD size=vsnprintf(buf+strlen(prefix), MAX_STRING-strlen(prefix), fmt, args);
52:
53: ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID,
54: HSE_APPEND_LOG_PARAMETER, buf, &size, 0);
55: }
56:
1.9 paf 57: const char *SAPI::get_env(Pool& pool, const char *name) {
1.15 paf 58: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1 paf 59:
60: char *variable_buf=(char *)pool.malloc(MAX_STRING);
61: DWORD variable_len = MAX_STRING-1;
62:
1.3 paf 63: if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name),
1.1 paf 64: variable_buf, &variable_len)) {
65: variable_buf[variable_len]=0;
66: return variable_buf;
1.7 paf 67: } else if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
68: variable_buf=(char *)pool.malloc(variable_len+1);
69:
70: if(ctx.lpECB->GetServerVariable(ctx.lpECB->ConnID, const_cast<char *>(name),
71: variable_buf, &variable_len)) {
72: variable_buf[variable_len]=0;
73: return variable_buf;
74: }
1.1 paf 75: }
1.7 paf 76:
1.1 paf 77: return 0;
78: }
79:
1.26 paf 80: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
1.15 paf 81: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1 paf 82:
83: DWORD read_from_buf=0;
84: DWORD read_from_input=0;
85: DWORD total_read=0;
86:
1.3 paf 87: read_from_buf=min(ctx.lpECB->cbAvailable, max_bytes);
88: memcpy(buf, ctx.lpECB->lpbData, read_from_buf);
1.1 paf 89: total_read+=read_from_buf;
90:
91: if(read_from_buf<max_bytes &&
1.3 paf 92: read_from_buf<ctx.lpECB->cbTotalBytes) {
1.1 paf 93: DWORD cbRead=0, cbSize;
94:
1.3 paf 95: read_from_input=min(max_bytes-read_from_buf,
96: ctx.lpECB->cbTotalBytes-read_from_buf);
1.1 paf 97: while(cbRead < read_from_input) {
98: cbSize=read_from_input - cbRead;
1.3 paf 99: if(!ctx.lpECB->ReadClient(ctx.lpECB->ConnID,
100: buf+read_from_buf+cbRead, &cbSize) ||
1.1 paf 101: cbSize==0)
102: break;
103: cbRead+=cbSize;
104: }
105: total_read+=cbRead;
106: }
107: return total_read;
108: }
109:
1.9 paf 110: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.15 paf 111: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.3 paf 112:
113: if(strcasecmp(key, "location")==0)
114: ctx.http_response_code=302;
115:
116: if(strcasecmp(key, "status")==0)
117: ctx.http_response_code=atoi(value);
118: else {
119: ctx.header->APPEND_CONST(key);
120: ctx.header->APPEND_CONST(": ");
121: ctx.header->APPEND_CONST(value);
1.30 ! paf 122: ctx.header->APPEND_CONST("\r\n");
1.3 paf 123: }
1.1 paf 124: }
125:
1.23 paf 126: /// @todo intelligent cache-control
1.9 paf 127: void SAPI::send_header(Pool& pool) {
1.15 paf 128: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1 paf 129:
1.6 paf 130: ctx.header->APPEND_CONST(
1.30 ! paf 131: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
! 132: "\r\n");
1.1 paf 133: HSE_SEND_HEADER_EX_INFO header_info;
134:
135: char status_buf[MAX_STATUS_LENGTH];
1.3 paf 136: switch(ctx.http_response_code) {
1.1 paf 137: case 200:
138: header_info.pszStatus="200 OK";
139: break;
140: case 302:
141: header_info.pszStatus="302 Moved Temporarily";
142: break;
1.8 paf 143: case 401:// useless untill parser auth mech
1.1 paf 144: header_info.pszStatus="401 Authorization Required";
1.8 paf 145: break;
1.1 paf 146: default:
1.3 paf 147: snprintf(status_buf, MAX_STATUS_LENGTH,
148: "%d Undescribed", ctx.http_response_code);
1.1 paf 149: header_info.pszStatus=status_buf;
150: break;
151: }
152: header_info.cchStatus=strlen(header_info.pszStatus);
1.3 paf 153: header_info.pszHeader=ctx.header->cstr();
154: header_info.cchHeader=ctx.header->size();
1.5 paf 155: header_info.fKeepConn=true;
1.1 paf 156:
1.3 paf 157: ctx.lpECB->dwHttpStatusCode=ctx.http_response_code;
1.1 paf 158:
1.3 paf 159: ctx.lpECB->ServerSupportFunction(ctx.lpECB->ConnID,
160: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 161: }
162:
1.23 paf 163: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.15 paf 164: SAPI_func_context& ctx=*static_cast<SAPI_func_context *>(pool.context());
1.1 paf 165:
166: DWORD num_bytes=size;
1.3 paf 167: ctx.lpECB->WriteClient(ctx.lpECB->ConnID,
1.23 paf 168: const_cast<void *>(buf), &num_bytes, HSE_IO_SYNC);
1.1 paf 169: }
1.16 paf 170:
1.1 paf 171: //
172:
1.15 paf 173: static bool parser_init() {
1.1 paf 174: static bool globals_inited=false;
175: if(globals_inited)
1.15 paf 176: return true;
1.1 paf 177: globals_inited=true;
178:
1.13 paf 179: static Pool pool(0); // global pool
1.1 paf 180: PTRY {
1.27 paf 181: // init socks
182: init_socks(pool);
183:
1.1 paf 184: // init global variables
1.9 paf 185: pa_globals_init(pool);
1.1 paf 186:
1.15 paf 187: // successful finish
188: return true;
1.1 paf 189: } PCATCH(e) { // global problem
1.15 paf 190: //const char *body=e.comment();
191:
192: // unsuccessful finish
193: return false;
1.1 paf 194: }
195: PEND_CATCH
196: }
197:
198: /// ISAPI //
199: BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) {
200: pVer->dwExtensionVersion = HSE_VERSION;
1.15 paf 201: strncpy(pVer->lpszExtensionDesc, "Parser "PARSER_VERSION, HSE_MAX_EXT_DLL_NAME_LEN);
202: return parser_init();
1.1 paf 203: }
204:
1.6 paf 205: /**
206: ISAPI // main workhorse
207:
1.23 paf 208: @todo
1.10 paf 209: IIS: remove trailing default-document[index.html] from $request.uri.
210: to do that we need to consult metabase,
1.12 paf 211: wich is tested&works but seems slow runtime
212: and not could-be-quickly-implemented if prepared.
1.6 paf 213: */
1.1 paf 214: DWORD WINAPI HttpExtensionProc(LPEXTENSION_CONTROL_BLOCK lpECB) {
1.13 paf 215: Pool_storage pool_storage;
1.15 paf 216: Pool pool(&pool_storage); // no allocations until assigned context [for reporting]
217: SAPI_func_context ctx={
218: lpECB,
1.18 paf 219: 0, // filling later: so that if there would be error pool would have ctx
220: 200
1.15 paf 221: };
222: pool.set_context(&ctx);// no allocations before this line!
1.2 paf 223:
1.1 paf 224: bool header_only=strcasecmp(lpECB->lpszMethod, "HEAD")==0;
225: PTRY { // global try
1.12 paf 226: ctx.header=new(pool) String(pool);
1.3 paf 227:
1.2 paf 228: // Request info
229: Request::Info request_info;
1.14 paf 230:
231: size_t path_translated_buf_size=strlen(lpECB->lpszPathTranslated)+1;
232: char *filespec_to_process=(char *)malloc(path_translated_buf_size);
233: memcpy(filespec_to_process, lpECB->lpszPathTranslated, path_translated_buf_size);
234: #ifdef WIN32
235: back_slashes_to_slashes(filespec_to_process);
236: #endif
237:
1.11 paf 238: if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
239: // IIS
240: size_t len=strlen(filespec_to_process)-strlen(path_info);
241: char *buf=(char *)pool.malloc(len+1);
242: strncpy(buf, filespec_to_process, len);
243: buf[len]=0;
244: request_info.document_root=buf;
245: } else
1.6 paf 246: PTHROW(0, 0,
247: 0,
1.11 paf 248: "ISAPI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
1.6 paf 249:
1.11 paf 250: request_info.path_translated=filespec_to_process;
1.2 paf 251: request_info.method=lpECB->lpszMethod;
252: request_info.query_string=lpECB->lpszQueryString;
253: if(lpECB->lpszQueryString && *lpECB->lpszQueryString) {
1.7 paf 254: char *reconstructed_uri=(char *)malloc(
255: strlen(lpECB->lpszPathInfo)+1/*'?'*/+
256: strlen(lpECB->lpszQueryString)+1/*0*/);
257: strcpy(reconstructed_uri, lpECB->lpszPathInfo);
1.2 paf 258: strcat(reconstructed_uri, "?");
1.7 paf 259: strcat(reconstructed_uri, lpECB->lpszQueryString);
1.2 paf 260: request_info.uri=reconstructed_uri;
261: } else
262: request_info.uri=lpECB->lpszPathInfo;
263:
264: request_info.content_type=lpECB->lpszContentType;
265: request_info.content_length=lpECB->cbTotalBytes;
1.9 paf 266: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
1.20 paf 267: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
268:
1.2 paf 269:
270: // prepare to process request
271: Request request(pool,
272: request_info,
273: String::UL_HTML_TYPO
274: );
1.15 paf 275:
1.2 paf 276: // some root-controlled location
1.7 paf 277: // c:\windows
278: // must be dynamic: rethrowing from request.core
279: // may return 'source' which can be inside of 'root auto.p@exeception'
280: char *root_auto_path=(char *)pool.malloc(MAX_STRING);
1.2 paf 281: GetWindowsDirectory(root_auto_path, MAX_STRING);
1.4 paf 282:
1.2 paf 283: // process the request
284: request.core(
285: root_auto_path, false/*may be abcent*/, // /path/to/admin/auto.p
286: 0/*parser_site_auto_path*/, false, // /path/to/site/auto.p
287: header_only);
288: // successful finish
1.12 paf 289: } PCATCH(e) { // global problem
290: // don't allocate anything on pool here:
291: // possible pool' exception not catch-ed now
292: // and there could be out-of-memory exception
1.1 paf 293: const char *body=e.comment();
1.16 paf 294: // log it
295: SAPI::log(pool, "exception in request exception handler: %s", body);
296:
297: //
1.1 paf 298: int content_length=strlen(body);
299:
1.12 paf 300: // prepare header // not using SAPI func wich allocates on pool
301: char header_buf[MAX_STRING];
302: int header_len=snprintf(header_buf, MAX_STRING,
1.30 ! paf 303: "content-type: text/plain\r\n"
! 304: "content-length: %lu\r\n"
! 305: "expires: Fri, 23 Mar 2001 09:32:23 GMT\r\n"
! 306: "\r\n",
1.12 paf 307: content_length);
308:
309: HSE_SEND_HEADER_EX_INFO header_info;
310: header_info.pszStatus="200 OK";
311: header_info.cchStatus=strlen(header_info.pszStatus);
312: header_info.pszHeader=header_buf;
313: header_info.cchHeader=header_len;
314: header_info.fKeepConn=true;
315:
1.1 paf 316: // send header
1.12 paf 317: lpECB->dwHttpStatusCode=200;
318: lpECB->ServerSupportFunction(lpECB->ConnID,
319: HSE_REQ_SEND_RESPONSE_HEADER_EX, &header_info, NULL, NULL);
1.1 paf 320:
321: // send body
322: if(!header_only)
1.9 paf 323: SAPI::send_body(pool, body, content_length);
1.1 paf 324:
325: // unsuccessful finish
326: }
327: PEND_CATCH
328:
1.6 paf 329: return HSE_STATUS_SUCCESS_AND_KEEP_CONN;
1.1 paf 330: }
E-mail: