Annotation of parser3/src/targets/cgi/parser3.C, revision 1.353
1.27 paf 1: /** @file
2: Parser: scripting and CGI main.
3:
1.350 moko 4: Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.189 paf 6: */
1.27 paf 7:
1.353 ! moko 8: volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.352 2024/08/25 16:24:03 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.316 moko 49: static const char* filespec_to_process = 0; // [file]
50: static const char* httpd_host_port = 0; // -p option
1.319 moko 51: static const char* config_filespec = 0; // -f option or from env or next to the executable if exists
1.316 moko 52: static bool mail_received = false; // -m option? [asked to parse incoming message to $mail:received]
1.347 moko 53: static const char* parser3_filespec = 0; // argv[0]
1.319 moko 54: static char** argv_extra = NULL;
1.245 misha 55:
1.317 moko 56: // for error logging
1.341 moko 57: static THREAD_LOCAL Request_info *request_info_4log = NULL; // global for correct log() reporting
1.322 moko 58: static const char* filespec_4log = NULL; // null only if system-wide auto.p used
1.201 paf 59:
1.352 moko 60: template <typename T> static T *dir_pos(T *fname){
61: T *result=NULL;
1.353 ! moko 62: while (fname=strpbrk(fname, "/\\")){
! 63: result=fname;
! 64: fname++;
1.352 moko 65: }
1.353 ! moko 66: return result;
1.352 moko 67: }
68:
1.46 paf 69: // SAPI
1.86 parser 70:
1.335 moko 71: static void pa_log(const char* fmt, va_list args) {
1.193 paf 72: bool opened=false;
1.61 paf 73: FILE *f=0;
74:
1.226 paf 75: const char* log_by_env=getenv(PARSER_LOG_ENV_NAME);
76: if(!log_by_env)
77: log_by_env=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME);
78: if(log_by_env) {
79: f=fopen(log_by_env, "at");
80: opened=f!=0;
81: }
1.272 moko 82: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.233 paf 83: f=fopen(PA_DEBUG_CGI_ENTRY_EXIT, "at");
84: opened=f!=0;
85: #endif
1.226 paf 86:
1.316 moko 87: if(!opened && filespec_4log) {
1.352 moko 88: char log_spec[MAX_STRING + 12 /* '/parser3.log' */];
89: pa_strncpy(log_spec, filespec_4log, MAX_STRING);
90:
91: if(char *log_dir_pos=dir_pos(log_spec)){
92: strcpy(log_dir_pos, "/parser3.log");
93: } else {
1.193 paf 94: // no path, just filename
1.352 moko 95: strcpy(log_spec, "./parser3.log");
1.193 paf 96: }
97:
1.352 moko 98: f=fopen(log_spec, "at");
1.193 paf 99: opened=f!=0;
100: }
1.192 paf 101: // fallback to stderr
1.61 paf 102: if(!opened)
103: f=stderr;
1.208 paf 104:
105: // use no memory [so that we could log out-of-memory error]
106: setbuf(f, 0); // stderr stream is unbuffered by default, but still...
1.61 paf 107:
108: // prefix
109: time_t t=time(0);
1.218 paf 110: if(const char* stamp=ctime(&t)) { // never saw that
1.173 paf 111: if(size_t len=strlen(stamp)) // saw once stamp being =""
1.333 moko 112: fprintf(f, "[%.*s] [%u] ", (int)len-1, stamp, (unsigned int)pa_get_thread_id() );
1.171 paf 113: }
1.61 paf 114: // message
1.117 parser 115:
1.246 misha 116: char buf[MAX_LOG_STRING];
117: size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args);
118: size=remove_crlf(buf, buf+size);
1.239 paf 119: fwrite(buf, size, 1, f);
120:
1.341 moko 121: if(request_info_4log && request_info_4log->method) {
122: 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 123: } else
124: fputs(" [no request info]\n", f);
1.61 paf 125:
126: if(opened)
127: fclose(f);
1.85 parser 128: else
129: fflush(f);
1.124 parser 130: }
1.272 moko 131:
1.335 moko 132: void pa_log(const char* fmt, ...) {
1.272 moko 133: va_list args;
1.233 paf 134: va_start(args,fmt);
1.335 moko 135: pa_log(fmt, args);
1.233 paf 136: va_end(args);
137: }
1.124 parser 138:
1.322 moko 139: // appends to parser3.log located next to the config file if openable, to stderr otherwize
1.218 paf 140: void SAPI::log(SAPI_Info&, const char* fmt, ...) {
1.272 moko 141: va_list args;
1.124 parser 142: va_start(args,fmt);
1.335 moko 143: pa_log(fmt, args);
1.124 parser 144: va_end(args);
145: }
146:
1.290 moko 147: void SAPI::die(const char* fmt, ...) {
148: va_list args;
149:
150: // logging first, first vsnprintf
151: va_start(args,fmt);
1.335 moko 152: pa_log(fmt, args);
1.290 moko 153: va_end(args);
1.138 paf 154:
1.290 moko 155: // inform user, second vsnprintf
156: va_start(args, fmt);
1.299 moko 157: char message[MAX_STRING];
1.300 moko 158: vsnprintf(message, MAX_STRING, fmt, args);
1.134 paf 159:
1.299 moko 160: SAPI::send_error(*sapiInfo, message);
1.290 moko 161: exit(1);
1.258 moko 162: // va_end(args);
1.28 paf 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.294 moko 177: size_t SAPI::read_post(SAPI_Info& info, char *buf, size_t max_bytes) {
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) {
182: info.add_header_attribute(dont_store_key, dont_store_value);
1.19 paf 183: }
184:
1.294 moko 185: void SAPI::send_header(SAPI_Info& info) {
186: info.send_header();
1.30 paf 187: }
1.20 paf 188:
1.294 moko 189: size_t SAPI::send_body(SAPI_Info& info, const void *buf, size_t size) {
190: return info.send_body(buf, size);
1.58 paf 191: }
192:
1.352 moko 193: static const char *full_disk_path(const char* file_name = "") {
1.351 moko 194: char *result;
195: if(file_name[0]=='/'
1.167 paf 196: #ifdef WIN32
1.308 moko 197: || file_name[0] && file_name[1]==':'
1.167 paf 198: #endif
1.308 moko 199: ){
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.294 moko 212: SAPI::log(*sapiInfo, "%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.321 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.352 moko 238: const char *exec_dir_pos = dir_pos(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=='.')){
! 244: // just parser3.exe or ./parser3.cgi, full path to avoid "parser already configured"
! 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.292 moko 264: 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:
296: #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) maybe_reconstruct_IIS_status_in_qs(s)
1.342 moko 297: #else
1.283 moko 298: #define MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(s) s
1.227 paf 299: #endif
300:
1.256 misha 301: class RequestController {
302: public:
303: RequestController(Request* r){
1.341 moko 304: request=r;
1.256 misha 305: }
306: ~RequestController(){
1.341 moko 307: request=0;
308: }
309: };
310:
311: class RequestInfoController {
312: public:
313: RequestInfoController(Request_info* rinfo){
314: request_info_4log=rinfo;
315: }
316: ~RequestInfoController(){
317: request_info_4log=0;
1.256 misha 318: }
319: };
320:
1.342 moko 321: /** httpd support */
322: static const String httpd_class_name("httpd");
323:
1.317 moko 324: static void config_handler(SAPI_Info &info) {
1.341 moko 325: Request_info request_info;
326: RequestInfoController ric(&request_info);
327:
1.351 moko 328: request_info.document_root = full_disk_path();
1.317 moko 329: request_info.uri = "";
1.319 moko 330: request_info.argv = argv_extra;
1.317 moko 331:
332: // prepare to process request
1.323 moko 333: Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
1.330 moko 334: // only once
335: config_filespec = locate_config(config_filespec, parser3_filespec);
336: // process main auto.p only
337: r.core(config_filespec, false, String::Empty);
1.317 moko 338: }
339:
340: static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection) {
1.341 moko 341: Request_info request_info;
342: RequestInfoController ric(&request_info);
343:
1.325 moko 344: try {
1.336 moko 345: if(!connection.read_header())
1.332 moko 346: return; // ignore "void" connections
1.325 moko 347: info.populate_env();
348:
1.351 moko 349: request_info.document_root = full_disk_path();
1.325 moko 350: request_info.path_translated = filespec_to_process;
351: request_info.method = connection.method();
352: request_info.query_string = connection.query();
353: request_info.uri = request_info.strip_absolute_uri(connection.uri());
354: request_info.content_type = connection.content_type();
355: request_info.content_length = (size_t)connection.content_length();
356: request_info.cookie = info.get_env("HTTP_COOKIE");
357: request_info.mail_received = false;
358: request_info.argv = argv_extra;
1.294 moko 359:
1.325 moko 360: // prepare to process request
361: Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
1.330 moko 362: // process the request
1.342 moko 363: r.core(config_filespec, strcasecmp(request_info.method, "HEAD")==0, main_method_name, &httpd_class_name);
1.325 moko 364: } catch(const Exception& e) { // exception in connection handling or unhandled exception
365: SAPI::log(info, "%s", e.comment());
1.337 moko 366: const char *status = info.exception_http_status(e.type());
1.338 moko 367: if(*status){
368: info.clear_response_headers();
1.337 moko 369: SAPI::send_error(info, e.comment(), status);
1.338 moko 370: }
1.325 moko 371: }
372: }
1.317 moko 373:
1.328 moko 374: #ifdef _MSC_VER
375: DWORD WINAPI connection_thread(void *arg){
376: #else
1.325 moko 377: static void *connection_thread(void *arg){
1.328 moko 378: #endif
1.325 moko 379: HTTPD_Connection &connection=*(HTTPD_Connection*)arg;
380: SAPI_Info_HTTPD info(connection);
1.294 moko 381:
1.325 moko 382: try {
383: connection_handler(info, connection);
384: } catch(const Exception& e) { // exception in send_error
385: SAPI::log(*sapiInfo, "%s", e.comment());
1.294 moko 386: }
1.325 moko 387:
388: delete(&connection);
1.328 moko 389: return 0;
1.294 moko 390: }
391:
1.317 moko 392: static void httpd_mode() {
1.325 moko 393: config_handler(*sapiInfo);
394:
1.346 moko 395: SOCKET sock = HTTPD_Server::bind(httpd_host_port);
1.294 moko 396:
1.327 moko 397: #ifdef SIGPIPE
1.325 moko 398: signal(SIGPIPE, SIG_IGN);
399: #endif
1.317 moko 400:
1.297 moko 401: while(1){
1.328 moko 402: #ifndef _MSC_VER
1.325 moko 403: pid_t pid=1;
1.343 moko 404: if(HTTPD_Server::mode == HTTPD_Server::PARALLEL)
405: while (waitpid((pid_t)(-1), 0, WNOHANG) > 0);
1.328 moko 406: #endif
1.303 moko 407: try {
408: HTTPD_Connection connection;
1.345 moko 409: if(!connection.accept(sock, 500))
1.303 moko 410: continue;
411:
1.325 moko 412: switch (HTTPD_Server::mode) {
413: case HTTPD_Server::MULTITHREADED:
1.328 moko 414: #ifdef _MSC_VER
415: if (!GC_CreateThread(0, 0, connection_thread, new HTTPD_Connection(connection), 0, 0))
416: throw Exception("httpd.fork", 0, "thread creation failed");
1.346 moko 417: connection.sock=INVALID_SOCKET;
1.328 moko 418: break;
419: #else
1.339 moko 420: #ifdef HAVE_TLS
1.325 moko 421: pthread_t thread;
422: pthread_attr_t attr;
423: pthread_attr_init(&attr);
424: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
425:
426: if(int result=GC_pthread_create(&thread, &attr, connection_thread, new HTTPD_Connection(connection)))
427: throw Exception("httpd.fork", 0, "thread creation failed (%d)", result);
1.346 moko 428: connection.sock=INVALID_SOCKET;
1.325 moko 429: break;
1.339 moko 430: #endif
1.325 moko 431: case HTTPD_Server::PARALLEL:
432: pid=fork();
433: if(pid<0)
434: throw Exception("httpd.fork", 0, "fork failed: %s (%d)", strerror(errno), errno);
435: if(pid>0)
436: continue; // parent should close connection.sock as well
437: #endif
438: case HTTPD_Server::SEQUENTIAL: // and fork child
1.303 moko 439:
1.325 moko 440: SAPI_Info_HTTPD info(connection);
441: connection_handler(info, connection);
1.303 moko 442: }
443: // closing connection socket in HTTPD_Connection destructor
444: } catch(const Exception& e) { // exception in accept or send_error
445: SAPI::log(*sapiInfo, "%s", e.comment());
1.294 moko 446: }
1.325 moko 447:
1.328 moko 448: #ifndef _MSC_VER
1.325 moko 449: if(pid==0) // fork child
450: exit(0);
1.328 moko 451: #endif
1.294 moko 452: }
453: }
1.231 paf 454:
1.294 moko 455: /** main workhorse */
456:
1.316 moko 457: static void real_parser_handler(bool cgi) {
1.280 moko 458: // init libraries
1.218 paf 459: pa_globals_init();
1.294 moko 460:
1.296 moko 461: if(httpd_host_port){
1.308 moko 462: httpd_mode();
1.294 moko 463: }
464:
465: const char* request_method=getenv("REQUEST_METHOD");
466:
1.308 moko 467: if(!filespec_to_process)
1.282 moko 468: SAPI::die("Parser/%s", PARSER_VERSION);
1.122 parser 469:
1.166 paf 470: char document_root_buf[MAX_STRING];
1.283 moko 471:
1.297 moko 472: // global request info
1.341 moko 473: Request_info request_info;
474: RequestInfoController ric(&request_info);
475:
1.283 moko 476: request_info.path_translated = filespec_to_process;
477: request_info.method = request_method ? request_method : "GET";
478: request_info.query_string = MAYBE_RECONSTRUCT_IIS_STATUS_IN_QS(getenv("QUERY_STRING"));
479:
1.122 parser 480: if(cgi) {
1.284 moko 481: // obligatory
1.218 paf 482: const char* path_info=getenv("PATH_INFO");
1.214 paf 483: if(!path_info)
1.349 moko 484: SAPI::die("parser3: illegal CGI call (missing PATH_INFO)");
1.283 moko 485:
486: request_info.document_root = getenv("DOCUMENT_ROOT");
487: if(!request_info.document_root) {
1.285 moko 488: // IIS or fcgiwrap minimalistic setup
489: ssize_t prefix_len = strlen(filespec_to_process) - strlen(path_info);
490: if(prefix_len < 0 || strcmp(filespec_to_process + prefix_len, path_info) != 0)
1.349 moko 491: SAPI::die("parser3: illegal CGI call (invalid PATH_INFO in reinventing DOCUMENT_ROOT)");
1.285 moko 492:
493: char* document_root = new(PointerFreeGC) char[prefix_len + 1/*0*/];
494: memcpy(document_root, filespec_to_process, prefix_len); document_root[prefix_len] = 0;
495: request_info.document_root = document_root;
1.283 moko 496: }
1.214 paf 497:
1.285 moko 498: request_info.uri = request_info.strip_absolute_uri(getenv("REQUEST_URI"));
1.283 moko 499: if(request_info.uri) { // apache & others stuck to standards
1.284 moko 500: // another obligatory
1.285 moko 501: const char* script_name = getenv("SCRIPT_NAME");
1.284 moko 502: if(!script_name)
1.349 moko 503: SAPI::die("parser3: illegal CGI call (missing SCRIPT_NAME)");
1.214 paf 504: /*
505: http://parser3/env.html?123 =OK
506: $request:uri=/env.html?123
507: REQUEST_URI='/env.html?123'
508: SCRIPT_NAME='/cgi-bin/parser3'
509: PATH_INFO='/env.html'
1.285 moko 510:
1.214 paf 511: http://parser3/cgi-bin/parser3/env.html?123 =ERROR
512: $request:uri=/cgi-bin/parser3/env.html?123
513: REQUEST_URI='/cgi-bin/parser3/env.html?123'
514: SCRIPT_NAME='/cgi-bin/parser3'
515: PATH_INFO='/env.html'
516: */
1.285 moko 517: size_t script_name_len = strlen(script_name);
518: size_t uri_len = strlen(request_info.uri);
1.283 moko 519: 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 520: SAPI::die("parser3: illegal CGI call (REQUEST_URI starts with SCRIPT_NAME)");
1.284 moko 521: } else { // fcgiwrap minimalistic setup
1.315 moko 522: request_info.uri = request_info.query_string && *request_info.query_string ? pa_strcat(path_info, "?", request_info.query_string) : path_info;
1.214 paf 523: }
1.283 moko 524: } else{
1.351 moko 525: request_info.document_root = full_disk_path();
1.285 moko 526: request_info.uri = "";
1.283 moko 527: }
1.122 parser 528:
1.283 moko 529: request_info.content_type = getenv("CONTENT_TYPE");
1.348 moko 530: request_info.content_length = cgi ? (size_t)pa_atoul(getenv("CONTENT_LENGTH")) : 0; // only SAPI_Info_CGI can read POST
1.283 moko 531: request_info.cookie = getenv("HTTP_COOKIE");
532: request_info.mail_received = mail_received;
1.184 paf 533:
1.319 moko 534: request_info.argv = argv_extra;
1.245 misha 535:
1.233 paf 536: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.320 moko 537: 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 538: #endif
539:
1.122 parser 540: // prepare to process request
1.323 moko 541: Request r(*sapiInfo, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS);
1.256 misha 542: {
1.297 moko 543: // initing ::request ptr for signal handlers
1.323 moko 544: RequestController rc(&r);
1.256 misha 545: // process the request
1.323 moko 546: r.core(locate_config(config_filespec, parser3_filespec), strcasecmp(request_info.method, "HEAD")==0);
1.303 moko 547: // clearing ::request in RequestController destructor to prevent signal handlers from accessing invalid memory
1.122 parser 548: }
1.231 paf 549:
1.280 moko 550: // finalize libraries
1.225 paf 551: pa_globals_done();
1.218 paf 552: }
553:
1.231 paf 554: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
1.316 moko 555: static const Exception call_real_parser_handler__do_PEH_return_it(bool cgi) {
1.231 paf 556: try {
1.316 moko 557: real_parser_handler(cgi);
1.231 paf 558: } catch(const Exception& e) {
559: return e;
1.122 parser 560: }
1.231 paf 561:
562: return Exception();
1.122 parser 563: }
1.272 moko 564:
1.316 moko 565: static void call_real_parser_handler__supress_system_exception(bool cgi) {
1.231 paf 566: Exception parser_exception;
567: LPEXCEPTION_POINTERS system_exception=0;
1.122 parser 568:
1.231 paf 569: __try {
1.316 moko 570: parser_exception=call_real_parser_handler__do_PEH_return_it(cgi);
1.288 moko 571: } __except ( (system_exception=GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {
1.231 paf 572: if(system_exception)
573: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.272 moko 574: throw Exception("system", 0, "0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
1.218 paf 575: else
1.272 moko 576: throw Exception("system", 0, "<no exception record>");
1.218 paf 577: else
1.272 moko 578: throw Exception("system", 0, "<no exception information>");
1.231 paf 579: }
580:
581: if(parser_exception)
582: throw Exception(parser_exception);
583: }
1.273 moko 584:
585: #define REAL_PARSER_HANDLER call_real_parser_handler__supress_system_exception
586: #else
587: #define REAL_PARSER_HANDLER real_parser_handler
1.218 paf 588: #endif
1.131 paf 589:
1.218 paf 590: static void usage(const char* program) {
1.188 paf 591: printf(
1.235 paf 592: "Parser/%s\n"
1.350 moko 593: "Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)\n"
594: "Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>\n"
1.184 paf 595: "\n"
1.307 moko 596: "Usage: %s [options] [file]\n"
1.184 paf 597: "Options are:\n"
1.185 paf 598: #ifdef WITH_MAILRECEIVE
1.193 paf 599: " -m Parse mail, put received letter to $mail:received\n"
1.184 paf 600: #endif
1.193 paf 601: " -f config_file Use this config file (/path/to/auto.p)\n"
1.296 moko 602: " -p [host:]port Start web server on this port\n"
1.272 moko 603: " -h Display usage information (this message)\n",
604: PARSER_VERSION,
1.184 paf 605: program);
606: exit(EINVAL);
607: }
608:
1.294 moko 609:
1.249 misha 610: int main(int argc, char *argv[]) {
1.233 paf 611: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
612: log("main: entry");
613: #endif
1.217 paf 614:
1.352 moko 615: parser3_filespec = argc && argv[0] ? argv[0] : "parser3";
1.294 moko 616: umask(2);
617:
618: // were we started as CGI?
1.316 moko 619: bool cgi=(getenv("SERVER_SOFTWARE") || getenv("SERVER_NAME") || getenv("GATEWAY_INTERFACE") || getenv("REQUEST_METHOD")) && !getenv("PARSER_VERSION");
1.294 moko 620: sapiInfo = cgi ? new SAPI_Info_CGI() : new SAPI_Info();
621:
1.203 paf 622: #ifdef SIGPIPE
1.325 moko 623: signal(SIGPIPE, SIGPIPE_handler);
1.203 paf 624: #endif
625:
1.301 moko 626: char *raw_filespec_to_process = NULL;
1.210 paf 627: if(cgi) {
1.184 paf 628: raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.319 moko 629: argv_extra=argv + 1;
1.210 paf 630: } else {
1.250 misha 631: int optind=1;
1.245 misha 632: while(optind < argc){
633: char *carg = argv[optind];
634: if(carg[0] != '-')
1.184 paf 635: break;
1.245 misha 636:
637: for(size_t k = 1; k < strlen(carg); k++){
638: char c = carg[k];
639: switch (c) {
640: case 'h':
1.347 moko 641: usage(parser3_filespec);
1.245 misha 642: break;
643: case 'f':
644: if(optind < argc - 1){
645: optind++;
1.311 moko 646: config_filespec=argv[optind];
1.245 misha 647: }
648: break;
1.294 moko 649: case 'p':
650: if(optind < argc - 1){
651: optind++;
1.296 moko 652: httpd_host_port=argv[optind];
1.294 moko 653: }
654: break;
1.185 paf 655: #ifdef WITH_MAILRECEIVE
1.245 misha 656: case 'm':
657: mail_received=true;
658: break;
659: #endif
660: default:
1.347 moko 661: fprintf(stderr, "%s: invalid option '%c'\n", parser3_filespec, c);
662: usage(parser3_filespec);
1.245 misha 663: break;
664: }
1.184 paf 665: }
1.245 misha 666: optind++;
1.184 paf 667: }
1.245 misha 668:
669: if (optind > argc - 1) {
1.296 moko 670: if(!httpd_host_port) {
1.347 moko 671: fprintf(stderr, "%s: file not specified\n", parser3_filespec);
672: usage(parser3_filespec);
1.294 moko 673: }
674: } else {
675: raw_filespec_to_process=argv[optind];
1.10 paf 676: }
1.294 moko 677:
1.296 moko 678: if (httpd_host_port && mail_received) {
1.347 moko 679: fprintf(stderr, "%s: -p and -m options should not be used together\n", parser3_filespec);
680: usage(parser3_filespec);
1.294 moko 681: }
1.310 moko 682:
1.319 moko 683: argv_extra=argv + optind;
1.10 paf 684: }
685:
1.264 moko 686: #ifdef _MSC_VER
1.100 parser 687: setmode(fileno(stdin), _O_BINARY);
688: setmode(fileno(stdout), _O_BINARY);
689: setmode(fileno(stderr), _O_BINARY);
690: #endif
691:
1.218 paf 692: #if defined(_MSC_VER) && defined(_DEBUG)
1.148 paf 693: // Get current flag
694: int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
695:
696: // Turn on leak-checking bit
697: tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
698:
699: // Set flag to the new value
700: _CrtSetDbgFlag( tmpFlag );
1.138 paf 701:
1.218 paf 702: _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
703: _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138 paf 704: #endif
705:
1.318 moko 706: try { // global try
707: if(raw_filespec_to_process && *raw_filespec_to_process){
1.351 moko 708: filespec_to_process=full_disk_path(raw_filespec_to_process);
1.318 moko 709: }
1.10 paf 710:
1.316 moko 711: REAL_PARSER_HANDLER(cgi);
1.292 moko 712: } catch(const Exception& e) { // exception in unhandled exception
1.298 moko 713: SAPI::die("%s", e.comment());
1.16 paf 714: }
1.109 parser 715:
1.233 paf 716: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
717: log("main: successful return");
718: #endif
1.299 moko 719: return sapiInfo->http_response_code < 100 ? sapiInfo->http_response_code : 0;
1.1 paf 720: }
E-mail: