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