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