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