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