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

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

E-mail: