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