Annotation of parser3/src/targets/cgi/parser3.C, revision 1.295
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.295 ! moko 8: volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.294 2020/10/10 06:08:37 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.68 paf 16: #include "pa_version.h"
1.266 moko 17: #include "pa_vconsole.h"
1.294 moko 18: #include "pa_sapi_info.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.288 moko 29: //#define PA_DEBUG_CGI_ENTRY_EXIT "parser3-debug.log"
1.233 paf 30:
1.278 moko 31: #if defined(_MSC_VER) && !defined(_DEBUG)
1.231 paf 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.293 moko 41: static const char* config_filespec_cstr=0; // -f option
1.294 moko 42: static const char* httpd_port=0; // -p option
1.293 moko 43: static bool mail_received=false; // -m option? [asked to parse incoming message to $mail:received]
1.192 paf 44:
1.245 misha 45: static int args_skip=1;
46: static char** argv_all = NULL;
47:
1.184 paf 48: static bool cgi; ///< we were started as CGI?
1.5 paf 49:
1.201 paf 50: // for signal handlers
51: Request *request=0;
1.218 paf 52: bool execution_canceled=false;
1.201 paf 53:
1.46 paf 54: // SAPI
1.86 parser 55:
1.218 paf 56: static void log(const char* fmt, va_list args) {
1.193 paf 57: bool opened=false;
1.61 paf 58: FILE *f=0;
59:
1.226 paf 60: const char* log_by_env=getenv(PARSER_LOG_ENV_NAME);
61: if(!log_by_env)
62: log_by_env=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME);
63: if(log_by_env) {
64: f=fopen(log_by_env, "at");
65: opened=f!=0;
66: }
1.272 moko 67: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.233 paf 68: f=fopen(PA_DEBUG_CGI_ENTRY_EXIT, "at");
69: opened=f!=0;
70: #endif
1.226 paf 71:
72: if(!opened && config_filespec_cstr) {
1.193 paf 73: char beside_config_path[MAX_STRING];
74: strncpy(beside_config_path, config_filespec_cstr, MAX_STRING-1); beside_config_path[MAX_STRING-1]=0;
1.272 moko 75: if(!(rsplit(beside_config_path, '/') || rsplit(beside_config_path, '\\'))) { // strip filename
1.193 paf 76: // no path, just filename
77: beside_config_path[0]='.'; beside_config_path[1]=0;
78: }
79:
80: char file_spec[MAX_STRING];
1.272 moko 81: snprintf(file_spec, MAX_STRING, "%s/parser3.log", beside_config_path);
1.193 paf 82: f=fopen(file_spec, "at");
83: opened=f!=0;
84: }
1.192 paf 85: // fallback to stderr
1.61 paf 86: if(!opened)
87: f=stderr;
1.208 paf 88:
89: // use no memory [so that we could log out-of-memory error]
90: setbuf(f, 0); // stderr stream is unbuffered by default, but still...
1.61 paf 91:
92: // prefix
93: time_t t=time(0);
1.218 paf 94: if(const char* stamp=ctime(&t)) { // never saw that
1.173 paf 95: if(size_t len=strlen(stamp)) // saw once stamp being =""
1.272 moko 96: fprintf(f, "[%.*s] [%u] ", (int)len-1, stamp, (unsigned int)getpid() );
1.171 paf 97: }
1.61 paf 98: // message
1.117 parser 99:
1.246 misha 100: char buf[MAX_LOG_STRING];
101: size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args);
102: size=remove_crlf(buf, buf+size);
1.239 paf 103: fwrite(buf, size, 1, f);
104:
1.294 moko 105: if(request){
106: Request_info& request_info = request->request_info;
1.261 moko 107: fprintf(f, " [uri=%s, method=%s, cl=%lu]",
1.294 moko 108: request_info.uri ? request_info.uri : "<unknown>",
109: request_info.method ? request_info.method : "<unknown>",
110: request_info.content_length);
111: }
1.239 paf 112: else
113: fputs(" [no request info]", f);
1.117 parser 114:
1.61 paf 115: // newline
1.239 paf 116: fputs("\n", f);
1.61 paf 117:
118: if(opened)
119: fclose(f);
1.85 parser 120: else
121: fflush(f);
1.124 parser 122: }
1.272 moko 123:
1.236 paf 124: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.233 paf 125: static void log(const char* fmt, ...) {
1.272 moko 126: va_list args;
1.233 paf 127: va_start(args,fmt);
128: log(fmt, args);
129: va_end(args);
130: }
1.236 paf 131: #endif
1.124 parser 132:
133: // appends to parser3.log located beside my binary if openable, to stderr otherwize
1.218 paf 134: void SAPI::log(SAPI_Info&, const char* fmt, ...) {
1.272 moko 135: va_list args;
1.124 parser 136: va_start(args,fmt);
137: ::log(fmt, args);
138: va_end(args);
139: }
140:
1.290 moko 141: void SAPI::die(const char* fmt, ...) {
142: va_list args;
143:
144: // logging first, first vsnprintf
145: va_start(args,fmt);
146: ::log(fmt, args);
147: va_end(args);
1.138 paf 148:
1.290 moko 149: // inform user, second vsnprintf
150: va_start(args, fmt);
1.134 paf 151: char body[MAX_STRING];
1.138 paf 152: int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134 paf 153:
1.294 moko 154: sapiInfo->die(body, content_length);
1.290 moko 155: exit(1);
1.258 moko 156: // va_end(args);
1.28 paf 157: }
158:
1.294 moko 159: char* SAPI::Env::get(SAPI_Info& info, const char* name) {
160: return info.get_env(name);
1.218 paf 161: }
162:
1.294 moko 163: const char* const *SAPI::Env::get(SAPI_Info& info) {
164: return info.get_env();
1.180 paf 165: }
166:
1.294 moko 167: size_t SAPI::read_post(SAPI_Info& info, char *buf, size_t max_bytes) {
168: return info.read_post(buf, max_bytes);
1.10 paf 169: }
170:
1.294 moko 171: void SAPI::add_header_attribute(SAPI_Info& info, const char* dont_store_key, const char* dont_store_value) {
172: info.add_header_attribute(dont_store_key, dont_store_value);
1.19 paf 173: }
174:
1.294 moko 175: void SAPI::send_header(SAPI_Info& info) {
176: info.send_header();
1.30 paf 177: }
1.20 paf 178:
1.294 moko 179: size_t SAPI::send_body(SAPI_Info& info, const void *buf, size_t size) {
180: return info.send_body(buf, size);
1.58 paf 181: }
182:
1.218 paf 183: static void full_file_spec(const char* file_name, char *buf, size_t buf_size) {
1.210 paf 184: if(file_name)
1.167 paf 185: if(file_name[0]=='/'
186: #ifdef WIN32
1.210 paf 187: || file_name[0] && file_name[1]==':'
1.167 paf 188: #endif
1.279 moko 189: ){
190: strncpy(buf, file_name, buf_size-1); buf[buf_size-1]=0;
191: } else {
1.261 moko 192: char cwd[MAX_STRING];
193: snprintf(buf, buf_size, "%s/%s", getcwd(cwd, MAX_STRING) ? cwd : "", file_name);
1.167 paf 194: }
195: else
196: buf[0]=0;
1.166 paf 197: #ifdef WIN32
198: back_slashes_to_slashes(buf);
199: #endif
1.97 parser 200: }
201:
1.218 paf 202: static void log_signal(const char* signal_name) {
1.294 moko 203: SAPI::log(*sapiInfo, "%s received %s processing request", signal_name, request ? "while" : "before or after");
1.205 paf 204: }
205:
1.201 paf 206: #ifdef SIGUSR1
1.205 paf 207: static void SIGUSR1_handler(int /*sig*/){
208: log_signal("SIGUSR1");
1.201 paf 209: }
210: #endif
211:
212: #ifdef SIGPIPE
1.243 misha 213: #define SIGPIPE_NAME "SIGPIPE"
214: static const String sigpipe_name(SIGPIPE_NAME);
1.205 paf 215: static void SIGPIPE_handler(int /*sig*/){
1.243 misha 216: Value* sigpipe=0;
217: if(request)
1.251 misha 218: sigpipe=request->main_class.get_element(sigpipe_name);
1.243 misha 219: if(sigpipe && sigpipe->as_bool())
1.244 misha 220: log_signal(SIGPIPE_NAME);
1.243 misha 221:
1.218 paf 222: execution_canceled=true;
1.201 paf 223: if(request)
1.276 moko 224: request->set_skip(Request::SKIP_INTERRUPTED);
1.201 paf 225: }
226: #endif
227:
1.227 paf 228: #ifdef WIN32
1.292 moko 229: const char* maybe_reconstruct_IIS_status_in_qs(const char* original) {
1.228 paf 230: // 404;http://servername/page[?param=value...]
1.227 paf 231: // ';' should be urlencoded by HTTP standard, so we shouldn't get it from browser
232: // and can consider that as an indication that this is IIS way to report errors
233:
1.272 moko 234: if(original && isdigit((unsigned char)original[0]) && isdigit((unsigned char)original[1]) && isdigit((unsigned char)original[2]) && original[3]==';'){
1.227 paf 235: size_t original_len=strlen(original);
1.272 moko 236: char* reconstructed=new(PointerFreeGC) char[original_len +12/*IIS-STATUS=&*/ +14/*IIS-DOCUMENT=&*/ +1];
1.227 paf 237: char* cur=reconstructed;
238: memcpy(cur, "IIS-STATUS=", 11); cur+=11;
239: memcpy(cur, original, 3); cur+=3;
240: *cur++='&';
241:
1.228 paf 242: const char* qmark_at=strchr(original, '?');
1.227 paf 243: memcpy(cur, "IIS-DOCUMENT=", 13); cur+=13;
244: {
1.272 moko 245: size_t value_len=(qmark_at ? qmark_at-original : original_len)-4;
1.227 paf 246: memcpy(cur, original+4, value_len); cur+=value_len;
247: }
248:
249: if(qmark_at) {
250: *cur++='&';
251: strcpy(cur, qmark_at+1/*skip ? itself*/);
252: } else
253: *cur=0;
254:
255: return reconstructed;
256: }
257:
258: return original;
259: }
1.283 moko 260:
261: #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) maybe_reconstruct_IIS_status_in_qs(s)
262: #else
263: #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) s
1.227 paf 264: #endif
265:
1.256 misha 266:
267: class RequestController {
268: public:
269: RequestController(Request* r){
270: ::request=r;
271: }
272: ~RequestController(){
273: ::request=0;
274: }
275: };
276:
1.288 moko 277: static bool locate_config(){
278: if(!config_filespec_cstr) {
279: config_filespec_cstr=getenv(PARSER_CONFIG_ENV_NAME);
280: if(!config_filespec_cstr)
281: config_filespec_cstr=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
282: if(!config_filespec_cstr){
283: // beside by binary
284: char beside_binary_path[MAX_STRING];
285: strncpy(beside_binary_path, argv_all[0], MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
286: if(!(rsplit(beside_binary_path, '/') || rsplit(beside_binary_path, '\\'))) { // strip filename
287: // no path, just filename
288: // @todo full path, not ./!
289: beside_binary_path[0]='.'; beside_binary_path[1]=0;
290: }
291: char config_filespec_buf[MAX_STRING];
292: snprintf(config_filespec_buf, MAX_STRING, "%s/%s", beside_binary_path, AUTO_FILE_NAME);
293: config_filespec_cstr=pa_strdup(config_filespec_buf);
294: return entry_exists(config_filespec_cstr);
295: }
296: }
297: return true;
298: }
1.256 misha 299:
1.294 moko 300: static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection, const char* filespec_to_process){
301: connection.read_header();
1.295 ! moko 302: info.populate_env();
1.294 moko 303:
304: // Request info
305: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
306:
307: char document_root_buf[MAX_STRING];
308: full_file_spec("", document_root_buf, sizeof(document_root_buf));
309: request_info.document_root = document_root_buf;
310: request_info.path_translated = filespec_to_process;
311: request_info.method = connection.method();
1.295 ! moko 312: request_info.query_string = connection.query();
! 313: request_info.uri = request_info.strip_absolute_uri(connection.uri());
! 314: request_info.content_type = connection.content_type();
! 315: request_info.content_length = connection.content_length();
! 316: request_info.cookie = info.get_env("HTTP_COOKIE");
! 317: request_info.mail_received = false;
1.294 moko 318: request_info.argv = argv_all + args_skip;
319:
320: // prepare to process request
321: Request request(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
322: {
323: // get ::request ptr for signal handlers
324: RequestController rc(&request);
325: bool fail_on_config_read_problem=locate_config();
326: // process the request
327: request.core(config_filespec_cstr, fail_on_config_read_problem, strcasecmp(request_info.method, "HEAD")==0);
328: // ::request cleared in RequestController desctructor to prevent signal handlers from accessing invalid memory
329: }
330: }
331:
332: static void httpd_mode(const char* filespec_to_process){
333: int sock = HTTPD_Server::bind(/*"127.0.0.1"*/ NULL, pa_atoui(httpd_port, 10));
334:
335: while(1 == 1){
336: HTTPD_Connection *connection = HTTPD_Server::accept(sock,5);
337: if(!connection)
338: continue;
339:
340: SAPI_Info_HTTPD info(*connection);
341:
342: try { // connection try
343: connection_handler(info, *connection, filespec_to_process);
344: } catch(const Exception& e) { // exception in unhandled exception
345: // info.die(e);
346: }
1.19 paf 347:
1.294 moko 348: close(connection->sock);
349: }
350: }
1.231 paf 351:
1.294 moko 352: /** main workhorse */
353:
354: static void real_parser_handler(const char* filespec_to_process) {
1.280 moko 355: // init libraries
1.218 paf 356: pa_globals_init();
1.294 moko 357:
358: if(httpd_port){
359: httpd_mode(filespec_to_process);
360: }
361:
362: const char* request_method=getenv("REQUEST_METHOD");
363:
1.186 paf 364: if(!filespec_to_process || !*filespec_to_process)
1.282 moko 365: SAPI::die("Parser/%s", PARSER_VERSION);
1.122 parser 366:
367: // Request info
1.285 moko 368: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
1.166 paf 369: char document_root_buf[MAX_STRING];
1.283 moko 370:
371: request_info.path_translated = filespec_to_process;
372: request_info.method = request_method ? request_method : "GET";
373: request_info.query_string = MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(getenv("QUERY_STRING"));
374:
1.122 parser 375: if(cgi) {
1.284 moko 376: // obligatory
1.218 paf 377: const char* path_info=getenv("PATH_INFO");
1.214 paf 378: if(!path_info)
379: SAPI::die("CGI: illegal call (missing PATH_INFO)");
1.283 moko 380:
381: request_info.document_root = getenv("DOCUMENT_ROOT");
382: if(!request_info.document_root) {
1.285 moko 383: // IIS or fcgiwrap minimalistic setup
384: ssize_t prefix_len = strlen(filespec_to_process) - strlen(path_info);
385: if(prefix_len < 0 || strcmp(filespec_to_process + prefix_len, path_info) != 0)
386: SAPI::die("CGI: illegal call (invalid PATH_INFO in reinventing DOCUMENT_ROOT)");
387:
388: char* document_root = new(PointerFreeGC) char[prefix_len + 1/*0*/];
389: memcpy(document_root, filespec_to_process, prefix_len); document_root[prefix_len] = 0;
390: request_info.document_root = document_root;
1.283 moko 391: }
1.214 paf 392:
1.285 moko 393: request_info.uri = request_info.strip_absolute_uri(getenv("REQUEST_URI"));
1.283 moko 394: if(request_info.uri) { // apache & others stuck to standards
1.284 moko 395: // another obligatory
1.285 moko 396: const char* script_name = getenv("SCRIPT_NAME");
1.284 moko 397: if(!script_name)
398: SAPI::die("CGI: illegal call (missing SCRIPT_NAME)");
1.214 paf 399: /*
400: http://parser3/env.html?123 =OK
401: $request:uri=/env.html?123
402: REQUEST_URI='/env.html?123'
403: SCRIPT_NAME='/cgi-bin/parser3'
404: PATH_INFO='/env.html'
1.285 moko 405:
1.214 paf 406: http://parser3/cgi-bin/parser3/env.html?123 =ERROR
407: $request:uri=/cgi-bin/parser3/env.html?123
408: REQUEST_URI='/cgi-bin/parser3/env.html?123'
409: SCRIPT_NAME='/cgi-bin/parser3'
410: PATH_INFO='/env.html'
411: */
1.285 moko 412: size_t script_name_len = strlen(script_name);
413: size_t uri_len = strlen(request_info.uri);
1.283 moko 414: if(strncmp(request_info.uri, script_name, script_name_len)==0 && script_name_len != uri_len) // under IIS they are the same
1.214 paf 415: SAPI::die("CGI: illegal call (1)");
1.284 moko 416: } else { // fcgiwrap minimalistic setup
1.283 moko 417:
1.286 moko 418: if(request_info.query_string && *request_info.query_string) {
1.285 moko 419: char* reconstructed_uri = new(PointerFreeGC) char[strlen(path_info) + 1/*'?'*/+ strlen(request_info.query_string) + 1/*0*/];
1.283 moko 420: strcpy(reconstructed_uri, path_info);
421: strcat(reconstructed_uri, "?");
422: strcat(reconstructed_uri, request_info.query_string);
1.285 moko 423: request_info.uri = reconstructed_uri;
1.283 moko 424: } else
1.285 moko 425: request_info.uri = path_info;
1.214 paf 426: }
1.283 moko 427: } else{
428: full_file_spec("", document_root_buf, sizeof(document_root_buf));
1.285 moko 429: request_info.document_root = document_root_buf;
430: request_info.uri = "";
1.283 moko 431: }
1.122 parser 432:
1.283 moko 433: request_info.content_type = getenv("CONTENT_TYPE");
1.294 moko 434: request_info.content_length = pa_atoui(getenv("CONTENT_LENGTH"), 10);
1.283 moko 435: request_info.cookie = getenv("HTTP_COOKIE");
436: request_info.mail_received = mail_received;
1.184 paf 437:
1.294 moko 438: request_info.argv = argv_all + args_skip;
1.245 misha 439:
1.218 paf 440: if(execution_canceled)
441: SAPI::die("Execution canceled");
1.198 paf 442:
1.233 paf 443: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
444: log("request_info: method=%s, uri=%s, q=%s, dr=%s, pt=%s, cookies=%s, cl=%u",
445: request_info.method,
446: request_info.uri,
447: request_info.query_string,
448: request_info.document_root,
449: request_info.path_translated,
450: request_info.cookie,
451: request_info.content_length);
452: #endif
453:
1.122 parser 454: // prepare to process request
1.294 moko 455: Request request(*sapiInfo, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS);
1.256 misha 456: {
457: // get ::request ptr for signal handlers
458: RequestController rc(&request);
1.288 moko 459: bool fail_on_config_read_problem=locate_config();
1.256 misha 460: // process the request
1.294 moko 461: request.core(config_filespec_cstr, fail_on_config_read_problem, strcasecmp(request_info.method, "HEAD")==0);
1.256 misha 462: // ::request cleared in RequestController desctructor to prevent signal handlers from accessing invalid memory
1.122 parser 463: }
1.231 paf 464:
1.280 moko 465: // finalize libraries
1.225 paf 466: pa_globals_done();
1.218 paf 467: }
468:
1.231 paf 469: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
1.294 moko 470: static const Exception call_real_parser_handler__do_PEH_return_it(const char* filespec_to_process) {
1.231 paf 471: try {
1.294 moko 472: real_parser_handler(filespec_to_process);
1.231 paf 473: } catch(const Exception& e) {
474: return e;
1.122 parser 475: }
1.231 paf 476:
477: return Exception();
1.122 parser 478: }
1.272 moko 479:
1.294 moko 480: static void call_real_parser_handler__supress_system_exception(const char* filespec_to_process) {
1.231 paf 481: Exception parser_exception;
482: LPEXCEPTION_POINTERS system_exception=0;
1.122 parser 483:
1.231 paf 484: __try {
1.294 moko 485: parser_exception=call_real_parser_handler__do_PEH_return_it(filespec_to_process);
1.288 moko 486: } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
1.231 paf 487: if(system_exception)
488: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.272 moko 489: throw Exception("system", 0, "0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
1.218 paf 490: else
1.272 moko 491: throw Exception("system", 0, "<no exception record>");
1.218 paf 492: else
1.272 moko 493: throw Exception("system", 0, "<no exception information>");
1.231 paf 494: }
495:
496: if(parser_exception)
497: throw Exception(parser_exception);
498: }
1.273 moko 499:
500: #define REAL_PARSER_HANDLER call_real_parser_handler__supress_system_exception
501: #else
502: #define REAL_PARSER_HANDLER real_parser_handler
1.218 paf 503: #endif
1.131 paf 504:
1.218 paf 505: static void usage(const char* program) {
1.188 paf 506: printf(
1.235 paf 507: "Parser/%s\n"
1.277 moko 508: "Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)\n"
1.184 paf 509: "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
510: "\n"
511: "Usage: %s [options] file\n"
512: "Options are:\n"
1.185 paf 513: #ifdef WITH_MAILRECEIVE
1.193 paf 514: " -m Parse mail, put received letter to $mail:received\n"
1.184 paf 515: #endif
1.193 paf 516: " -f config_file Use this config file (/path/to/auto.p)\n"
1.294 moko 517: " -p port Start web server on this port\n"
1.272 moko 518: " -h Display usage information (this message)\n",
519: PARSER_VERSION,
1.184 paf 520: program);
521: exit(EINVAL);
522: }
523:
1.294 moko 524:
1.249 misha 525: int main(int argc, char *argv[]) {
1.233 paf 526: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
527: log("main: entry");
528: #endif
1.217 paf 529:
1.294 moko 530: argv_all=argv;
531: umask(2);
532:
533: // were we started as CGI?
534: cgi=(getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) && !getenv("PARSER_VERSION");
535: sapiInfo = cgi ? new SAPI_Info_CGI() : new SAPI_Info();
536:
1.203 paf 537: #ifdef SIGUSR1
1.205 paf 538: if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR)
1.203 paf 539: SAPI::die("Can not set handler for SIGUSR1");
540: #endif
541: #ifdef SIGPIPE
1.205 paf 542: if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR)
1.203 paf 543: SAPI::die("Can not set handler for SIGPIPE");
544: #endif
545:
1.184 paf 546: char *raw_filespec_to_process;
1.210 paf 547: if(cgi) {
1.184 paf 548: raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.210 paf 549: if(raw_filespec_to_process && !*raw_filespec_to_process)
550: raw_filespec_to_process=0;
551: } else {
1.250 misha 552: int optind=1;
1.245 misha 553: while(optind < argc){
554: char *carg = argv[optind];
555: if(carg[0] != '-')
1.184 paf 556: break;
1.245 misha 557:
558: for(size_t k = 1; k < strlen(carg); k++){
559: char c = carg[k];
560: switch (c) {
561: case 'h':
562: usage(argv[0]);
563: break;
564: case 'f':
565: if(optind < argc - 1){
566: optind++;
567: config_filespec_cstr=argv[optind];
568: }
569: break;
1.294 moko 570: case 'p':
571: if(optind < argc - 1){
572: optind++;
573: httpd_port=argv[optind];
574: }
575: break;
1.185 paf 576: #ifdef WITH_MAILRECEIVE
1.245 misha 577: case 'm':
578: mail_received=true;
579: break;
580: #endif
581: default:
582: fprintf(stderr, "%s: invalid option '%c'\n", argv[0], c);
583: usage(argv[0]);
584: break;
585: }
1.184 paf 586: }
1.245 misha 587: optind++;
1.184 paf 588: }
1.245 misha 589:
590: if (optind > argc - 1) {
1.294 moko 591: if(!httpd_port) {
592: fprintf(stderr, "%s: file not specified\n", argv[0]);
593: usage(argv[0]);
594: }
595: } else {
596: raw_filespec_to_process=argv[optind];
1.10 paf 597: }
1.245 misha 598: args_skip=optind;
1.294 moko 599:
600: if (httpd_port && mail_received) {
601: fprintf(stderr, "%s: -p and -m options should not be used together\n", argv[0]);
602: usage(argv[0]);
603: }
1.10 paf 604: }
605:
1.264 moko 606: #ifdef _MSC_VER
1.100 parser 607: setmode(fileno(stdin), _O_BINARY);
608: setmode(fileno(stdout), _O_BINARY);
609: setmode(fileno(stderr), _O_BINARY);
610: #endif
611:
1.218 paf 612: #if defined(_MSC_VER) && defined(_DEBUG)
1.148 paf 613: // Get current flag
614: int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
615:
616: // Turn on leak-checking bit
617: tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
618:
619: // Set flag to the new value
620: _CrtSetDbgFlag( tmpFlag );
1.138 paf 621:
1.218 paf 622: _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
623: _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138 paf 624: #endif
625:
1.166 paf 626: char filespec_to_process[MAX_STRING];
627: full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10 paf 628:
1.122 parser 629: try { // global try
1.294 moko 630: REAL_PARSER_HANDLER(filespec_to_process);
1.292 moko 631: } catch(const Exception& e) { // exception in unhandled exception
632: SAPI::die("Unhandled exception %s", e.comment());
1.16 paf 633: }
1.109 parser 634:
1.233 paf 635: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
636: log("main: successful return");
637: #endif
1.294 moko 638: return sapiInfo && sapiInfo->http_response_code < 100 ? sapiInfo->http_response_code : 0;
1.1 paf 639: }
E-mail: