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