Annotation of parser3/src/targets/cgi/parser3.C, revision 1.377
1.27 paf 1: /** @file
2: Parser: scripting and CGI main.
3:
1.368 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.350 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.189 paf 6: */
1.27 paf 7:
1.377 ! moko 8: volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.376 2026/10/07 19:37:27 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.375 moko 20: #include "pa_code_cache.h"
1.207 paf 21:
1.263 moko 22: #ifdef _MSC_VER
1.264 moko 23: #include <crtdbg.h>
1.263 moko 24: #include <windows.h>
25: #include <direct.h>
1.328 moko 26:
27: extern "C" HANDLE WINAPI GC_CreateThread(LPSECURITY_ATTRIBUTES, DWORD, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD);
28:
29: #else
30:
31: extern "C" int GC_pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void * /* arg */);
32:
1.120 parser 33: #endif
34:
1.232 paf 35: // defines
1.231 paf 36:
1.233 paf 37: // comment remove me after debugging
1.362 moko 38: //#define PA_DEBUG_CGI_ENTRY_EXIT
1.233 paf 39:
1.278 moko 40: #if defined(_MSC_VER) && !defined(_DEBUG)
1.231 paf 41: # define PA_SUPPRESS_SYSTEM_EXCEPTION
42: #endif
43:
1.109 parser 44: // consts
1.84 parser 45:
1.175 paf 46: #define REDIRECT_PREFIX "REDIRECT_"
1.181 paf 47: #define PARSER_CONFIG_ENV_NAME "CGI_PARSER_CONFIG"
1.226 paf 48: #define PARSER_LOG_ENV_NAME "CGI_PARSER_LOG"
1.159 paf 49:
1.358 moko 50: SAPI_Info sapi_console;
51: SAPI_Info_CGI sapi_cgi;
52:
53: static SAPI_Info *sapi_info = &sapi_cgi;
54: static THREAD_LOCAL SAPI_Info *sapi_info_4log = NULL; // global for correct send error in die()
1.357 moko 55:
1.365 moko 56: const char* parser3_mode = "cgi"; // $status:mode
1.316 moko 57: static const char* filespec_to_process = 0; // [file]
58: static const char* httpd_host_port = 0; // -p option
1.319 moko 59: static const char* config_filespec = 0; // -f option or from env or next to the executable if exists
1.374 moko 60: static const char* log_filespec = 0; // -l option or from env, full disk path
1.316 moko 61: static bool mail_received = false; // -m option? [asked to parse incoming message to $mail:received]
1.347 moko 62: static const char* parser3_filespec = 0; // argv[0]
1.319 moko 63: static char** argv_extra = NULL;
1.245 misha 64:
1.317 moko 65: // for error logging
1.341 moko 66: static THREAD_LOCAL Request_info *request_info_4log = NULL; // global for correct log() reporting
1.374 moko 67: static const char* filespec_4log = NULL; // null only if system-wide auto.p used; full disk path, parser3.log goes next to it
1.201 paf 68:
1.366 moko 69: const char *parser3_log_filespec() { // $status:log-filename
1.363 moko 70: const char* slog=log_filespec;
1.61 paf 71:
1.366 moko 72: if(!slog) {
73: static char log_spec[MAX_STRING + 12 /* '/parser3.log' */];
1.352 moko 74: pa_strncpy(log_spec, filespec_4log, MAX_STRING);
75:
1.374 moko 76: if(char* log_dir_pos=strrchr(log_spec, '/')){
1.352 moko 77: strcpy(log_dir_pos, "/parser3.log");
78: } else {
1.374 moko 79: // system-wide auto.p used: the current directory
1.352 moko 80: strcpy(log_spec, "./parser3.log");
1.193 paf 81: }
1.366 moko 82: slog=log_spec;
1.193 paf 83: }
1.366 moko 84: return slog;
85: }
86:
87: // SAPI
88:
89: static void pa_log(const char* fmt, va_list args) {
90: // use no memory [so that we could log out-of-memory error]
91: const char* slog=parser3_log_filespec();
92: FILE *f=strcmp(slog,"-") ? fopen(slog, "at") : stderr;
1.363 moko 93:
1.192 paf 94: // fallback to stderr
1.362 moko 95: if(!f)
1.61 paf 96: f=stderr;
1.208 paf 97:
98: setbuf(f, 0); // stderr stream is unbuffered by default, but still...
1.61 paf 99:
100: // prefix
101: time_t t=time(0);
1.218 paf 102: if(const char* stamp=ctime(&t)) { // never saw that
1.173 paf 103: if(size_t len=strlen(stamp)) // saw once stamp being =""
1.333 moko 104: fprintf(f, "[%.*s] [%u] ", (int)len-1, stamp, (unsigned int)pa_get_thread_id() );
1.171 paf 105: }
1.366 moko 106:
1.61 paf 107: // message
1.246 misha 108: char buf[MAX_LOG_STRING];
109: size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args);
110: size=remove_crlf(buf, buf+size);
1.239 paf 111: fwrite(buf, size, 1, f);
112:
1.341 moko 113: if(request_info_4log && request_info_4log->method) {
114: fprintf(f, " [uri=%s, method=%s, cl=%lu]\n", request_info_4log->uri ? request_info_4log->uri : "<unknown>", request_info_4log->method, (unsigned long)request_info_4log->content_length);
1.297 moko 115: } else
116: fputs(" [no request info]\n", f);
1.61 paf 117:
1.362 moko 118: if(f!=stderr)
1.61 paf 119: fclose(f);
1.85 parser 120: else
121: fflush(f);
1.124 parser 122: }
1.272 moko 123:
1.335 moko 124: void pa_log(const char* fmt, ...) {
1.272 moko 125: va_list args;
1.233 paf 126: va_start(args,fmt);
1.335 moko 127: pa_log(fmt, args);
1.233 paf 128: va_end(args);
129: }
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);
1.335 moko 135: pa_log(fmt, args);
1.124 parser 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);
1.335 moko 144: pa_log(fmt, args);
1.290 moko 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.358 moko 152: SAPI::send_error(sapi_info_4log ? *sapi_info_4log : *sapi_info, message);
1.290 moko 153: exit(1);
1.258 moko 154: // va_end(args);
1.28 paf 155: }
156:
1.358 moko 157: void SAPI::send_error(SAPI_Info& info, const char *exception_cstr, const char *status){
158: info.send_error(exception_cstr, status);
159: }
160:
1.294 moko 161: char* SAPI::Env::get(SAPI_Info& info, const char* name) {
162: return info.get_env(name);
1.218 paf 163: }
164:
1.305 moko 165: bool SAPI::Env::set(SAPI_Info& info, const char* name, const char* value) {
166: return info.set_env(name, value);
167: }
168:
1.294 moko 169: const char* const *SAPI::Env::get(SAPI_Info& info) {
170: return info.get_env();
1.180 paf 171: }
172:
1.354 moko 173: size_t SAPI::read_post(SAPI_Info& info, char* buf, size_t max_bytes) {
1.294 moko 174: return info.read_post(buf, max_bytes);
1.10 paf 175: }
176:
1.294 moko 177: void SAPI::add_header_attribute(SAPI_Info& info, const char* dont_store_key, const char* dont_store_value) {
1.358 moko 178: info.add_header(dont_store_key, dont_store_value);
1.19 paf 179: }
180:
1.360 moko 181: void SAPI::send_headers(SAPI_Info& info) {
1.358 moko 182: info.send_headers();
1.30 paf 183: }
1.20 paf 184:
1.360 moko 185: void SAPI::clear_headers(SAPI_Info& info) {
1.361 moko 186: info.clear_headers();
1.360 moko 187: }
188:
1.294 moko 189: size_t SAPI::send_body(SAPI_Info& info, const void *buf, size_t size) {
190: return info.send_body(buf, size);
1.58 paf 191: }
192:
1.354 moko 193: static const char* full_disk_path(const char* file_name = "") {
1.374 moko 194: const char* path=backslashes_to_slashes(file_name);
195: if(path[0]=='/' || is_os_absolute_path(path)) // '/' includes the UNC path //server, as there are no web paths here; C: is left for is_os_absolute_path
196: return path;
1.373 moko 197: char cwd[MAX_STRING];
1.374 moko 198: return pa_strcat(backslashes_to_slashes(getcwd(cwd, MAX_STRING) ? cwd : ""), "/", path);
1.97 parser 199: }
200:
1.218 paf 201: static void log_signal(const char* signal_name) {
1.358 moko 202: pa_log("%s received %s processing request", signal_name, request ? "while" : "before or after");
1.205 paf 203: }
204:
1.201 paf 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.322 moko 220: // requires pa_thread_request() in entry_exists() under Windows
1.354 moko 221: static const char* locate_config(const char* config_filespec_option, const char* executable_path){
1.322 moko 222: filespec_4log=config_filespec_option;
223: if(!filespec_4log)
1.321 moko 224: filespec_4log=getenv(PARSER_CONFIG_ENV_NAME);
1.322 moko 225: if(!filespec_4log)
226: filespec_4log=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
227: if(!filespec_4log){
1.373 moko 228: const char* exec_dir_pos = strrchr(executable_path, '/');
1.322 moko 229: #ifdef SYSTEM_CONFIG_FILE
1.353 moko 230: if(exec_dir_pos){
231: #endif
1.374 moko 232: // next to the executable; full path should be used to avoid "parser already configured"
233: filespec_4log=full_disk_path(!exec_dir_pos || (exec_dir_pos==executable_path+1 && *executable_path=='.')
234: ? AUTO_FILE_NAME // parser3 or ./parser3
235: : pa_strcat(pa_strdup(executable_path, exec_dir_pos - executable_path), "/" AUTO_FILE_NAME));
1.353 moko 236: if(entry_exists(filespec_4log))
237: return filespec_4log;
238: #ifdef SYSTEM_CONFIG_FILE
239: }
1.322 moko 240: if(entry_exists(SYSTEM_CONFIG_FILE)){
241: filespec_4log=NULL;
242: return SYSTEM_CONFIG_FILE;
243: }
244: #endif
245: return NULL;
1.321 moko 246: }
1.374 moko 247: // when -f auto.p full path should be used to avoid "parser already configured"
248: return filespec_4log=full_disk_path(filespec_4log);
1.321 moko 249: }
250:
1.227 paf 251: #ifdef WIN32
1.354 moko 252: static 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)
1.354 moko 285:
1.342 moko 286: #else
1.354 moko 287:
1.283 moko 288: #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) s
1.354 moko 289:
1.227 paf 290: #endif
291:
1.256 misha 292: class RequestController {
293: public:
294: RequestController(Request* r){
1.341 moko 295: request=r;
1.256 misha 296: }
297: ~RequestController(){
1.341 moko 298: request=0;
299: }
300: };
301:
302: class RequestInfoController {
303: public:
1.358 moko 304: RequestInfoController(Request_info* rinfo, SAPI_Info* sinfo){
1.341 moko 305: request_info_4log=rinfo;
1.358 moko 306: sapi_info_4log=sinfo;
1.341 moko 307: }
308: ~RequestInfoController(){
309: request_info_4log=0;
1.358 moko 310: sapi_info_4log=0;
1.256 misha 311: }
312: };
313:
1.342 moko 314: /** httpd support */
315: static const String httpd_class_name("httpd");
316:
1.317 moko 317: static void config_handler(SAPI_Info &info) {
1.341 moko 318: Request_info request_info;
1.358 moko 319: RequestInfoController ric(&request_info, &info);
1.341 moko 320:
1.373 moko 321: request_info.set_document_root(full_disk_path());
1.317 moko 322: request_info.uri = "";
1.319 moko 323: request_info.argv = argv_extra;
1.317 moko 324:
325: // prepare to process request
1.323 moko 326: Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
1.330 moko 327: // only once
328: config_filespec = locate_config(config_filespec, parser3_filespec);
329: // process main auto.p only
1.375 moko 330: #ifdef CODE_CACHE
331: Code_cache_manager::start();
332: #endif
1.330 moko 333: r.core(config_filespec, false, String::Empty);
1.375 moko 334: #ifdef CODE_CACHE
335: Code_cache_manager::finish(r);
336: #endif
1.317 moko 337: }
338:
339: static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection) {
1.341 moko 340: Request_info request_info;
1.358 moko 341: RequestInfoController ric(&request_info, &info);
1.341 moko 342:
1.325 moko 343: try {
1.336 moko 344: if(!connection.read_header())
1.332 moko 345: return; // ignore "void" connections
1.325 moko 346: info.populate_env();
347:
1.373 moko 348: request_info.set_document_root(full_disk_path());
1.325 moko 349: request_info.path_translated = filespec_to_process;
350: request_info.method = connection.method();
351: request_info.query_string = connection.query();
352: request_info.uri = request_info.strip_absolute_uri(connection.uri());
353: request_info.content_type = connection.content_type();
354: request_info.content_length = (size_t)connection.content_length();
355: request_info.cookie = info.get_env("HTTP_COOKIE");
356: request_info.mail_received = false;
357: request_info.argv = argv_extra;
1.294 moko 358:
1.325 moko 359: // prepare to process request
360: Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
1.330 moko 361: // process the request
1.342 moko 362: r.core(config_filespec, strcasecmp(request_info.method, "HEAD")==0, main_method_name, &httpd_class_name);
1.361 moko 363: } catch(const Exception& e) { // exception in connection handling
1.325 moko 364: SAPI::log(info, "%s", e.comment());
1.354 moko 365: const char* status = info.exception_http_status(e.type());
1.358 moko 366: if(*status)
1.337 moko 367: SAPI::send_error(info, e.comment(), status);
1.325 moko 368: }
369: }
1.317 moko 370:
1.328 moko 371: #ifdef _MSC_VER
372: DWORD WINAPI connection_thread(void *arg){
373: #else
1.325 moko 374: static void *connection_thread(void *arg){
1.328 moko 375: #endif
1.325 moko 376: HTTPD_Connection &connection=*(HTTPD_Connection*)arg;
377: SAPI_Info_HTTPD info(connection);
1.294 moko 378:
1.325 moko 379: try {
380: connection_handler(info, connection);
381: } catch(const Exception& e) { // exception in send_error
1.358 moko 382: pa_log("%s", e.comment());
1.294 moko 383: }
1.325 moko 384:
385: delete(&connection);
1.328 moko 386: return 0;
1.294 moko 387: }
388:
1.317 moko 389: static void httpd_mode() {
1.358 moko 390: config_handler(*sapi_info);
1.325 moko 391:
1.346 moko 392: SOCKET sock = HTTPD_Server::bind(httpd_host_port);
1.294 moko 393:
1.327 moko 394: #ifdef SIGPIPE
1.325 moko 395: signal(SIGPIPE, SIG_IGN);
396: #endif
1.317 moko 397:
1.297 moko 398: while(1){
1.328 moko 399: #ifndef _MSC_VER
1.325 moko 400: pid_t pid=1;
1.343 moko 401: if(HTTPD_Server::mode == HTTPD_Server::PARALLEL)
402: while (waitpid((pid_t)(-1), 0, WNOHANG) > 0);
1.328 moko 403: #endif
1.303 moko 404: try {
405: HTTPD_Connection connection;
1.345 moko 406: if(!connection.accept(sock, 500))
1.303 moko 407: continue;
408:
1.325 moko 409: switch (HTTPD_Server::mode) {
410: case HTTPD_Server::MULTITHREADED:
1.328 moko 411: #ifdef _MSC_VER
412: if (!GC_CreateThread(0, 0, connection_thread, new HTTPD_Connection(connection), 0, 0))
413: throw Exception("httpd.fork", 0, "thread creation failed");
1.346 moko 414: connection.sock=INVALID_SOCKET;
1.328 moko 415: break;
416: #else
1.339 moko 417: #ifdef HAVE_TLS
1.325 moko 418: pthread_t thread;
419: pthread_attr_t attr;
420: pthread_attr_init(&attr);
421: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
422:
423: if(int result=GC_pthread_create(&thread, &attr, connection_thread, new HTTPD_Connection(connection)))
424: throw Exception("httpd.fork", 0, "thread creation failed (%d)", result);
1.346 moko 425: connection.sock=INVALID_SOCKET;
1.325 moko 426: break;
1.339 moko 427: #endif
1.325 moko 428: case HTTPD_Server::PARALLEL:
429: pid=fork();
430: if(pid<0)
431: throw Exception("httpd.fork", 0, "fork failed: %s (%d)", strerror(errno), errno);
432: if(pid>0)
433: continue; // parent should close connection.sock as well
434: #endif
435: case HTTPD_Server::SEQUENTIAL: // and fork child
1.303 moko 436:
1.325 moko 437: SAPI_Info_HTTPD info(connection);
438: connection_handler(info, connection);
1.303 moko 439: }
440: // closing connection socket in HTTPD_Connection destructor
441: } catch(const Exception& e) { // exception in accept or send_error
1.358 moko 442: pa_log("%s", e.comment());
1.294 moko 443: }
1.325 moko 444:
1.328 moko 445: #ifndef _MSC_VER
1.325 moko 446: if(pid==0) // fork child
447: exit(0);
1.328 moko 448: #endif
1.294 moko 449: }
450: }
1.231 paf 451:
1.294 moko 452: /** main workhorse */
453:
1.316 moko 454: static void real_parser_handler(bool cgi) {
1.280 moko 455: // init libraries
1.218 paf 456: pa_globals_init();
1.294 moko 457:
1.296 moko 458: if(httpd_host_port){
1.308 moko 459: httpd_mode();
1.294 moko 460: }
461:
462: const char* request_method=getenv("REQUEST_METHOD");
463:
1.367 moko 464: if(!filespec_to_process){
1.376 moko 465: SAPI::send_error(*sapi_info, "Parser/" PARSER_VERSION_WITH_FEATURES);
1.367 moko 466: exit(1);
467: }
468:
1.297 moko 469: // global request info
1.341 moko 470: Request_info request_info;
1.358 moko 471: RequestInfoController ric(&request_info, sapi_info);
1.341 moko 472:
1.283 moko 473: request_info.path_translated = filespec_to_process;
474: request_info.method = request_method ? request_method : "GET";
475: request_info.query_string = MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(getenv("QUERY_STRING"));
476:
1.122 parser 477: if(cgi) {
1.284 moko 478: // obligatory
1.218 paf 479: const char* path_info=getenv("PATH_INFO");
1.214 paf 480: if(!path_info)
1.349 moko 481: SAPI::die("parser3: illegal CGI call (missing PATH_INFO)");
1.283 moko 482:
1.373 moko 483: const char* document_root = getenv("DOCUMENT_ROOT");
484: if(!document_root) {
1.285 moko 485: // IIS or fcgiwrap minimalistic setup
486: ssize_t prefix_len = strlen(filespec_to_process) - strlen(path_info);
487: if(prefix_len < 0 || strcmp(filespec_to_process + prefix_len, path_info) != 0)
1.349 moko 488: SAPI::die("parser3: illegal CGI call (invalid PATH_INFO in reinventing DOCUMENT_ROOT)");
1.285 moko 489:
1.373 moko 490: document_root = pa_strdup(filespec_to_process, prefix_len);
1.283 moko 491: }
1.373 moko 492: request_info.set_document_root(document_root);
1.214 paf 493:
1.285 moko 494: request_info.uri = request_info.strip_absolute_uri(getenv("REQUEST_URI"));
1.283 moko 495: if(request_info.uri) { // apache & others stuck to standards
1.284 moko 496: // another obligatory
1.285 moko 497: const char* script_name = getenv("SCRIPT_NAME");
1.284 moko 498: if(!script_name)
1.349 moko 499: SAPI::die("parser3: illegal CGI call (missing SCRIPT_NAME)");
1.214 paf 500: /*
501: http://parser3/env.html?123 =OK
502: $request:uri=/env.html?123
503: REQUEST_URI='/env.html?123'
504: SCRIPT_NAME='/cgi-bin/parser3'
505: PATH_INFO='/env.html'
1.285 moko 506:
1.214 paf 507: http://parser3/cgi-bin/parser3/env.html?123 =ERROR
508: $request:uri=/cgi-bin/parser3/env.html?123
509: REQUEST_URI='/cgi-bin/parser3/env.html?123'
510: SCRIPT_NAME='/cgi-bin/parser3'
511: PATH_INFO='/env.html'
512: */
1.285 moko 513: size_t script_name_len = strlen(script_name);
514: size_t uri_len = strlen(request_info.uri);
1.283 moko 515: if(strncmp(request_info.uri, script_name, script_name_len)==0 && script_name_len != uri_len) // under IIS they are the same
1.349 moko 516: SAPI::die("parser3: illegal CGI call (REQUEST_URI starts with SCRIPT_NAME)");
1.284 moko 517: } else { // fcgiwrap minimalistic setup
1.315 moko 518: request_info.uri = request_info.query_string && *request_info.query_string ? pa_strcat(path_info, "?", request_info.query_string) : path_info;
1.214 paf 519: }
1.283 moko 520: } else{
1.373 moko 521: request_info.set_document_root(full_disk_path());
1.285 moko 522: request_info.uri = "";
1.283 moko 523: }
1.122 parser 524:
1.283 moko 525: request_info.content_type = getenv("CONTENT_TYPE");
1.348 moko 526: request_info.content_length = cgi ? (size_t)pa_atoul(getenv("CONTENT_LENGTH")) : 0; // only SAPI_Info_CGI can read POST
1.283 moko 527: request_info.cookie = getenv("HTTP_COOKIE");
528: request_info.mail_received = mail_received;
1.184 paf 529:
1.319 moko 530: request_info.argv = argv_extra;
1.245 misha 531:
1.233 paf 532: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.362 moko 533: pa_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 534: #endif
535:
1.256 misha 536: {
1.377 ! moko 537: // prepare to process request
! 538: Request r(*sapi_info, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS);
1.297 moko 539: // initing ::request ptr for signal handlers
1.323 moko 540: RequestController rc(&r);
1.256 misha 541: // process the request
1.323 moko 542: r.core(locate_config(config_filespec, parser3_filespec), strcasecmp(request_info.method, "HEAD")==0);
1.303 moko 543: // clearing ::request in RequestController destructor to prevent signal handlers from accessing invalid memory
1.377 ! moko 544: // then the request is destroyed, before the libraries are unloaded: its pool cleanups call them (memcached)
1.122 parser 545: }
1.231 paf 546:
1.280 moko 547: // finalize libraries
1.225 paf 548: pa_globals_done();
1.218 paf 549: }
550:
1.231 paf 551: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
1.316 moko 552: static const Exception call_real_parser_handler__do_PEH_return_it(bool cgi) {
1.231 paf 553: try {
1.316 moko 554: real_parser_handler(cgi);
1.231 paf 555: } catch(const Exception& e) {
556: return e;
1.122 parser 557: }
1.231 paf 558:
559: return Exception();
1.122 parser 560: }
1.272 moko 561:
1.316 moko 562: static void call_real_parser_handler__supress_system_exception(bool cgi) {
1.231 paf 563: Exception parser_exception;
564: LPEXCEPTION_POINTERS system_exception=0;
1.122 parser 565:
1.231 paf 566: __try {
1.316 moko 567: parser_exception=call_real_parser_handler__do_PEH_return_it(cgi);
1.288 moko 568: } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
1.231 paf 569: if(system_exception)
570: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.371 moko 571: throw Exception("system", 0, "0x%08X at %p", er->ExceptionCode, er->ExceptionAddress);
1.218 paf 572: else
1.272 moko 573: throw Exception("system", 0, "<no exception record>");
1.218 paf 574: else
1.272 moko 575: throw Exception("system", 0, "<no exception information>");
1.231 paf 576: }
577:
578: if(parser_exception)
579: throw Exception(parser_exception);
580: }
1.273 moko 581:
582: #define REAL_PARSER_HANDLER call_real_parser_handler__supress_system_exception
583: #else
584: #define REAL_PARSER_HANDLER real_parser_handler
1.218 paf 585: #endif
1.131 paf 586:
1.364 moko 587: static void usage(const char* message=NULL) {
588: if(message){
589: fprintf(stderr, message, parser3_filespec);
590: }
591:
1.188 paf 592: printf(
1.235 paf 593: "Parser/%s\n"
1.368 moko 594: "Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)\n"
1.350 moko 595: "Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>\n"
1.184 paf 596: "\n"
1.307 moko 597: "Usage: %s [options] [file]\n"
1.184 paf 598: "Options are:\n"
1.185 paf 599: #ifdef WITH_MAILRECEIVE
1.193 paf 600: " -m Parse mail, put received letter to $mail:received\n"
1.184 paf 601: #endif
1.193 paf 602: " -f config_file Use this config file (/path/to/auto.p)\n"
1.364 moko 603: " -l log_file Use this log file (/path/to/parser3.log)\n"
1.296 moko 604: " -p [host:]port Start web server on this port\n"
1.272 moko 605: " -h Display usage information (this message)\n",
1.376 moko 606: PARSER_VERSION_WITH_FEATURES,
1.364 moko 607: parser3_filespec ? parser3_filespec : "parser3" );
1.184 paf 608: exit(EINVAL);
609: }
610:
1.364 moko 611: #define ARG_REQUIRED \
612: if(c[1] || !*(++carg)){ \
613: fprintf(stderr, "%s: option '%c' requires an argument\n", parser3_filespec, *c); \
614: usage(); \
615: }
1.294 moko 616:
1.249 misha 617: int main(int argc, char *argv[]) {
1.233 paf 618: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.362 moko 619: pa_log("main: entry");
1.233 paf 620: #endif
1.217 paf 621:
1.364 moko 622: if(!argc || !argv[0])
623: usage();
1.373 moko 624: parser3_filespec = backslashes_to_slashes(argv[0]);
1.364 moko 625:
1.294 moko 626: umask(2);
627:
628: // were we started as CGI?
1.316 moko 629: bool cgi=(getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) && !getenv("PARSER_VERSION");
1.365 moko 630: if(!cgi){
1.358 moko 631: sapi_info = &sapi_console;
1.365 moko 632: parser3_mode = "console";
633: }
1.294 moko 634:
1.203 paf 635: #ifdef SIGPIPE
1.325 moko 636: signal(SIGPIPE, SIGPIPE_handler);
1.203 paf 637: #endif
1.354 moko 638: char* raw_filespec_to_process = NULL;
1.210 paf 639: if(cgi) {
1.184 paf 640: raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.319 moko 641: argv_extra=argv + 1;
1.210 paf 642: } else {
1.364 moko 643: char** carg = argv + 1;
644: for(;*carg; carg++){
645: if((*carg)[0] != '-')
1.184 paf 646: break;
1.245 misha 647:
1.364 moko 648: for(char* c=(*carg)+1; *c; c++){
649: switch (*c) {
1.245 misha 650: case 'h':
1.364 moko 651: usage();
1.245 misha 652: break;
653: case 'f':
1.364 moko 654: ARG_REQUIRED;
655: config_filespec=*carg;
1.245 misha 656: break;
1.362 moko 657: case 'l':
1.364 moko 658: ARG_REQUIRED;
659: log_filespec=*carg;
1.362 moko 660: break;
1.294 moko 661: case 'p':
1.364 moko 662: ARG_REQUIRED;
663: httpd_host_port=*carg;
1.365 moko 664: parser3_mode="httpd";
1.294 moko 665: break;
1.185 paf 666: #ifdef WITH_MAILRECEIVE
1.245 misha 667: case 'm':
668: mail_received=true;
1.365 moko 669: parser3_mode="mail";
1.245 misha 670: break;
671: #endif
672: default:
1.364 moko 673: fprintf(stderr, "%s: invalid option '%c'\n", parser3_filespec, *c);
674: usage();
1.245 misha 675: break;
676: }
1.184 paf 677: }
678: }
1.245 misha 679:
1.364 moko 680: if (*carg) {
681: raw_filespec_to_process=*carg;
1.294 moko 682: } else {
1.364 moko 683: if(!httpd_host_port)
684: usage("%s: file not specified\n");
1.10 paf 685: }
1.294 moko 686:
1.364 moko 687: if (httpd_host_port && mail_received)
688: usage("%s: -p and -m options should not be used together\n");
1.310 moko 689:
1.364 moko 690: argv_extra=carg;
1.10 paf 691: }
692:
1.374 moko 693: // prepare full disk path log_filespec here, as logging must not allocate
694: if(!log_filespec)
695: log_filespec=getenv(PARSER_LOG_ENV_NAME);
696: if(!log_filespec)
697: log_filespec=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME);
698: if(log_filespec)
699: log_filespec=full_disk_path(log_filespec);
700:
1.264 moko 701: #ifdef _MSC_VER
1.100 parser 702: setmode(fileno(stdin), _O_BINARY);
703: setmode(fileno(stdout), _O_BINARY);
704: setmode(fileno(stderr), _O_BINARY);
705: #endif
706:
1.218 paf 707: #if defined(_MSC_VER) && defined(_DEBUG)
1.148 paf 708: // Get current flag
709: int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
710:
711: // Turn on leak-checking bit
712: tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
713:
714: // Set flag to the new value
715: _CrtSetDbgFlag( tmpFlag );
1.138 paf 716:
1.218 paf 717: _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
718: _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138 paf 719: #endif
720:
1.318 moko 721: try { // global try
722: if(raw_filespec_to_process && *raw_filespec_to_process){
1.351 moko 723: filespec_to_process=full_disk_path(raw_filespec_to_process);
1.318 moko 724: }
1.10 paf 725:
1.316 moko 726: REAL_PARSER_HANDLER(cgi);
1.361 moko 727: } catch(const Exception& e) { // exception in config_handler
1.359 moko 728: SAPI::log(*sapi_info, "%s", e.comment());
729: SAPI::send_error(*sapi_info, e.comment(), strcmp(e.type(), "file.missing") ? "500" : "404");
1.16 paf 730: }
1.109 parser 731:
1.233 paf 732: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.362 moko 733: pa_log("main: successful return");
1.233 paf 734: #endif
1.358 moko 735: return sapi_info->http_response_code < 100 ? sapi_info->http_response_code : 0;
1.1 paf 736: }
E-mail: