Annotation of parser3/src/targets/cgi/parser3.C, revision 1.330

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

E-mail: