Annotation of parser3/src/targets/cgi/parser3.C, revision 1.220
1.27 paf 1: /** @file
2: Parser: scripting and CGI main.
3:
1.218 paf 4: Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.154 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.189 paf 6: */
1.27 paf 7:
1.220 ! paf 8: static const char* IDENT_PARSER3_C="$Date: 2003/11/03 11:24:53 $";
1.1 paf 9:
1.40 paf 10: #include "pa_config_includes.h"
1.3 paf 11:
1.139 paf 12: #if _MSC_VER
1.148 paf 13: # include <crtdbg.h>
1.3 paf 14: #endif
1.27 paf 15:
1.37 paf 16: #include "pa_sapi.h"
1.76 paf 17: #include "classes.h"
1.24 paf 18: #include "pa_common.h"
1.2 paf 19: #include "pa_request.h"
1.57 paf 20: #include "pa_socks.h"
1.68 paf 21: #include "pa_version.h"
1.207 paf 22:
1.218 paf 23:
1.69 paf 24:
1.149 paf 25: #ifdef WIN32
26: # include <windows.h>
1.184 paf 27: # include "getopt.h"
28: #else
29: # include <getopt.h>
1.120 parser 30: #endif
31:
1.196 paf 32: //#define DEBUG_MAILRECEIVE "mailreceive.eml"
1.160 paf 33:
1.109 parser 34: // consts
1.84 parser 35:
1.175 paf 36: #define REDIRECT_PREFIX "REDIRECT_"
1.181 paf 37: #define PARSER_CONFIG_ENV_NAME "CGI_PARSER_CONFIG"
1.159 paf 38:
1.42 paf 39: /// IIS refuses to read bigger chunks
40: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M
41:
1.218 paf 42: static const char* argv0;
43: static const char* config_filespec_cstr=0;
1.193 paf 44: static bool fail_on_config_read_problem=true;
1.192 paf 45:
1.184 paf 46: static bool cgi; ///< we were started as CGI?
47: static bool mail_received=false; ///< we were started with -m option? [asked to parse incoming message to $mail:received]
1.5 paf 48:
1.201 paf 49: // for signal handlers
50: Request *request=0;
1.218 paf 51: Request_info *request_info=0;
52: bool execution_canceled=false;
1.201 paf 53:
1.46 paf 54: // SAPI
1.86 parser 55:
1.218 paf 56: class SAPI_Info{} SAPI_info;
57:
58: static void log(const char* fmt, va_list args) {
1.193 paf 59: bool opened=false;
1.61 paf 60: FILE *f=0;
61:
1.193 paf 62: if(config_filespec_cstr) {
63: char beside_config_path[MAX_STRING];
64: strncpy(beside_config_path, config_filespec_cstr, MAX_STRING-1); beside_config_path[MAX_STRING-1]=0;
65: if(!(
66: rsplit(beside_config_path, '/') ||
67: rsplit(beside_config_path, '\\'))) { // strip filename
68: // no path, just filename
69: beside_config_path[0]='.'; beside_config_path[1]=0;
70: }
71:
72: char file_spec[MAX_STRING];
73: snprintf(file_spec, MAX_STRING,
74: "%s/parser3.log", beside_config_path);
75: f=fopen(file_spec, "at");
76: opened=f!=0;
77: }
1.192 paf 78: // fallback to stderr
1.61 paf 79: if(!opened)
80: f=stderr;
1.208 paf 81:
82: // use no memory [so that we could log out-of-memory error]
83: setbuf(f, 0); // stderr stream is unbuffered by default, but still...
1.61 paf 84:
85: // prefix
86: time_t t=time(0);
1.218 paf 87: if(const char* stamp=ctime(&t)) { // never saw that
1.173 paf 88: if(size_t len=strlen(stamp)) // saw once stamp being =""
1.172 paf 89: fprintf(f, "[%.*s] ", len-1, stamp);
1.171 paf 90: }
1.61 paf 91: // message
1.117 parser 92:
93: char buf[MAX_STRING];
94: size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
95: remove_crlf(buf, buf+size);
96:
97: fwrite(buf, size, 1, f);
1.61 paf 98: // newline
99: fprintf(f, "\n");
100:
101: if(opened)
102: fclose(f);
1.85 parser 103: else
104: fflush(f);
1.124 parser 105: }
106:
107: // appends to parser3.log located beside my binary if openable, to stderr otherwize
1.218 paf 108: void SAPI::log(SAPI_Info&, const char* fmt, ...) {
1.124 parser 109: va_list args;
110: va_start(args,fmt);
111: ::log(fmt, args);
112: va_end(args);
113: }
114:
1.218 paf 115: static void die_or_abort(const char* fmt, va_list args, bool write_core) {
1.138 paf 116: // log
117:
118: // logging is more important than user
1.204 paf 119: // she can cancel download, we'd get SIGPIPE,
1.138 paf 120: // nothing would be logged then
1.124 parser 121: ::log(fmt, args);
122:
1.138 paf 123: // inform user
124:
1.134 paf 125: char body[MAX_STRING];
1.138 paf 126: int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134 paf 127:
128: // prepare header
129: // let's be honest, that's bad we couldn't produce valid output
1.218 paf 130: SAPI::add_header_attribute(SAPI_info, "status", "500");
131: SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
1.134 paf 132: char content_length_cstr[MAX_NUMBER];
1.168 paf 133: snprintf(content_length_cstr, sizeof(content_length_cstr), "%u", content_length);
1.218 paf 134: SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
1.134 paf 135:
136: // send header
1.218 paf 137: SAPI::send_header(SAPI_info);
1.134 paf 138:
139: // body
1.218 paf 140: SAPI::send_body(SAPI_info, body, content_length);
1.134 paf 141:
1.218 paf 142: // exit & try to produce core dump[unix] or invoke debugger[Win32 Debug version]
143: if(write_core) {
144: #if defined(WIN32) && !defined(_DEBUG)
145: // IIS with abort failes to show STDOUT, it just barks "abnormal program termination"
146: exit(1);
1.214 paf 147: #else
1.218 paf 148: #if _MSC_VER
149: _asm int 3;
150: #endif
151: abort();
1.214 paf 152: #endif
1.218 paf 153: }
154: else
155: exit(1);
156: }
157:
158: void SAPI::die(const char* fmt, ...) {
159: va_list args;
160: va_start(args, fmt);
161: die_or_abort(fmt, args, false/*write core?*/);
162: va_end(args);
1.61 paf 163: }
164:
1.218 paf 165: void SAPI::abort(const char* fmt, ...) {
166: va_list args;
167: va_start(args, fmt);
168: die_or_abort(fmt, args, true/*write core?*/);
169: va_end(args);
1.28 paf 170: }
171:
1.218 paf 172: char* SAPI::get_env(SAPI_Info& , const char* name) {
173: if(char *local=getenv(name))
174: return pa_strdup(local);
175: else
176: return 0;
177: }
178:
179: const char* const *SAPI::environment(SAPI_Info&) {
1.180 paf 180: #ifdef _MSC_VER
181: extern char **_environ;
182: return _environ;
183: #else
184: extern char **environ;
185: return environ;
186: #endif
187: }
188:
1.218 paf 189: size_t SAPI::read_post(SAPI_Info& , char *buf, size_t max_bytes) {
1.59 paf 190: size_t read_size=0;
1.12 paf 191: do {
1.200 paf 192: ssize_t chunk_size=read(fileno(stdin),
1.42 paf 193: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129 paf 194: if(chunk_size<=0)
1.12 paf 195: break;
196: read_size+=chunk_size;
197: } while(read_size<max_bytes);
198:
199: return read_size;
1.10 paf 200: }
201:
1.218 paf 202: void SAPI::add_header_attribute(SAPI_Info& , const char* dont_store_key, const char* dont_store_value) {
1.68 paf 203: if(cgi)
1.218 paf 204: printf("%s: %s\n", dont_store_key, dont_store_value);
1.19 paf 205: }
206:
1.56 paf 207: /// @todo intelligent cache-control
1.218 paf 208: void SAPI::send_header(SAPI_Info& ) {
1.33 paf 209: if(cgi) {
1.147 paf 210: // puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 211:
212: // header | body delimiter
1.20 paf 213: puts("");
1.33 paf 214: }
1.30 paf 215: }
1.20 paf 216:
1.218 paf 217: void SAPI::send_body(SAPI_Info& , const void *buf, size_t size) {
1.19 paf 218: stdout_write(buf, size);
1.58 paf 219: }
220:
1.97 parser 221: //
222:
1.218 paf 223: static void full_file_spec(const char* file_name, char *buf, size_t buf_size) {
1.210 paf 224: if(file_name)
1.167 paf 225: if(file_name[0]=='/'
226: #ifdef WIN32
1.210 paf 227: || file_name[0] && file_name[1]==':'
1.167 paf 228: #endif
1.210 paf 229: )
1.218 paf 230: strncpy(buf, file_name, buf_size);
1.167 paf 231: else {
232: char cwd[MAX_STRING]; getcwd(cwd, MAX_STRING);
233: snprintf(buf, buf_size, "%s/%s", cwd, file_name);
234: }
235: else
236: buf[0]=0;
1.166 paf 237: #ifdef WIN32
238: back_slashes_to_slashes(buf);
239: #endif
1.97 parser 240: }
241:
1.218 paf 242: static void log_signal(const char* signal_name) {
243: if(request_info)
1.219 paf 244: SAPI::log(SAPI_info, "%s received while %s. uri=%s, method=%s, cl=%u",
1.218 paf 245: signal_name,
246: request?"executing code":"reading data",
247: request_info->uri,
248: request_info->method,
249: request_info->content_length);
250: else
251: SAPI::log(SAPI_info, "%s received before or after processing request",
252: signal_name);
1.205 paf 253: }
254:
1.201 paf 255: #ifdef SIGUSR1
1.205 paf 256: static void SIGUSR1_handler(int /*sig*/){
257: log_signal("SIGUSR1");
1.201 paf 258: }
259: #endif
260:
261: #ifdef SIGPIPE
1.205 paf 262: static void SIGPIPE_handler(int /*sig*/){
263: log_signal("SIGPIPE");
1.218 paf 264: execution_canceled=true;
1.201 paf 265: if(request)
1.218 paf 266: request->set_interrupted(true);
1.201 paf 267: }
268: #endif
269:
1.40 paf 270: /**
1.122 parser 271: main workhorse
1.19 paf 272:
1.122 parser 273: @todo
1.40 paf 274: IIS: remove trailing default-document[index.html] from $request.uri.
275: to do that we need to consult metabase,
276: wich is tested but seems slow.
277: */
1.218 paf 278: static void real_parser_handler(const char* filespec_to_process,
279: const char* request_method, bool header_only) {
1.122 parser 280: // init socks
1.218 paf 281: pa_init_socks();
1.122 parser 282:
283: // init global variables
1.218 paf 284: pa_globals_init();
1.122 parser 285:
1.186 paf 286: if(!filespec_to_process || !*filespec_to_process)
1.144 paf 287: SAPI::die("Parser/%s", PARSER_VERSION);
1.122 parser 288:
289: // Request info
1.218 paf 290: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
1.166 paf 291: char document_root_buf[MAX_STRING];
1.122 parser 292: if(cgi) {
1.218 paf 293: if(const char* env_document_root=getenv("DOCUMENT_ROOT"))
1.122 parser 294: request_info.document_root=env_document_root;
1.218 paf 295: else if(const char* path_info=getenv("PATH_INFO")) {
1.122 parser 296: // IIS
1.166 paf 297: size_t len=min(sizeof(document_root_buf)-1, strlen(filespec_to_process)-strlen(path_info));
298: memcpy(document_root_buf, filespec_to_process, len); document_root_buf[len]=0;
299: request_info.document_root=document_root_buf;
1.122 parser 300: } else
1.165 paf 301: throw Exception("parser.runtime",
302: 0,
303: "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.122 parser 304: } else {
1.166 paf 305: full_file_spec("", document_root_buf, sizeof(document_root_buf));
306: request_info.document_root=document_root_buf;
1.122 parser 307: }
308: request_info.path_translated=filespec_to_process;
309: request_info.method=request_method ? request_method : "GET";
1.218 paf 310: const char* query_string=getenv("QUERY_STRING");
1.122 parser 311: request_info.query_string=query_string;
312: if(cgi) {
1.214 paf 313: // few absolute obligatory
1.218 paf 314: const char* path_info=getenv("PATH_INFO");
1.214 paf 315: if(!path_info)
316: SAPI::die("CGI: illegal call (missing PATH_INFO)");
1.218 paf 317: const char* script_name=getenv("SCRIPT_NAME");
1.214 paf 318: if(!script_name)
319: SAPI::die("CGI: illegal call (missing SCRIPT_NAME)");
320:
1.218 paf 321: const char* env_request_uri=getenv("REQUEST_URI");
1.214 paf 322: if(env_request_uri)
1.122 parser 323: request_info.uri=env_request_uri;
1.214 paf 324: else
1.122 parser 325: if(query_string) {
1.218 paf 326: char* reconstructed_uri=new(PointerFreeGC) char[
1.122 parser 327: strlen(path_info)+1/*'?'*/+
1.218 paf 328: strlen(query_string)+1/*0*/];
1.122 parser 329: strcpy(reconstructed_uri, path_info);
330: strcat(reconstructed_uri, "?");
331: strcat(reconstructed_uri, query_string);
332: request_info.uri=reconstructed_uri;
333: } else
334: request_info.uri=path_info;
1.214 paf 335:
336: if(env_request_uri) { // apache & others stuck to standards
337: /*
338: http://parser3/env.html?123 =OK
339: $request:uri=/env.html?123
340: REQUEST_URI='/env.html?123'
341: SCRIPT_NAME='/cgi-bin/parser3'
342: PATH_INFO='/env.html'
343:
344: http://parser3/cgi-bin/parser3/env.html?123 =ERROR
345: $request:uri=/cgi-bin/parser3/env.html?123
346: REQUEST_URI='/cgi-bin/parser3/env.html?123'
347: SCRIPT_NAME='/cgi-bin/parser3'
348: PATH_INFO='/env.html'
349: */
350: size_t script_name_len=strlen(script_name);
351: size_t uri_len=strlen(env_request_uri);
352: if(strncmp(env_request_uri, script_name, script_name_len)==0 &&
353: script_name_len != uri_len) // under IIS they are the same
354: SAPI::die("CGI: illegal call (1)");
355: } else { // seen on IIS5
356: /*
357: http://nestle/env.html?123 =OK
358: $request:uri=/env.html?123
359: REQUEST_URI=''
360: SCRIPT_NAME='/env.html'
361: PATH_INFO='/env.html'
362:
363: http://nestle/cgi-bin/parser3.exe/env.html =ERROR
364: $request:uri=/env.html
365: REQUEST_URI=''
366: SCRIPT_NAME='/cgi-bin/parser3.exe'
367: PATH_INFO='/env.html'
368: */
369: if(strcmp(script_name, path_info)!=0)
370: SAPI::die("CGI: illegal call (2)");
371: }
1.122 parser 372: } else
1.177 paf 373: request_info.uri="";
1.122 parser 374:
1.218 paf 375: request_info.content_type=getenv("CONTENT_TYPE");
376: const char* content_length=getenv("CONTENT_LENGTH");
1.122 parser 377: request_info.content_length=(content_length?atoi(content_length):0);
1.218 paf 378: request_info.cookie=getenv("HTTP_COOKIE");
1.184 paf 379: request_info.mail_received=mail_received;
380:
1.218 paf 381: // get request_info ptr for signal handlers
382: ::request_info=&request_info;
383: if(execution_canceled)
384: SAPI::die("Execution canceled");
1.198 paf 385:
1.122 parser 386: // prepare to process request
1.218 paf 387: Request request(SAPI_info,
1.122 parser 388: request_info,
1.218 paf 389: cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS,
1.130 paf 390: true /* status_allowed */);
1.201 paf 391:
392: // get request ptr for signal handlers
393: ::request=&request;
394:
1.181 paf 395: char config_filespec_buf[MAX_STRING];
1.193 paf 396: if(!config_filespec_cstr) {
1.218 paf 397: const char* config_by_env=getenv(PARSER_CONFIG_ENV_NAME);
1.193 paf 398: if(!config_by_env)
399: config_by_env=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
400: if(config_by_env)
401: config_filespec_cstr=config_by_env;
402: else {
403: // beside by binary
404: char beside_binary_path[MAX_STRING];
405: strncpy(beside_binary_path, argv0, MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
406: if(!(
407: rsplit(beside_binary_path, '/') ||
408: rsplit(beside_binary_path, '\\'))) { // strip filename
409: // no path, just filename
410: // @todo full path, not ./!
411: beside_binary_path[0]='.'; beside_binary_path[1]=0;
412: }
413: snprintf(config_filespec_buf, MAX_STRING,
414: "%s/%s",
415: beside_binary_path, AUTO_FILE_NAME);
416: config_filespec_cstr=config_filespec_buf;
1.197 paf 417: fail_on_config_read_problem=entry_exists(config_filespec_cstr);
1.193 paf 418: }
1.122 parser 419: }
420:
421: // process the request
422: request.core(
1.193 paf 423: config_filespec_cstr, fail_on_config_read_problem,
1.122 parser 424: header_only);
1.201 paf 425:
426: // no request [prevent signal handlers from accessing invalid memory]
427: ::request=0;
1.122 parser 428:
429: //
1.218 paf 430: pa_done_socks();
431: }
432:
433: #if _MSC_VER && !defined(_DEBUG)
434: # define PA_USE___TRY
435: struct Parser_executed {
436: bool with_system_exception;
437: LPEXCEPTION_POINTERS system_exception;
438: };
1.122 parser 439: #endif
1.160 paf 440:
1.218 paf 441: static
442: #ifdef PA_USE___TRY
443: Parser_executed
444: #else
445: void
1.160 paf 446: #endif
1.218 paf 447: call_real_parser_handler__do_SEH_return_it(
448: const char* filespec_to_process,
449: const char* request_method, bool header_only) {
450: #ifdef PA_USE___TRY
451: Parser_executed result={false};
1.122 parser 452: __try {
453: #endif
454: real_parser_handler(
455: filespec_to_process,
456: request_method, header_only);
457:
1.218 paf 458: #ifdef PA_USE___TRY
1.122 parser 459: } __except (
1.218 paf 460: (result.system_exception=GetExceptionInformation()),
1.122 parser 461: EXCEPTION_EXECUTE_HANDLER) {
1.218 paf 462: result.with_system_exception=true;
1.122 parser 463: }
1.218 paf 464: return result;
1.122 parser 465: #endif
466: }
467:
1.218 paf 468: static void call_real_parser_handler__do_SEH(
469: const char* filespec_to_process,
470: const char* request_method, bool header_only) {
471: #ifdef PA_USE___TRY
472: Parser_executed executed=
1.135 paf 473: #endif
1.218 paf 474: call_real_parser_handler__do_SEH_return_it(filespec_to_process,
475: request_method, header_only);
1.131 paf 476:
1.218 paf 477: #ifdef PA_USE___TRY
478: if(executed.with_system_exception)
479: if(executed.system_exception)
480: if(_EXCEPTION_RECORD *er=executed.system_exception->ExceptionRecord)
481: throw Exception(0,
482: 0,
483: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
484: else
485: throw Exception(0,
486: 0,
487: "Exception <no exception record>");
488: else
489: throw Exception(0,
490: 0,
491: "Exception <no exception information>");
492: #endif
1.131 paf 493: }
494:
1.218 paf 495: static void usage(const char* program) {
1.188 paf 496: printf(
1.218 paf 497: "Parser/%s Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)\n"
1.184 paf 498: "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
499: "\n"
500: "Usage: %s [options] file\n"
501: "Options are:\n"
1.185 paf 502: #ifdef WITH_MAILRECEIVE
1.193 paf 503: " -m Parse mail, put received letter to $mail:received\n"
1.184 paf 504: #endif
1.193 paf 505: " -f config_file Use this config file (/path/to/auto.p)\n"
506: " -h Display usage information (this message)\n"
1.184 paf 507: , PARSER_VERSION,
508: program);
509: exit(EINVAL);
510: }
511:
1.218 paf 512: int main(int argc, char *argv[]) {
513: GC_java_finalization=0;
1.217 paf 514:
1.218 paf 515: #ifndef PA_DEBUG_DISABLE_GC
516: // Dont collect unless explicitly requested
517: // this is quicker (~30% ), but less memory-efficient(~8%)
518: // so deciding for speed
519: GC_dont_gc=1;
520: #endif
521: /*
522:
523: Array<int> test;
524: test+=3;
525: test+=4;
526: // int a=test.count();
527: int i=0;
528: scanf("%d", &i);
529: int b=test.get(i);
530: // int b=test.get(10);
531: printf("%d", b);//test.count());*/
1.217 paf 532:
1.203 paf 533: #ifdef SIGUSR1
1.205 paf 534: if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR)
1.203 paf 535: SAPI::die("Can not set handler for SIGUSR1");
536: #endif
537: #ifdef SIGPIPE
1.205 paf 538: if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR)
1.203 paf 539: SAPI::die("Can not set handler for SIGPIPE");
540: #endif
541:
542:
1.185 paf 543: #ifdef DEBUG_MAILRECEIVE
544: if(FILE *fake_in=fopen(DEBUG_MAILRECEIVE, "rt")) {
1.184 paf 545: dup2(fake_in->_file, 0/*STDIN_FILENO*/);
546: }
547: #endif
548:
1.178 paf 549: #ifdef _DEBUG
1.220 ! paf 550: //_crtBreakAlloc=46;
1.178 paf 551: #endif
1.193 paf 552: argv0=argv[0];
1.45 paf 553:
1.32 paf 554: umask(2);
555:
1.3 paf 556: // were we started as CGI?
1.146 paf 557: cgi=
1.109 parser 558: getenv("SERVER_SOFTWARE") ||
559: getenv("SERVER_NAME") ||
560: getenv("GATEWAY_INTERFACE") ||
561: getenv("REQUEST_METHOD");
1.5 paf 562:
1.184 paf 563: char *raw_filespec_to_process;
1.210 paf 564: if(cgi) {
1.184 paf 565: raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.210 paf 566: if(raw_filespec_to_process && !*raw_filespec_to_process)
567: raw_filespec_to_process=0;
568: } else {
1.184 paf 569: optind = 1;
570: opterr = 0;
571: int c;
1.193 paf 572: while((c = getopt(argc, argv, "hf:"
1.185 paf 573: #ifdef WITH_MAILRECEIVE
1.184 paf 574: "m"
575: #endif
576: )) > 0) {
577: switch (c) {
578: case 'h':
579: usage(argv[0]);
1.193 paf 580: break;
581: case 'f':
582: config_filespec_cstr=optarg;
1.184 paf 583: break;
1.185 paf 584: #ifdef WITH_MAILRECEIVE
1.184 paf 585: case 'm':
586: mail_received=true;
587: break;
588: #endif
589: default:
590: fprintf(stderr, "%s: invalid option '%c'\n", argv[0], optopt);
591: usage(argv[0]);
592: break;
593: }
594: }
595: if (optind != argc - 1) {
596: fprintf(stderr, "%s: file not specified\n", argv[0]);
597: usage(argv[0]);
1.10 paf 598: }
1.184 paf 599:
600: raw_filespec_to_process=argv[optind++];
1.10 paf 601: }
602:
1.100 parser 603: #ifdef WIN32
604: setmode(fileno(stdin), _O_BINARY);
605: setmode(fileno(stdout), _O_BINARY);
606: setmode(fileno(stderr), _O_BINARY);
607: #endif
608:
1.218 paf 609: #if defined(_MSC_VER) && defined(_DEBUG)
1.148 paf 610: // Get current flag
611: int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
612:
613: // Turn on leak-checking bit
614: tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
615:
616: // Set flag to the new value
617: _CrtSetDbgFlag( tmpFlag );
1.218 paf 618: // _CrtSetBreakAlloc(61);
1.138 paf 619:
1.218 paf 620: _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
621: _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138 paf 622: #endif
623:
1.166 paf 624: char filespec_to_process[MAX_STRING];
625: full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10 paf 626:
1.218 paf 627: const char* request_method=getenv("REQUEST_METHOD");
1.35 paf 628: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131 paf 629:
1.122 parser 630: try { // global try
631: call_real_parser_handler__do_SEH(
632: filespec_to_process,
633: request_method, header_only);
634: } catch(const Exception& e) { // global problem
1.44 paf 635: // don't allocate anything on pool here:
636: // possible pool' exception not catch-ed now
637: // and there could be out-of-memory exception
1.218 paf 638: const char* body=e.comment();
639: char buf[MAX_STRING];
640: snprintf(buf, MAX_STRING, "exception in request exception handler: %s",
641: body);
642: // log it
643: SAPI::log(SAPI_info, "%s", buf);
644:
645: //
646: int content_length=strlen(buf);
647:
648: // prepare header
649: SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
650: char content_length_cstr[MAX_NUMBER];
651: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
652: SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
653:
654: // send header
655: SAPI::send_header(SAPI_info);
656:
657: // send body
658: if(!header_only)
659: SAPI::send_body(SAPI_info, buf, content_length);
1.43 paf 660:
1.218 paf 661: // unsuccessful finish
1.16 paf 662: }
1.109 parser 663:
1.134 paf 664: return 0;
1.1 paf 665: }
E-mail: