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

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

E-mail: