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