Annotation of parser3/src/targets/cgi/parser3.C, revision 1.218
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.218 ! paf 8: static const char* IDENT_PARSER3_C="$Date: 2003/07/22 11:37:31 $";
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)
! 244: SAPI::log(SAPI_info, "%s received while %s. uri=%s, method=%s, qs=%s, cl=%u",
! 245: signal_name,
! 246: request?"executing code":"reading data",
! 247: request_info->uri,
! 248: request_info->method,
! 249: request_info->query_string,
! 250: request_info->content_length);
! 251: else
! 252: SAPI::log(SAPI_info, "%s received before or after processing request",
! 253: signal_name);
1.205 paf 254: }
255:
1.201 paf 256: #ifdef SIGUSR1
1.205 paf 257: static void SIGUSR1_handler(int /*sig*/){
258: log_signal("SIGUSR1");
1.201 paf 259: }
260: #endif
261:
262: #ifdef SIGPIPE
1.205 paf 263: static void SIGPIPE_handler(int /*sig*/){
264: log_signal("SIGPIPE");
1.218 ! paf 265: execution_canceled=true;
1.201 paf 266: if(request)
1.218 ! paf 267: request->set_interrupted(true);
1.201 paf 268: }
269: #endif
270:
1.40 paf 271: /**
1.122 parser 272: main workhorse
1.19 paf 273:
1.122 parser 274: @todo
1.40 paf 275: IIS: remove trailing default-document[index.html] from $request.uri.
276: to do that we need to consult metabase,
277: wich is tested but seems slow.
278: */
1.218 ! paf 279: static void real_parser_handler(const char* filespec_to_process,
! 280: const char* request_method, bool header_only) {
1.122 parser 281: // init socks
1.218 ! paf 282: pa_init_socks();
1.122 parser 283:
284: // init global variables
1.218 ! paf 285: pa_globals_init();
1.122 parser 286:
1.186 paf 287: if(!filespec_to_process || !*filespec_to_process)
1.144 paf 288: SAPI::die("Parser/%s", PARSER_VERSION);
1.122 parser 289:
290: // Request info
1.218 ! paf 291: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
1.166 paf 292: char document_root_buf[MAX_STRING];
1.122 parser 293: if(cgi) {
1.218 ! paf 294: if(const char* env_document_root=getenv("DOCUMENT_ROOT"))
1.122 parser 295: request_info.document_root=env_document_root;
1.218 ! paf 296: else if(const char* path_info=getenv("PATH_INFO")) {
1.122 parser 297: // IIS
1.166 paf 298: size_t len=min(sizeof(document_root_buf)-1, strlen(filespec_to_process)-strlen(path_info));
299: memcpy(document_root_buf, filespec_to_process, len); document_root_buf[len]=0;
300: request_info.document_root=document_root_buf;
1.122 parser 301: } else
1.165 paf 302: throw Exception("parser.runtime",
303: 0,
304: "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.122 parser 305: } else {
1.166 paf 306: full_file_spec("", document_root_buf, sizeof(document_root_buf));
307: request_info.document_root=document_root_buf;
1.122 parser 308: }
309: request_info.path_translated=filespec_to_process;
310: request_info.method=request_method ? request_method : "GET";
1.218 ! paf 311: const char* query_string=getenv("QUERY_STRING");
1.122 parser 312: request_info.query_string=query_string;
313: if(cgi) {
1.214 paf 314: // few absolute obligatory
1.218 ! paf 315: const char* path_info=getenv("PATH_INFO");
1.214 paf 316: if(!path_info)
317: SAPI::die("CGI: illegal call (missing PATH_INFO)");
1.218 ! paf 318: const char* script_name=getenv("SCRIPT_NAME");
1.214 paf 319: if(!script_name)
320: SAPI::die("CGI: illegal call (missing SCRIPT_NAME)");
321:
1.218 ! paf 322: const char* env_request_uri=getenv("REQUEST_URI");
1.214 paf 323: if(env_request_uri)
1.122 parser 324: request_info.uri=env_request_uri;
1.214 paf 325: else
1.122 parser 326: if(query_string) {
1.218 ! paf 327: char* reconstructed_uri=new(PointerFreeGC) char[
1.122 parser 328: strlen(path_info)+1/*'?'*/+
1.218 ! paf 329: strlen(query_string)+1/*0*/];
1.122 parser 330: strcpy(reconstructed_uri, path_info);
331: strcat(reconstructed_uri, "?");
332: strcat(reconstructed_uri, query_string);
333: request_info.uri=reconstructed_uri;
334: } else
335: request_info.uri=path_info;
1.214 paf 336:
337: if(env_request_uri) { // apache & others stuck to standards
338: /*
339: http://parser3/env.html?123 =OK
340: $request:uri=/env.html?123
341: REQUEST_URI='/env.html?123'
342: SCRIPT_NAME='/cgi-bin/parser3'
343: PATH_INFO='/env.html'
344:
345: http://parser3/cgi-bin/parser3/env.html?123 =ERROR
346: $request:uri=/cgi-bin/parser3/env.html?123
347: REQUEST_URI='/cgi-bin/parser3/env.html?123'
348: SCRIPT_NAME='/cgi-bin/parser3'
349: PATH_INFO='/env.html'
350: */
351: size_t script_name_len=strlen(script_name);
352: size_t uri_len=strlen(env_request_uri);
353: if(strncmp(env_request_uri, script_name, script_name_len)==0 &&
354: script_name_len != uri_len) // under IIS they are the same
355: SAPI::die("CGI: illegal call (1)");
356: } else { // seen on IIS5
357: /*
358: http://nestle/env.html?123 =OK
359: $request:uri=/env.html?123
360: REQUEST_URI=''
361: SCRIPT_NAME='/env.html'
362: PATH_INFO='/env.html'
363:
364: http://nestle/cgi-bin/parser3.exe/env.html =ERROR
365: $request:uri=/env.html
366: REQUEST_URI=''
367: SCRIPT_NAME='/cgi-bin/parser3.exe'
368: PATH_INFO='/env.html'
369: */
370: if(strcmp(script_name, path_info)!=0)
371: SAPI::die("CGI: illegal call (2)");
372: }
1.122 parser 373: } else
1.177 paf 374: request_info.uri="";
1.122 parser 375:
1.218 ! paf 376: request_info.content_type=getenv("CONTENT_TYPE");
! 377: const char* content_length=getenv("CONTENT_LENGTH");
1.122 parser 378: request_info.content_length=(content_length?atoi(content_length):0);
1.218 ! paf 379: request_info.cookie=getenv("HTTP_COOKIE");
1.184 paf 380: request_info.mail_received=mail_received;
381:
1.218 ! paf 382: // get request_info ptr for signal handlers
! 383: ::request_info=&request_info;
! 384: if(execution_canceled)
! 385: SAPI::die("Execution canceled");
1.198 paf 386:
1.122 parser 387: // prepare to process request
1.218 ! paf 388: Request request(SAPI_info,
1.122 parser 389: request_info,
1.218 ! paf 390: cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS,
1.130 paf 391: true /* status_allowed */);
1.201 paf 392:
393: // get request ptr for signal handlers
394: ::request=&request;
395:
1.181 paf 396: char config_filespec_buf[MAX_STRING];
1.193 paf 397: if(!config_filespec_cstr) {
1.218 ! paf 398: const char* config_by_env=getenv(PARSER_CONFIG_ENV_NAME);
1.193 paf 399: if(!config_by_env)
400: config_by_env=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
401: if(config_by_env)
402: config_filespec_cstr=config_by_env;
403: else {
404: // beside by binary
405: char beside_binary_path[MAX_STRING];
406: strncpy(beside_binary_path, argv0, MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
407: if(!(
408: rsplit(beside_binary_path, '/') ||
409: rsplit(beside_binary_path, '\\'))) { // strip filename
410: // no path, just filename
411: // @todo full path, not ./!
412: beside_binary_path[0]='.'; beside_binary_path[1]=0;
413: }
414: snprintf(config_filespec_buf, MAX_STRING,
415: "%s/%s",
416: beside_binary_path, AUTO_FILE_NAME);
417: config_filespec_cstr=config_filespec_buf;
1.197 paf 418: fail_on_config_read_problem=entry_exists(config_filespec_cstr);
1.193 paf 419: }
1.122 parser 420: }
421:
422: // process the request
423: request.core(
1.193 paf 424: config_filespec_cstr, fail_on_config_read_problem,
1.122 parser 425: header_only);
1.201 paf 426:
427: // no request [prevent signal handlers from accessing invalid memory]
428: ::request=0;
1.122 parser 429:
430: //
1.218 ! paf 431: pa_done_socks();
! 432: }
! 433:
! 434: #if _MSC_VER && !defined(_DEBUG)
! 435: # define PA_USE___TRY
! 436: struct Parser_executed {
! 437: bool with_system_exception;
! 438: LPEXCEPTION_POINTERS system_exception;
! 439: };
1.122 parser 440: #endif
1.160 paf 441:
1.218 ! paf 442: static
! 443: #ifdef PA_USE___TRY
! 444: Parser_executed
! 445: #else
! 446: void
1.160 paf 447: #endif
1.218 ! paf 448: call_real_parser_handler__do_SEH_return_it(
! 449: const char* filespec_to_process,
! 450: const char* request_method, bool header_only) {
! 451: #ifdef PA_USE___TRY
! 452: Parser_executed result={false};
1.122 parser 453: __try {
454: #endif
455: real_parser_handler(
456: filespec_to_process,
457: request_method, header_only);
458:
1.218 ! paf 459: #ifdef PA_USE___TRY
1.122 parser 460: } __except (
1.218 ! paf 461: (result.system_exception=GetExceptionInformation()),
1.122 parser 462: EXCEPTION_EXECUTE_HANDLER) {
1.218 ! paf 463: result.with_system_exception=true;
1.122 parser 464: }
1.218 ! paf 465: return result;
1.122 parser 466: #endif
467: }
468:
1.218 ! paf 469: static void call_real_parser_handler__do_SEH(
! 470: const char* filespec_to_process,
! 471: const char* request_method, bool header_only) {
! 472: #ifdef PA_USE___TRY
! 473: Parser_executed executed=
1.135 paf 474: #endif
1.218 ! paf 475: call_real_parser_handler__do_SEH_return_it(filespec_to_process,
! 476: request_method, header_only);
1.131 paf 477:
1.218 ! paf 478: #ifdef PA_USE___TRY
! 479: if(executed.with_system_exception)
! 480: if(executed.system_exception)
! 481: if(_EXCEPTION_RECORD *er=executed.system_exception->ExceptionRecord)
! 482: throw Exception(0,
! 483: 0,
! 484: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
! 485: else
! 486: throw Exception(0,
! 487: 0,
! 488: "Exception <no exception record>");
! 489: else
! 490: throw Exception(0,
! 491: 0,
! 492: "Exception <no exception information>");
! 493: #endif
1.131 paf 494: }
495:
1.218 ! paf 496: static void usage(const char* program) {
1.188 paf 497: printf(
1.218 ! paf 498: "Parser/%s Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)\n"
1.184 paf 499: "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
500: "\n"
501: "Usage: %s [options] file\n"
502: "Options are:\n"
1.185 paf 503: #ifdef WITH_MAILRECEIVE
1.193 paf 504: " -m Parse mail, put received letter to $mail:received\n"
1.184 paf 505: #endif
1.193 paf 506: " -f config_file Use this config file (/path/to/auto.p)\n"
507: " -h Display usage information (this message)\n"
1.184 paf 508: , PARSER_VERSION,
509: program);
510: exit(EINVAL);
511: }
512:
1.218 ! paf 513: int main(int argc, char *argv[]) {
! 514: GC_java_finalization=0;
1.217 paf 515:
1.218 ! paf 516: #ifndef PA_DEBUG_DISABLE_GC
! 517: // Dont collect unless explicitly requested
! 518: // this is quicker (~30% ), but less memory-efficient(~8%)
! 519: // so deciding for speed
! 520: GC_dont_gc=1;
! 521: #endif
! 522: /*
! 523:
! 524: Array<int> test;
! 525: test+=3;
! 526: test+=4;
! 527: // int a=test.count();
! 528: int i=0;
! 529: scanf("%d", &i);
! 530: int b=test.get(i);
! 531: // int b=test.get(10);
! 532: printf("%d", b);//test.count());*/
1.217 paf 533:
1.203 paf 534: #ifdef SIGUSR1
1.205 paf 535: if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR)
1.203 paf 536: SAPI::die("Can not set handler for SIGUSR1");
537: #endif
538: #ifdef SIGPIPE
1.205 paf 539: if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR)
1.203 paf 540: SAPI::die("Can not set handler for SIGPIPE");
541: #endif
542:
543:
1.185 paf 544: #ifdef DEBUG_MAILRECEIVE
545: if(FILE *fake_in=fopen(DEBUG_MAILRECEIVE, "rt")) {
1.184 paf 546: dup2(fake_in->_file, 0/*STDIN_FILENO*/);
547: }
548: #endif
549:
1.178 paf 550: #ifdef _DEBUG
1.218 ! paf 551: //_crtBreakAlloc=4042;
1.178 paf 552: #endif
1.193 paf 553: argv0=argv[0];
1.45 paf 554:
1.32 paf 555: umask(2);
556:
1.3 paf 557: // were we started as CGI?
1.146 paf 558: cgi=
1.109 parser 559: getenv("SERVER_SOFTWARE") ||
560: getenv("SERVER_NAME") ||
561: getenv("GATEWAY_INTERFACE") ||
562: getenv("REQUEST_METHOD");
1.5 paf 563:
1.184 paf 564: char *raw_filespec_to_process;
1.210 paf 565: if(cgi) {
1.184 paf 566: raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.210 paf 567: if(raw_filespec_to_process && !*raw_filespec_to_process)
568: raw_filespec_to_process=0;
569: } else {
1.184 paf 570: optind = 1;
571: opterr = 0;
572: int c;
1.193 paf 573: while((c = getopt(argc, argv, "hf:"
1.185 paf 574: #ifdef WITH_MAILRECEIVE
1.184 paf 575: "m"
576: #endif
577: )) > 0) {
578: switch (c) {
579: case 'h':
580: usage(argv[0]);
1.193 paf 581: break;
582: case 'f':
583: config_filespec_cstr=optarg;
1.184 paf 584: break;
1.185 paf 585: #ifdef WITH_MAILRECEIVE
1.184 paf 586: case 'm':
587: mail_received=true;
588: break;
589: #endif
590: default:
591: fprintf(stderr, "%s: invalid option '%c'\n", argv[0], optopt);
592: usage(argv[0]);
593: break;
594: }
595: }
596: if (optind != argc - 1) {
597: fprintf(stderr, "%s: file not specified\n", argv[0]);
598: usage(argv[0]);
1.10 paf 599: }
1.184 paf 600:
601: raw_filespec_to_process=argv[optind++];
1.10 paf 602: }
603:
1.100 parser 604: #ifdef WIN32
605: setmode(fileno(stdin), _O_BINARY);
606: setmode(fileno(stdout), _O_BINARY);
607: setmode(fileno(stderr), _O_BINARY);
608: #endif
609:
1.218 ! paf 610: #if defined(_MSC_VER) && defined(_DEBUG)
1.148 paf 611: // Get current flag
612: int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
613:
614: // Turn on leak-checking bit
615: tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
616:
617: // Set flag to the new value
618: _CrtSetDbgFlag( tmpFlag );
1.218 ! paf 619: // _CrtSetBreakAlloc(61);
1.138 paf 620:
1.218 ! paf 621: _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
! 622: _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138 paf 623: #endif
624:
1.166 paf 625: char filespec_to_process[MAX_STRING];
626: full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10 paf 627:
1.218 ! paf 628: const char* request_method=getenv("REQUEST_METHOD");
1.35 paf 629: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131 paf 630:
1.122 parser 631: try { // global try
632: call_real_parser_handler__do_SEH(
633: filespec_to_process,
634: request_method, header_only);
635: } catch(const Exception& e) { // global problem
1.44 paf 636: // don't allocate anything on pool here:
637: // possible pool' exception not catch-ed now
638: // and there could be out-of-memory exception
1.218 ! paf 639: const char* body=e.comment();
! 640: char buf[MAX_STRING];
! 641: snprintf(buf, MAX_STRING, "exception in request exception handler: %s",
! 642: body);
! 643: // log it
! 644: SAPI::log(SAPI_info, "%s", buf);
! 645:
! 646: //
! 647: int content_length=strlen(buf);
! 648:
! 649: // prepare header
! 650: SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
! 651: char content_length_cstr[MAX_NUMBER];
! 652: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
! 653: SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
! 654:
! 655: // send header
! 656: SAPI::send_header(SAPI_info);
! 657:
! 658: // send body
! 659: if(!header_only)
! 660: SAPI::send_body(SAPI_info, buf, content_length);
1.43 paf 661:
1.218 ! paf 662: // unsuccessful finish
1.16 paf 663: }
1.109 parser 664:
1.134 paf 665: return 0;
1.1 paf 666: }
E-mail: