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