Annotation of parser3/src/targets/cgi/parser3.C, revision 1.294
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.294 ! moko 8: volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.293 2020/10/03 22:58:07 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();
! 302:
! 303: // Request info
! 304: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
! 305:
! 306: char document_root_buf[MAX_STRING];
! 307: full_file_spec("", document_root_buf, sizeof(document_root_buf));
! 308: request_info.document_root = document_root_buf;
! 309: request_info.path_translated = filespec_to_process;
! 310: request_info.method = connection.method();
! 311: request_info.query_string=NULL;
! 312: request_info.uri=request_info.strip_absolute_uri(connection.uri());
! 313: request_info.content_type=connection.content_type();
! 314: request_info.content_length=connection.content_length();
! 315: request_info.cookie=info.get_env("HTTP_COOKIE");
! 316: request_info.mail_received=false;
! 317: request_info.argv = argv_all + args_skip;
! 318:
! 319: // prepare to process request
! 320: Request request(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
! 321: {
! 322: // get ::request ptr for signal handlers
! 323: RequestController rc(&request);
! 324: bool fail_on_config_read_problem=locate_config();
! 325: // process the request
! 326: request.core(config_filespec_cstr, fail_on_config_read_problem, strcasecmp(request_info.method, "HEAD")==0);
! 327: // ::request cleared in RequestController desctructor to prevent signal handlers from accessing invalid memory
! 328: }
! 329: }
! 330:
! 331: static void httpd_mode(const char* filespec_to_process){
! 332: int sock = HTTPD_Server::bind(/*"127.0.0.1"*/ NULL, pa_atoui(httpd_port, 10));
! 333:
! 334: while(1 == 1){
! 335: HTTPD_Connection *connection = HTTPD_Server::accept(sock,5);
! 336: if(!connection)
! 337: continue;
! 338:
! 339: SAPI_Info_HTTPD info(*connection);
! 340:
! 341: try { // connection try
! 342: connection_handler(info, *connection, filespec_to_process);
! 343: } catch(const Exception& e) { // exception in unhandled exception
! 344: // info.die(e);
! 345: }
1.19 paf 346:
1.294 ! moko 347: close(connection->sock);
! 348: }
! 349: }
1.231 paf 350:
1.294 ! moko 351: /** main workhorse */
! 352:
! 353: static void real_parser_handler(const char* filespec_to_process) {
1.280 moko 354: // init libraries
1.218 paf 355: pa_globals_init();
1.294 ! moko 356:
! 357: if(httpd_port){
! 358: httpd_mode(filespec_to_process);
! 359: }
! 360:
! 361: const char* request_method=getenv("REQUEST_METHOD");
! 362:
1.186 paf 363: if(!filespec_to_process || !*filespec_to_process)
1.282 moko 364: SAPI::die("Parser/%s", PARSER_VERSION);
1.122 parser 365:
366: // Request info
1.285 moko 367: Request_info request_info; memset(&request_info, 0, sizeof(request_info));
1.166 paf 368: char document_root_buf[MAX_STRING];
1.283 moko 369:
370: request_info.path_translated = filespec_to_process;
371: request_info.method = request_method ? request_method : "GET";
372: request_info.query_string = MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(getenv("QUERY_STRING"));
373:
1.122 parser 374: if(cgi) {
1.284 moko 375: // obligatory
1.218 paf 376: const char* path_info=getenv("PATH_INFO");
1.214 paf 377: if(!path_info)
378: SAPI::die("CGI: illegal call (missing PATH_INFO)");
1.283 moko 379:
380: request_info.document_root = getenv("DOCUMENT_ROOT");
381: if(!request_info.document_root) {
1.285 moko 382: // IIS or fcgiwrap minimalistic setup
383: ssize_t prefix_len = strlen(filespec_to_process) - strlen(path_info);
384: if(prefix_len < 0 || strcmp(filespec_to_process + prefix_len, path_info) != 0)
385: SAPI::die("CGI: illegal call (invalid PATH_INFO in reinventing DOCUMENT_ROOT)");
386:
387: char* document_root = new(PointerFreeGC) char[prefix_len + 1/*0*/];
388: memcpy(document_root, filespec_to_process, prefix_len); document_root[prefix_len] = 0;
389: request_info.document_root = document_root;
1.283 moko 390: }
1.214 paf 391:
1.285 moko 392: request_info.uri = request_info.strip_absolute_uri(getenv("REQUEST_URI"));
1.283 moko 393: if(request_info.uri) { // apache & others stuck to standards
1.284 moko 394: // another obligatory
1.285 moko 395: const char* script_name = getenv("SCRIPT_NAME");
1.284 moko 396: if(!script_name)
397: SAPI::die("CGI: illegal call (missing SCRIPT_NAME)");
1.214 paf 398: /*
399: http://parser3/env.html?123 =OK
400: $request:uri=/env.html?123
401: REQUEST_URI='/env.html?123'
402: SCRIPT_NAME='/cgi-bin/parser3'
403: PATH_INFO='/env.html'
1.285 moko 404:
1.214 paf 405: http://parser3/cgi-bin/parser3/env.html?123 =ERROR
406: $request:uri=/cgi-bin/parser3/env.html?123
407: REQUEST_URI='/cgi-bin/parser3/env.html?123'
408: SCRIPT_NAME='/cgi-bin/parser3'
409: PATH_INFO='/env.html'
410: */
1.285 moko 411: size_t script_name_len = strlen(script_name);
412: size_t uri_len = strlen(request_info.uri);
1.283 moko 413: 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 414: SAPI::die("CGI: illegal call (1)");
1.284 moko 415: } else { // fcgiwrap minimalistic setup
1.283 moko 416:
1.286 moko 417: if(request_info.query_string && *request_info.query_string) {
1.285 moko 418: char* reconstructed_uri = new(PointerFreeGC) char[strlen(path_info) + 1/*'?'*/+ strlen(request_info.query_string) + 1/*0*/];
1.283 moko 419: strcpy(reconstructed_uri, path_info);
420: strcat(reconstructed_uri, "?");
421: strcat(reconstructed_uri, request_info.query_string);
1.285 moko 422: request_info.uri = reconstructed_uri;
1.283 moko 423: } else
1.285 moko 424: request_info.uri = path_info;
1.214 paf 425: }
1.283 moko 426: } else{
427: full_file_spec("", document_root_buf, sizeof(document_root_buf));
1.285 moko 428: request_info.document_root = document_root_buf;
429: request_info.uri = "";
1.283 moko 430: }
1.122 parser 431:
1.283 moko 432: request_info.content_type = getenv("CONTENT_TYPE");
1.294 ! moko 433: request_info.content_length = pa_atoui(getenv("CONTENT_LENGTH"), 10);
1.283 moko 434: request_info.cookie = getenv("HTTP_COOKIE");
435: request_info.mail_received = mail_received;
1.184 paf 436:
1.294 ! moko 437: request_info.argv = argv_all + args_skip;
1.245 misha 438:
1.218 paf 439: if(execution_canceled)
440: SAPI::die("Execution canceled");
1.198 paf 441:
1.233 paf 442: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
443: log("request_info: method=%s, uri=%s, q=%s, dr=%s, pt=%s, cookies=%s, cl=%u",
444: request_info.method,
445: request_info.uri,
446: request_info.query_string,
447: request_info.document_root,
448: request_info.path_translated,
449: request_info.cookie,
450: request_info.content_length);
451: #endif
452:
1.122 parser 453: // prepare to process request
1.294 ! moko 454: Request request(*sapiInfo, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS);
1.256 misha 455: {
456: // get ::request ptr for signal handlers
457: RequestController rc(&request);
1.288 moko 458: bool fail_on_config_read_problem=locate_config();
1.256 misha 459: // process the request
1.294 ! moko 460: request.core(config_filespec_cstr, fail_on_config_read_problem, strcasecmp(request_info.method, "HEAD")==0);
1.256 misha 461: // ::request cleared in RequestController desctructor to prevent signal handlers from accessing invalid memory
1.122 parser 462: }
1.231 paf 463:
1.280 moko 464: // finalize libraries
1.225 paf 465: pa_globals_done();
1.218 paf 466: }
467:
1.231 paf 468: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
1.294 ! moko 469: static const Exception call_real_parser_handler__do_PEH_return_it(const char* filespec_to_process) {
1.231 paf 470: try {
1.294 ! moko 471: real_parser_handler(filespec_to_process);
1.231 paf 472: } catch(const Exception& e) {
473: return e;
1.122 parser 474: }
1.231 paf 475:
476: return Exception();
1.122 parser 477: }
1.272 moko 478:
1.294 ! moko 479: static void call_real_parser_handler__supress_system_exception(const char* filespec_to_process) {
1.231 paf 480: Exception parser_exception;
481: LPEXCEPTION_POINTERS system_exception=0;
1.122 parser 482:
1.231 paf 483: __try {
1.294 ! moko 484: parser_exception=call_real_parser_handler__do_PEH_return_it(filespec_to_process);
1.288 moko 485: } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
1.231 paf 486: if(system_exception)
487: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.272 moko 488: throw Exception("system", 0, "0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
1.218 paf 489: else
1.272 moko 490: throw Exception("system", 0, "<no exception record>");
1.218 paf 491: else
1.272 moko 492: throw Exception("system", 0, "<no exception information>");
1.231 paf 493: }
494:
495: if(parser_exception)
496: throw Exception(parser_exception);
497: }
1.273 moko 498:
499: #define REAL_PARSER_HANDLER call_real_parser_handler__supress_system_exception
500: #else
501: #define REAL_PARSER_HANDLER real_parser_handler
1.218 paf 502: #endif
1.131 paf 503:
1.218 paf 504: static void usage(const char* program) {
1.188 paf 505: printf(
1.235 paf 506: "Parser/%s\n"
1.277 moko 507: "Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)\n"
1.184 paf 508: "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
509: "\n"
510: "Usage: %s [options] file\n"
511: "Options are:\n"
1.185 paf 512: #ifdef WITH_MAILRECEIVE
1.193 paf 513: " -m Parse mail, put received letter to $mail:received\n"
1.184 paf 514: #endif
1.193 paf 515: " -f config_file Use this config file (/path/to/auto.p)\n"
1.294 ! moko 516: " -p port Start web server on this port\n"
1.272 moko 517: " -h Display usage information (this message)\n",
518: PARSER_VERSION,
1.184 paf 519: program);
520: exit(EINVAL);
521: }
522:
1.294 ! moko 523:
1.249 misha 524: int main(int argc, char *argv[]) {
1.233 paf 525: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
526: log("main: entry");
527: #endif
1.217 paf 528:
1.294 ! moko 529: argv_all=argv;
! 530: umask(2);
! 531:
! 532: // were we started as CGI?
! 533: cgi=(getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) && !getenv("PARSER_VERSION");
! 534: sapiInfo = cgi ? new SAPI_Info_CGI() : new SAPI_Info();
! 535:
1.203 paf 536: #ifdef SIGUSR1
1.205 paf 537: if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR)
1.203 paf 538: SAPI::die("Can not set handler for SIGUSR1");
539: #endif
540: #ifdef SIGPIPE
1.205 paf 541: if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR)
1.203 paf 542: SAPI::die("Can not set handler for SIGPIPE");
543: #endif
544:
1.184 paf 545: char *raw_filespec_to_process;
1.210 paf 546: if(cgi) {
1.184 paf 547: raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.210 paf 548: if(raw_filespec_to_process && !*raw_filespec_to_process)
549: raw_filespec_to_process=0;
550: } else {
1.250 misha 551: int optind=1;
1.245 misha 552: while(optind < argc){
553: char *carg = argv[optind];
554: if(carg[0] != '-')
1.184 paf 555: break;
1.245 misha 556:
557: for(size_t k = 1; k < strlen(carg); k++){
558: char c = carg[k];
559: switch (c) {
560: case 'h':
561: usage(argv[0]);
562: break;
563: case 'f':
564: if(optind < argc - 1){
565: optind++;
566: config_filespec_cstr=argv[optind];
567: }
568: break;
1.294 ! moko 569: case 'p':
! 570: if(optind < argc - 1){
! 571: optind++;
! 572: httpd_port=argv[optind];
! 573: }
! 574: break;
1.185 paf 575: #ifdef WITH_MAILRECEIVE
1.245 misha 576: case 'm':
577: mail_received=true;
578: break;
579: #endif
580: default:
581: fprintf(stderr, "%s: invalid option '%c'\n", argv[0], c);
582: usage(argv[0]);
583: break;
584: }
1.184 paf 585: }
1.245 misha 586: optind++;
1.184 paf 587: }
1.245 misha 588:
589: if (optind > argc - 1) {
1.294 ! moko 590: if(!httpd_port) {
! 591: fprintf(stderr, "%s: file not specified\n", argv[0]);
! 592: usage(argv[0]);
! 593: }
! 594: } else {
! 595: raw_filespec_to_process=argv[optind];
1.10 paf 596: }
1.245 misha 597: args_skip=optind;
1.294 ! moko 598:
! 599: if (httpd_port && mail_received) {
! 600: fprintf(stderr, "%s: -p and -m options should not be used together\n", argv[0]);
! 601: usage(argv[0]);
! 602: }
1.10 paf 603: }
604:
1.264 moko 605: #ifdef _MSC_VER
1.100 parser 606: setmode(fileno(stdin), _O_BINARY);
607: setmode(fileno(stdout), _O_BINARY);
608: setmode(fileno(stderr), _O_BINARY);
609: #endif
610:
1.218 paf 611: #if defined(_MSC_VER) && defined(_DEBUG)
1.148 paf 612: // Get current flag
613: int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
614:
615: // Turn on leak-checking bit
616: tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
617:
618: // Set flag to the new value
619: _CrtSetDbgFlag( tmpFlag );
1.138 paf 620:
1.218 paf 621: _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
622: _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138 paf 623: #endif
624:
1.166 paf 625: char filespec_to_process[MAX_STRING];
626: full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10 paf 627:
1.122 parser 628: try { // global try
1.294 ! moko 629: REAL_PARSER_HANDLER(filespec_to_process);
1.292 moko 630: } catch(const Exception& e) { // exception in unhandled exception
631: SAPI::die("Unhandled exception %s", e.comment());
1.16 paf 632: }
1.109 parser 633:
1.233 paf 634: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
635: log("main: successful return");
636: #endif
1.294 ! moko 637: return sapiInfo && sapiInfo->http_response_code < 100 ? sapiInfo->http_response_code : 0;
1.1 paf 638: }
E-mail: