Annotation of parser3/src/targets/cgi/parser3.C, revision 1.388
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.388 ! moko 8: volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.387 2026/10/27 23:05:00 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.379 moko 21: #include "pa_websocket.h"
1.387 moko 22: #include "pa_os.h"
1.207 paf 23:
1.263 moko 24: #ifdef _MSC_VER
1.264 moko 25: #include <crtdbg.h>
1.263 moko 26: #include <windows.h>
27: #include <direct.h>
1.328 moko 28:
29: extern "C" HANDLE WINAPI GC_CreateThread(LPSECURITY_ATTRIBUTES, DWORD, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD);
30:
31: #else
32:
33: extern "C" int GC_pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void * /* arg */);
34:
1.120 parser 35: #endif
36:
1.232 paf 37: // defines
1.231 paf 38:
1.233 paf 39: // comment remove me after debugging
1.362 moko 40: //#define PA_DEBUG_CGI_ENTRY_EXIT
1.233 paf 41:
1.278 moko 42: #if defined(_MSC_VER) && !defined(_DEBUG)
1.231 paf 43: # define PA_SUPPRESS_SYSTEM_EXCEPTION
44: #endif
45:
1.109 parser 46: // consts
1.84 parser 47:
1.175 paf 48: #define REDIRECT_PREFIX "REDIRECT_"
1.181 paf 49: #define PARSER_CONFIG_ENV_NAME "CGI_PARSER_CONFIG"
1.226 paf 50: #define PARSER_LOG_ENV_NAME "CGI_PARSER_LOG"
1.159 paf 51:
1.358 moko 52: SAPI_Info sapi_console;
53: SAPI_Info_CGI sapi_cgi;
54:
55: static SAPI_Info *sapi_info = &sapi_cgi;
56: static THREAD_LOCAL SAPI_Info *sapi_info_4log = NULL; // global for correct send error in die()
1.357 moko 57:
1.365 moko 58: const char* parser3_mode = "cgi"; // $status:mode
1.316 moko 59: static const char* filespec_to_process = 0; // [file]
60: static const char* httpd_host_port = 0; // -p option
1.319 moko 61: static const char* config_filespec = 0; // -f option or from env or next to the executable if exists
1.374 moko 62: static const char* log_filespec = 0; // -l option or from env, full disk path
1.316 moko 63: static bool mail_received = false; // -m option? [asked to parse incoming message to $mail:received]
1.347 moko 64: static const char* parser3_filespec = 0; // argv[0]
1.319 moko 65: static char** argv_extra = NULL;
1.245 misha 66:
1.317 moko 67: // for error logging
1.341 moko 68: static THREAD_LOCAL Request_info *request_info_4log = NULL; // global for correct log() reporting
1.374 moko 69: 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 70:
1.366 moko 71: const char *parser3_log_filespec() { // $status:log-filename
1.363 moko 72: const char* slog=log_filespec;
1.61 paf 73:
1.366 moko 74: if(!slog) {
1.381 moko 75: static THREAD_LOCAL char log_spec[MAX_STRING + 12 /* '/parser3.log' */];
1.352 moko 76: pa_strncpy(log_spec, filespec_4log, MAX_STRING);
77:
1.374 moko 78: if(char* log_dir_pos=strrchr(log_spec, '/')){
1.352 moko 79: strcpy(log_dir_pos, "/parser3.log");
80: } else {
1.374 moko 81: // system-wide auto.p used: the current directory
1.352 moko 82: strcpy(log_spec, "./parser3.log");
1.193 paf 83: }
1.366 moko 84: slog=log_spec;
1.193 paf 85: }
1.366 moko 86: return slog;
87: }
88:
89: // SAPI
90:
1.385 moko 91: #ifndef O_CLOEXEC
92: #define O_CLOEXEC 0
93: #endif
94: #ifndef STDERR_FILENO
95: #define STDERR_FILENO 2
96: #endif
1.363 moko 97:
1.385 moko 98: static void pa_log(const char* stamp, const char* fmt, va_list args) {
99: // use no memory (to log out-of-memory error): the line is built on the stack and written at once
100: char line[64 + MAX_LOG_STRING + 1024 + 32 + 100]; // prefix + message + request info suffix: uri + method and the rest of it
101: size_t size=0;
1.61 paf 102:
103: // prefix
1.387 moko 104: if(!stamp)
105: stamp=pa_ctime(time(0));
106: if(size_t len=strlen(stamp)) // saw once stamp being =""
107: size=pa_snprintf(line, 64, "[%.*s] [%u] ", (int)len-1 /* exclude \n */, stamp, (unsigned int)pa_get_thread_id() );
1.366 moko 108:
1.61 paf 109: // message
1.385 moko 110: size+=remove_crlf(line+size, line+size+pa_vsnprintf(line+size, MAX_LOG_STRING, fmt, args));
1.61 paf 111:
1.385 moko 112: // suffix
113: if(request_info_4log && request_info_4log->method)
114: size+=pa_snprintf(line+size, sizeof(line)-size, " [uri=%.1024s, method=%.32s, cl=%lu]\n", request_info_4log->uri ? request_info_4log->uri : "<unknown>", request_info_4log->method, (unsigned long)request_info_4log->content_length);
1.85 parser 115: else
1.385 moko 116: size+=pa_snprintf(line+size, sizeof(line)-size, " [no request info]\n");
117:
118: const char* slog=parser3_log_filespec();
119: int fd=strcmp(slog,"-") ? pa_open(slog, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0666) : -1;
120: if(fd<0)
121: fd=STDERR_FILENO; // fallback to stderr
122: PA_UNUSED int ignore_result=write(fd, line, (unsigned int)size);
123: if(fd!=STDERR_FILENO)
124: close(fd);
1.124 parser 125: }
1.272 moko 126:
1.335 moko 127: void pa_log(const char* fmt, ...) {
1.272 moko 128: va_list args;
1.233 paf 129: va_start(args,fmt);
1.385 moko 130: pa_log(0, fmt, args);
1.233 paf 131: va_end(args);
132: }
1.124 parser 133:
1.387 moko 134: // for signal handlers, where pa_ctime can't be called: the stamp is prepared in advance
135: static void pa_log_at(const char* stamp, const char* fmt, ...) {
136: va_list args;
137: va_start(args,fmt);
138: pa_log(stamp, fmt, args);
139: va_end(args);
140: }
141:
1.322 moko 142: // appends to parser3.log located next to the config file if openable, to stderr otherwize
1.218 paf 143: void SAPI::log(SAPI_Info&, const char* fmt, ...) {
1.272 moko 144: va_list args;
1.124 parser 145: va_start(args,fmt);
1.385 moko 146: pa_log(0, fmt, args);
1.124 parser 147: va_end(args);
148: }
149:
1.290 moko 150: void SAPI::die(const char* fmt, ...) {
151: va_list args;
152:
153: // logging first, first vsnprintf
154: va_start(args,fmt);
1.385 moko 155: pa_log(0, fmt, args);
1.290 moko 156: va_end(args);
1.138 paf 157:
1.290 moko 158: // inform user, second vsnprintf
159: va_start(args, fmt);
1.299 moko 160: char message[MAX_STRING];
1.380 moko 161: pa_vsnprintf(message, MAX_STRING, fmt, args);
1.134 paf 162:
1.358 moko 163: SAPI::send_error(sapi_info_4log ? *sapi_info_4log : *sapi_info, message);
1.290 moko 164: exit(1);
1.258 moko 165: // va_end(args);
1.28 paf 166: }
167:
1.358 moko 168: void SAPI::send_error(SAPI_Info& info, const char *exception_cstr, const char *status){
169: info.send_error(exception_cstr, status);
170: }
171:
1.294 moko 172: char* SAPI::Env::get(SAPI_Info& info, const char* name) {
173: return info.get_env(name);
1.218 paf 174: }
175:
1.305 moko 176: bool SAPI::Env::set(SAPI_Info& info, const char* name, const char* value) {
177: return info.set_env(name, value);
178: }
179:
1.294 moko 180: const char* const *SAPI::Env::get(SAPI_Info& info) {
181: return info.get_env();
1.180 paf 182: }
183:
1.354 moko 184: size_t SAPI::read_post(SAPI_Info& info, char* buf, size_t max_bytes) {
1.294 moko 185: return info.read_post(buf, max_bytes);
1.10 paf 186: }
187:
1.294 moko 188: void SAPI::add_header_attribute(SAPI_Info& info, const char* dont_store_key, const char* dont_store_value) {
1.358 moko 189: info.add_header(dont_store_key, dont_store_value);
1.19 paf 190: }
191:
1.360 moko 192: void SAPI::send_headers(SAPI_Info& info) {
1.358 moko 193: info.send_headers();
1.30 paf 194: }
1.20 paf 195:
1.360 moko 196: void SAPI::clear_headers(SAPI_Info& info) {
1.361 moko 197: info.clear_headers();
1.360 moko 198: }
199:
1.294 moko 200: size_t SAPI::send_body(SAPI_Info& info, const void *buf, size_t size) {
201: return info.send_body(buf, size);
1.58 paf 202: }
203:
1.354 moko 204: static const char* full_disk_path(const char* file_name = "") {
1.374 moko 205: const char* path=backslashes_to_slashes(file_name);
206: 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
207: return path;
1.373 moko 208: char cwd[MAX_STRING];
1.374 moko 209: return pa_strcat(backslashes_to_slashes(getcwd(cwd, MAX_STRING) ? cwd : ""), "/", path);
1.97 parser 210: }
211:
1.201 paf 212: #ifdef SIGPIPE
1.386 moko 213: // write to a closed socket or pipe fails with EPIPE. Not SIG_IGN, as the ignored signal is inherited by the executed programs
214: static void SIGPIPE_handler(int /*sig*/){}
1.201 paf 215: #endif
216:
1.322 moko 217: // requires pa_thread_request() in entry_exists() under Windows
1.354 moko 218: static const char* locate_config(const char* config_filespec_option, const char* executable_path){
1.322 moko 219: filespec_4log=config_filespec_option;
220: if(!filespec_4log)
1.321 moko 221: filespec_4log=getenv(PARSER_CONFIG_ENV_NAME);
1.322 moko 222: if(!filespec_4log)
223: filespec_4log=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
224: if(!filespec_4log){
1.373 moko 225: const char* exec_dir_pos = strrchr(executable_path, '/');
1.322 moko 226: #ifdef SYSTEM_CONFIG_FILE
1.353 moko 227: if(exec_dir_pos){
228: #endif
1.374 moko 229: // next to the executable; full path should be used to avoid "parser already configured"
230: filespec_4log=full_disk_path(!exec_dir_pos || (exec_dir_pos==executable_path+1 && *executable_path=='.')
231: ? AUTO_FILE_NAME // parser3 or ./parser3
232: : pa_strcat(pa_strdup(executable_path, exec_dir_pos - executable_path), "/" AUTO_FILE_NAME));
1.353 moko 233: if(entry_exists(filespec_4log))
234: return filespec_4log;
235: #ifdef SYSTEM_CONFIG_FILE
236: }
1.322 moko 237: if(entry_exists(SYSTEM_CONFIG_FILE)){
238: filespec_4log=NULL;
239: return SYSTEM_CONFIG_FILE;
240: }
241: #endif
242: return NULL;
1.321 moko 243: }
1.374 moko 244: // when -f auto.p full path should be used to avoid "parser already configured"
245: return filespec_4log=full_disk_path(filespec_4log);
1.321 moko 246: }
247:
1.227 paf 248: #ifdef WIN32
1.354 moko 249: static const char* maybe_reconstruct_IIS_status_in_qs(const char* original) {
1.228 paf 250: // 404;http://servername/page[?param=value...]
1.227 paf 251: // ';' should be urlencoded by HTTP standard, so we shouldn't get it from browser
252: // and can consider that as an indication that this is IIS way to report errors
253:
1.272 moko 254: if(original && isdigit((unsigned char)original[0]) && isdigit((unsigned char)original[1]) && isdigit((unsigned char)original[2]) && original[3]==';'){
1.227 paf 255: size_t original_len=strlen(original);
1.272 moko 256: char* reconstructed=new(PointerFreeGC) char[original_len +12/*IIS-STATUS=&*/ +14/*IIS-DOCUMENT=&*/ +1];
1.227 paf 257: char* cur=reconstructed;
258: memcpy(cur, "IIS-STATUS=", 11); cur+=11;
259: memcpy(cur, original, 3); cur+=3;
260: *cur++='&';
261:
1.228 paf 262: const char* qmark_at=strchr(original, '?');
1.227 paf 263: memcpy(cur, "IIS-DOCUMENT=", 13); cur+=13;
264: {
1.272 moko 265: size_t value_len=(qmark_at ? qmark_at-original : original_len)-4;
1.227 paf 266: memcpy(cur, original+4, value_len); cur+=value_len;
267: }
268:
269: if(qmark_at) {
270: *cur++='&';
271: strcpy(cur, qmark_at+1/*skip ? itself*/);
272: } else
273: *cur=0;
274:
275: return reconstructed;
276: }
277:
278: return original;
279: }
1.283 moko 280:
281: #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) maybe_reconstruct_IIS_status_in_qs(s)
1.354 moko 282:
1.342 moko 283: #else
1.354 moko 284:
1.283 moko 285: #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) s
1.354 moko 286:
1.227 paf 287: #endif
288:
1.387 moko 289: #define RESPONSE_TIMEOUT_GRACE 1 // seconds to finish after the interruption, then the process is killed
290:
291: /// $response:timeout: interrupts the request that is not finished in time, the object is created before the request and destroyed after it
292: class Response_timeout {
293: public:
294: static THREAD_LOCAL Response_timeout *current;
295:
296: /// @a akill_process: the process will be killed if the request is still running after the grace period
297: Response_timeout(bool akill_process) : kill_process(akill_process), expired(0), armed(false) {
298: current=this;
299: }
300: ~Response_timeout() {
301: set(0);
302: current=0;
303: }
304:
305: /// restarts the timer for the given number of seconds, 0 cancels it
306: void set(int aseconds) {
307: if(armed){
308: pa_timer_stop(timer);
309: armed=false;
310: }
311: if(!aseconds)
312: return;
313:
314: seconds=aseconds;
315: expired=0;
316: if(kill_process) // pa_ctime can't be called from the handler, so preparing for the log in advance
317: stamp=pa_strdup(pa_ctime(time(0)+seconds+RESPONSE_TIMEOUT_GRACE));
318:
319: armed=pa_timer_start(&timer, seconds, kill_process ? RESPONSE_TIMEOUT_GRACE : 0, handler);
320: }
321:
322: private:
323: bool kill_process;
1.388 ! moko 324: volatile int expired;
1.387 moko 325: bool armed;
326: pa_timer timer;
327: int seconds;
328: const char *stamp;
329:
330: static void handler(int) {
331: Response_timeout *timeout=current;
332: if(!timeout)
333: return;
334:
335: if(timeout->expired && timeout->kill_process){
336: // the request is still running after the grace period
337: pa_log_at(timeout->stamp, "response timeout (%d s) exceeded, process killed", timeout->seconds);
338: #ifndef _MSC_VER
339: kill(getpid(), SIGKILL);
340: #endif
341: return;
342: }
343:
344: timeout->expired=1;
1.388 ! moko 345: if(thread_request)
! 346: thread_request->set_skip(Request::SKIP_INTERRUPTED);
1.387 moko 347: }
348: };
349:
350: THREAD_LOCAL Response_timeout *Response_timeout::current=0;
351:
352: void SAPI::set_response_timeout(SAPI_Info&, int seconds) {
353: if(Response_timeout::current)
354: Response_timeout::current->set(seconds);
355: }
356:
1.341 moko 357: class RequestInfoController {
358: public:
1.358 moko 359: RequestInfoController(Request_info* rinfo, SAPI_Info* sinfo){
1.341 moko 360: request_info_4log=rinfo;
1.358 moko 361: sapi_info_4log=sinfo;
1.341 moko 362: }
363: ~RequestInfoController(){
364: request_info_4log=0;
1.358 moko 365: sapi_info_4log=0;
1.256 misha 366: }
367: };
368:
1.342 moko 369: /** httpd support */
370: static const String httpd_class_name("httpd");
371:
1.317 moko 372: static void config_handler(SAPI_Info &info) {
1.341 moko 373: Request_info request_info;
1.358 moko 374: RequestInfoController ric(&request_info, &info);
1.341 moko 375:
1.373 moko 376: request_info.set_document_root(full_disk_path());
1.317 moko 377: request_info.uri = "";
1.319 moko 378: request_info.argv = argv_extra;
1.317 moko 379:
380: // prepare to process request
1.323 moko 381: Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
1.330 moko 382: // only once
383: config_filespec = locate_config(config_filespec, parser3_filespec);
384: // process main auto.p only
1.375 moko 385: #ifdef CODE_CACHE
386: Code_cache_manager::start();
387: #endif
1.378 moko 388: r.core(config_filespec, String::Empty);
1.375 moko 389: #ifdef CODE_CACHE
390: Code_cache_manager::finish(r);
391: #endif
1.317 moko 392: }
393:
394: static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection) {
1.341 moko 395: Request_info request_info;
1.358 moko 396: RequestInfoController ric(&request_info, &info);
1.341 moko 397:
1.325 moko 398: try {
1.336 moko 399: if(!connection.read_header())
1.332 moko 400: return; // ignore "void" connections
1.325 moko 401: info.populate_env();
402:
1.373 moko 403: request_info.set_document_root(full_disk_path());
1.325 moko 404: request_info.path_translated = filespec_to_process;
405: request_info.method = connection.method();
406: request_info.query_string = connection.query();
407: request_info.uri = request_info.strip_absolute_uri(connection.uri());
408: request_info.content_type = connection.content_type();
409: request_info.content_length = (size_t)connection.content_length();
410: request_info.cookie = info.get_env("HTTP_COOKIE");
411: request_info.mail_received = false;
412: request_info.argv = argv_extra;
1.294 moko 413:
1.387 moko 414: // process can be killed on timeout only in parallel mode
415: Response_timeout response_timeout(HTTPD_Server::mode == HTTPD_Server::PARALLEL);
416:
1.325 moko 417: // prepare to process request
418: Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
1.379 moko 419: WebSocket websocket(connection);
420: r.websocket=&websocket;
1.330 moko 421: // process the request
1.378 moko 422: r.core(config_filespec, main_method_name, &httpd_class_name);
1.361 moko 423: } catch(const Exception& e) { // exception in connection handling
1.325 moko 424: SAPI::log(info, "%s", e.comment());
1.354 moko 425: const char* status = info.exception_http_status(e.type());
1.358 moko 426: if(*status)
1.337 moko 427: SAPI::send_error(info, e.comment(), status);
1.325 moko 428: }
429: }
1.317 moko 430:
1.328 moko 431: #ifdef _MSC_VER
432: DWORD WINAPI connection_thread(void *arg){
433: #else
1.325 moko 434: static void *connection_thread(void *arg){
1.328 moko 435: #endif
1.382 moko 436: Temp_gc_references gc_references; // for pointers GC does not see, the very start of the thread
437:
1.325 moko 438: HTTPD_Connection &connection=*(HTTPD_Connection*)arg;
439: SAPI_Info_HTTPD info(connection);
1.294 moko 440:
1.325 moko 441: try {
442: connection_handler(info, connection);
443: } catch(const Exception& e) { // exception in send_error
1.358 moko 444: pa_log("%s", e.comment());
1.294 moko 445: }
1.325 moko 446:
447: delete(&connection);
1.328 moko 448: return 0;
1.294 moko 449: }
450:
1.384 moko 451: static void reject_connection(HTTPD_Connection &connection) {
452: // main thread only
453: static unsigned long total=0;
454: static time_t logged=0;
455:
456: total++;
457: time_t now=time(0);
458: if(now!=logged){ // not more than once a second
459: logged=now;
460: pa_log("max-connections limit %d reached, rejecting connection from %s (%lu rejected in total)", HTTPD_Server::max_connections, connection.remote_addr, total);
461: }
462:
463: SAPI_Info_HTTPD info(connection);
464: SAPI::send_error(info, "too many connections", "503");
465: }
466:
1.317 moko 467: static void httpd_mode() {
1.358 moko 468: config_handler(*sapi_info);
1.325 moko 469:
1.346 moko 470: SOCKET sock = HTTPD_Server::bind(httpd_host_port);
1.294 moko 471:
1.297 moko 472: while(1){
1.328 moko 473: #ifndef _MSC_VER
1.325 moko 474: pid_t pid=1;
1.328 moko 475: #endif
1.303 moko 476: try {
477: HTTPD_Connection connection;
1.383 moko 478: bool accepted=connection.accept(sock, 500);
479: #ifndef _MSC_VER
480: if(HTTPD_Server::mode == HTTPD_Server::PARALLEL)
481: while (waitpid((pid_t)(-1), 0, WNOHANG) > 0)
482: HTTPD_Server::track_connection(-1); // the child has exited, some of them while we were waiting
483: #endif
484: if(!accepted)
1.303 moko 485: continue;
486:
1.383 moko 487: if(HTTPD_Server::too_many_connections()){
1.384 moko 488: reject_connection(connection); // no thread or process is started
1.383 moko 489: continue;
490: }
491:
1.325 moko 492: switch (HTTPD_Server::mode) {
493: case HTTPD_Server::MULTITHREADED:
1.328 moko 494: #ifdef _MSC_VER
495: if (!GC_CreateThread(0, 0, connection_thread, new HTTPD_Connection(connection), 0, 0))
496: throw Exception("httpd.fork", 0, "thread creation failed");
1.383 moko 497: connection.sock=INVALID_SOCKET; // thread owns the connection now
1.328 moko 498: break;
499: #else
1.339 moko 500: #ifdef HAVE_TLS
1.325 moko 501: pthread_t thread;
502: pthread_attr_t attr;
503: pthread_attr_init(&attr);
504: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
505:
506: if(int result=GC_pthread_create(&thread, &attr, connection_thread, new HTTPD_Connection(connection)))
507: throw Exception("httpd.fork", 0, "thread creation failed (%d)", result);
1.383 moko 508: connection.sock=INVALID_SOCKET; // thread owns the connection now
1.325 moko 509: break;
1.339 moko 510: #endif
1.325 moko 511: case HTTPD_Server::PARALLEL:
512: pid=fork();
513: if(pid<0)
514: throw Exception("httpd.fork", 0, "fork failed: %s (%d)", strerror(errno), errno);
1.383 moko 515: if(pid>0){
516: closesocket(connection.sock); // parent should close connection.sock as well
517: connection.sock=INVALID_SOCKET; // child owns the connection now
518: continue;
519: }
1.325 moko 520: #endif
521: case HTTPD_Server::SEQUENTIAL: // and fork child
1.303 moko 522:
1.325 moko 523: SAPI_Info_HTTPD info(connection);
524: connection_handler(info, connection);
1.303 moko 525: }
1.383 moko 526: // SEQUENTIAL or error: closing connection socket in HTTPD_Connection destructor
1.303 moko 527: } catch(const Exception& e) { // exception in accept or send_error
1.358 moko 528: pa_log("%s", e.comment());
1.294 moko 529: }
1.325 moko 530:
1.328 moko 531: #ifndef _MSC_VER
1.325 moko 532: if(pid==0) // fork child
533: exit(0);
1.328 moko 534: #endif
1.294 moko 535: }
536: }
1.231 paf 537:
1.294 moko 538: /** main workhorse */
539:
1.316 moko 540: static void real_parser_handler(bool cgi) {
1.280 moko 541: // init libraries
1.218 paf 542: pa_globals_init();
1.294 moko 543:
1.296 moko 544: if(httpd_host_port){
1.308 moko 545: httpd_mode();
1.294 moko 546: }
547:
548: const char* request_method=getenv("REQUEST_METHOD");
549:
1.367 moko 550: if(!filespec_to_process){
1.376 moko 551: SAPI::send_error(*sapi_info, "Parser/" PARSER_VERSION_WITH_FEATURES);
1.367 moko 552: exit(1);
553: }
554:
1.297 moko 555: // global request info
1.341 moko 556: Request_info request_info;
1.358 moko 557: RequestInfoController ric(&request_info, sapi_info);
1.341 moko 558:
1.283 moko 559: request_info.path_translated = filespec_to_process;
560: request_info.method = request_method ? request_method : "GET";
561: request_info.query_string = MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(getenv("QUERY_STRING"));
562:
1.122 parser 563: if(cgi) {
1.284 moko 564: // obligatory
1.218 paf 565: const char* path_info=getenv("PATH_INFO");
1.214 paf 566: if(!path_info)
1.349 moko 567: SAPI::die("parser3: illegal CGI call (missing PATH_INFO)");
1.283 moko 568:
1.373 moko 569: const char* document_root = getenv("DOCUMENT_ROOT");
570: if(!document_root) {
1.285 moko 571: // IIS or fcgiwrap minimalistic setup
572: ssize_t prefix_len = strlen(filespec_to_process) - strlen(path_info);
573: if(prefix_len < 0 || strcmp(filespec_to_process + prefix_len, path_info) != 0)
1.349 moko 574: SAPI::die("parser3: illegal CGI call (invalid PATH_INFO in reinventing DOCUMENT_ROOT)");
1.285 moko 575:
1.373 moko 576: document_root = pa_strdup(filespec_to_process, prefix_len);
1.283 moko 577: }
1.373 moko 578: request_info.set_document_root(document_root);
1.214 paf 579:
1.285 moko 580: request_info.uri = request_info.strip_absolute_uri(getenv("REQUEST_URI"));
1.283 moko 581: if(request_info.uri) { // apache & others stuck to standards
1.284 moko 582: // another obligatory
1.285 moko 583: const char* script_name = getenv("SCRIPT_NAME");
1.284 moko 584: if(!script_name)
1.349 moko 585: SAPI::die("parser3: illegal CGI call (missing SCRIPT_NAME)");
1.214 paf 586: /*
587: http://parser3/env.html?123 =OK
588: $request:uri=/env.html?123
589: REQUEST_URI='/env.html?123'
590: SCRIPT_NAME='/cgi-bin/parser3'
591: PATH_INFO='/env.html'
1.285 moko 592:
1.214 paf 593: http://parser3/cgi-bin/parser3/env.html?123 =ERROR
594: $request:uri=/cgi-bin/parser3/env.html?123
595: REQUEST_URI='/cgi-bin/parser3/env.html?123'
596: SCRIPT_NAME='/cgi-bin/parser3'
597: PATH_INFO='/env.html'
598: */
1.285 moko 599: size_t script_name_len = strlen(script_name);
600: size_t uri_len = strlen(request_info.uri);
1.283 moko 601: 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 602: SAPI::die("parser3: illegal CGI call (REQUEST_URI starts with SCRIPT_NAME)");
1.284 moko 603: } else { // fcgiwrap minimalistic setup
1.315 moko 604: request_info.uri = request_info.query_string && *request_info.query_string ? pa_strcat(path_info, "?", request_info.query_string) : path_info;
1.214 paf 605: }
1.283 moko 606: } else{
1.373 moko 607: request_info.set_document_root(full_disk_path());
1.285 moko 608: request_info.uri = "";
1.283 moko 609: }
1.122 parser 610:
1.283 moko 611: request_info.content_type = getenv("CONTENT_TYPE");
1.348 moko 612: request_info.content_length = cgi ? (size_t)pa_atoul(getenv("CONTENT_LENGTH")) : 0; // only SAPI_Info_CGI can read POST
1.283 moko 613: request_info.cookie = getenv("HTTP_COOKIE");
614: request_info.mail_received = mail_received;
1.184 paf 615:
1.319 moko 616: request_info.argv = argv_extra;
1.245 misha 617:
1.233 paf 618: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.362 moko 619: 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 620: #endif
621:
1.256 misha 622: {
1.387 moko 623: // CGI process can be killed on timeout
624: Response_timeout response_timeout(true);
625:
1.377 moko 626: // prepare to process request
627: Request r(*sapi_info, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS);
1.256 misha 628: // process the request
1.378 moko 629: r.core(locate_config(config_filespec, parser3_filespec));
1.377 moko 630: // then the request is destroyed, before the libraries are unloaded: its pool cleanups call them (memcached)
1.122 parser 631: }
1.231 paf 632:
1.280 moko 633: // finalize libraries
1.225 paf 634: pa_globals_done();
1.218 paf 635: }
636:
1.231 paf 637: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
1.316 moko 638: static const Exception call_real_parser_handler__do_PEH_return_it(bool cgi) {
1.231 paf 639: try {
1.316 moko 640: real_parser_handler(cgi);
1.231 paf 641: } catch(const Exception& e) {
642: return e;
1.122 parser 643: }
1.231 paf 644:
645: return Exception();
1.122 parser 646: }
1.272 moko 647:
1.316 moko 648: static void call_real_parser_handler__supress_system_exception(bool cgi) {
1.231 paf 649: Exception parser_exception;
650: LPEXCEPTION_POINTERS system_exception=0;
1.122 parser 651:
1.231 paf 652: __try {
1.316 moko 653: parser_exception=call_real_parser_handler__do_PEH_return_it(cgi);
1.288 moko 654: } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
1.231 paf 655: if(system_exception)
656: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.371 moko 657: throw Exception("system", 0, "0x%08X at %p", er->ExceptionCode, er->ExceptionAddress);
1.218 paf 658: else
1.272 moko 659: throw Exception("system", 0, "<no exception record>");
1.218 paf 660: else
1.272 moko 661: throw Exception("system", 0, "<no exception information>");
1.231 paf 662: }
663:
664: if(parser_exception)
665: throw Exception(parser_exception);
666: }
1.273 moko 667:
668: #define REAL_PARSER_HANDLER call_real_parser_handler__supress_system_exception
669: #else
670: #define REAL_PARSER_HANDLER real_parser_handler
1.218 paf 671: #endif
1.131 paf 672:
1.364 moko 673: static void usage(const char* message=NULL) {
674: if(message){
675: fprintf(stderr, message, parser3_filespec);
676: }
677:
1.188 paf 678: printf(
1.235 paf 679: "Parser/%s\n"
1.368 moko 680: "Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)\n"
1.350 moko 681: "Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>\n"
1.184 paf 682: "\n"
1.307 moko 683: "Usage: %s [options] [file]\n"
1.184 paf 684: "Options are:\n"
1.185 paf 685: #ifdef WITH_MAILRECEIVE
1.193 paf 686: " -m Parse mail, put received letter to $mail:received\n"
1.184 paf 687: #endif
1.193 paf 688: " -f config_file Use this config file (/path/to/auto.p)\n"
1.364 moko 689: " -l log_file Use this log file (/path/to/parser3.log)\n"
1.296 moko 690: " -p [host:]port Start web server on this port\n"
1.272 moko 691: " -h Display usage information (this message)\n",
1.376 moko 692: PARSER_VERSION_WITH_FEATURES,
1.364 moko 693: parser3_filespec ? parser3_filespec : "parser3" );
1.184 paf 694: exit(EINVAL);
695: }
696:
1.364 moko 697: #define ARG_REQUIRED \
698: if(c[1] || !*(++carg)){ \
699: fprintf(stderr, "%s: option '%c' requires an argument\n", parser3_filespec, *c); \
700: usage(); \
701: }
1.294 moko 702:
1.249 misha 703: int main(int argc, char *argv[]) {
1.382 moko 704: Temp_gc_references gc_references; // for pointers GC does not see
705:
1.233 paf 706: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.362 moko 707: pa_log("main: entry");
1.233 paf 708: #endif
1.217 paf 709:
1.364 moko 710: if(!argc || !argv[0])
711: usage();
1.373 moko 712: parser3_filespec = backslashes_to_slashes(argv[0]);
1.364 moko 713:
1.294 moko 714: umask(2);
715:
716: // were we started as CGI?
1.316 moko 717: bool cgi=(getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) && !getenv("PARSER_VERSION");
1.365 moko 718: if(!cgi){
1.358 moko 719: sapi_info = &sapi_console;
1.365 moko 720: parser3_mode = "console";
721: }
1.294 moko 722:
1.203 paf 723: #ifdef SIGPIPE
1.325 moko 724: signal(SIGPIPE, SIGPIPE_handler);
1.203 paf 725: #endif
1.354 moko 726: char* raw_filespec_to_process = NULL;
1.210 paf 727: if(cgi) {
1.184 paf 728: raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.319 moko 729: argv_extra=argv + 1;
1.210 paf 730: } else {
1.364 moko 731: char** carg = argv + 1;
732: for(;*carg; carg++){
733: if((*carg)[0] != '-')
1.184 paf 734: break;
1.245 misha 735:
1.364 moko 736: for(char* c=(*carg)+1; *c; c++){
737: switch (*c) {
1.245 misha 738: case 'h':
1.364 moko 739: usage();
1.245 misha 740: break;
741: case 'f':
1.364 moko 742: ARG_REQUIRED;
743: config_filespec=*carg;
1.245 misha 744: break;
1.362 moko 745: case 'l':
1.364 moko 746: ARG_REQUIRED;
747: log_filespec=*carg;
1.362 moko 748: break;
1.294 moko 749: case 'p':
1.364 moko 750: ARG_REQUIRED;
751: httpd_host_port=*carg;
1.365 moko 752: parser3_mode="httpd";
1.294 moko 753: break;
1.185 paf 754: #ifdef WITH_MAILRECEIVE
1.245 misha 755: case 'm':
756: mail_received=true;
1.365 moko 757: parser3_mode="mail";
1.245 misha 758: break;
759: #endif
760: default:
1.364 moko 761: fprintf(stderr, "%s: invalid option '%c'\n", parser3_filespec, *c);
762: usage();
1.245 misha 763: break;
764: }
1.184 paf 765: }
766: }
1.245 misha 767:
1.364 moko 768: if (*carg) {
769: raw_filespec_to_process=*carg;
1.294 moko 770: } else {
1.364 moko 771: if(!httpd_host_port)
772: usage("%s: file not specified\n");
1.10 paf 773: }
1.294 moko 774:
1.364 moko 775: if (httpd_host_port && mail_received)
776: usage("%s: -p and -m options should not be used together\n");
1.310 moko 777:
1.364 moko 778: argv_extra=carg;
1.10 paf 779: }
780:
1.374 moko 781: // prepare full disk path log_filespec here, as logging must not allocate
782: if(!log_filespec)
783: log_filespec=getenv(PARSER_LOG_ENV_NAME);
784: if(!log_filespec)
785: log_filespec=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME);
1.385 moko 786: if(log_filespec && strcmp(log_filespec, "-")) // "-" is stderr
1.374 moko 787: log_filespec=full_disk_path(log_filespec);
788:
1.264 moko 789: #ifdef _MSC_VER
1.100 parser 790: setmode(fileno(stdin), _O_BINARY);
791: setmode(fileno(stdout), _O_BINARY);
792: setmode(fileno(stderr), _O_BINARY);
793: #endif
794:
1.218 paf 795: #if defined(_MSC_VER) && defined(_DEBUG)
1.148 paf 796: // Get current flag
797: int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
798:
799: // Turn on leak-checking bit
800: tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
801:
802: // Set flag to the new value
803: _CrtSetDbgFlag( tmpFlag );
1.138 paf 804:
1.218 paf 805: _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
806: _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138 paf 807: #endif
808:
1.318 moko 809: try { // global try
810: if(raw_filespec_to_process && *raw_filespec_to_process){
1.351 moko 811: filespec_to_process=full_disk_path(raw_filespec_to_process);
1.318 moko 812: }
1.10 paf 813:
1.316 moko 814: REAL_PARSER_HANDLER(cgi);
1.361 moko 815: } catch(const Exception& e) { // exception in config_handler
1.359 moko 816: SAPI::log(*sapi_info, "%s", e.comment());
817: SAPI::send_error(*sapi_info, e.comment(), strcmp(e.type(), "file.missing") ? "500" : "404");
1.16 paf 818: }
1.109 parser 819:
1.233 paf 820: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.362 moko 821: pa_log("main: successful return");
1.233 paf 822: #endif
1.358 moko 823: return sapi_info->http_response_code < 100 ? sapi_info->http_response_code : 0;
1.1 paf 824: }
E-mail: