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