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