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