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