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