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