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