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