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