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

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.216.2.1  paf         4:        Copyright(c) 2001-2003 ArtLebedev Group (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.216.2.30! paf         8: static const char* IDENT_PARSER3_C="$Date: 2003/03/13 12:34:21 $";
1.1       paf         9: 
1.40      paf        10: #include "pa_config_includes.h"
1.3       paf        11: 
1.139     paf        12: #if _MSC_VER
1.148     paf        13: #      include <crtdbg.h>
1.3       paf        14: #endif
1.27      paf        15: 
1.37      paf        16: #include "pa_sapi.h"
1.76      paf        17: #include "classes.h"
1.24      paf        18: #include "pa_common.h"
1.2       paf        19: #include "pa_request.h"
1.57      paf        20: #include "pa_socks.h"
1.68      paf        21: #include "pa_version.h"
1.207     paf        22: 
1.216.2.30! paf        23: 
        !            24: 
1.149     paf        25: #ifdef WIN32
                     26: #      include <windows.h>
1.184     paf        27: #      include "getopt.h"
                     28: #else
                     29: #      include <getopt.h>
1.120     parser     30: #endif
                     31: 
1.158     paf        32: //#define DEBUG_POOL_MALLOC
1.196     paf        33: //#define DEBUG_MAILRECEIVE "mailreceive.eml"
1.160     paf        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.159     paf        39: 
1.42      paf        40: /// IIS refuses to read bigger chunks
                     41: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M 
                     42: 
1.216.2.1  paf        43: static const char* argv0;
                     44: static const char* config_filespec_cstr=0;
1.193     paf        45: static bool fail_on_config_read_problem=true;
1.192     paf        46: 
1.216.2.15  paf        47: static bool force_header_output=false;
1.184     paf        48: static bool cgi; ///< we were started as CGI?
                     49: static bool mail_received=false; ///< we were started with -m option? [asked to parse incoming message to $mail:received]
1.5       paf        50: 
1.201     paf        51: // for signal handlers
                     52: Request *request=0;
1.216.2.26  paf        53: Request_info *request_info=0;
                     54: bool execution_canceled=false;
1.201     paf        55: 
1.46      paf        56: // SAPI
1.86      parser     57: 
1.216.2.3  paf        58: class SAPI_Info{} SAPI_info;
                     59: 
1.216.2.1  paf        60: static void log(const char* fmt, va_list args) {
1.193     paf        61:        bool opened=false;
1.61      paf        62:        FILE *f=0;
                     63: 
1.193     paf        64:        if(config_filespec_cstr) {
                     65:                char beside_config_path[MAX_STRING];
                     66:                strncpy(beside_config_path, config_filespec_cstr, MAX_STRING-1);  beside_config_path[MAX_STRING-1]=0;
                     67:                if(!(
                     68:                        rsplit(beside_config_path, '/') || 
                     69:                        rsplit(beside_config_path, '\\'))) { // strip filename
                     70:                        // no path, just filename
                     71:                        beside_config_path[0]='.'; beside_config_path[1]=0;
                     72:                }
                     73: 
                     74:                char file_spec[MAX_STRING];
                     75:                snprintf(file_spec, MAX_STRING, 
                     76:                        "%s/parser3.log", beside_config_path);
                     77:                f=fopen(file_spec, "at");
                     78:                opened=f!=0;
                     79:        }
1.192     paf        80:        // fallback to stderr
1.61      paf        81:        if(!opened)
                     82:                f=stderr;
1.208     paf        83: 
                     84:        // use no memory [so that we could log out-of-memory error]
                     85:        setbuf(f, 0); // stderr stream is unbuffered by default, but still...
1.61      paf        86: 
                     87:        // prefix
                     88:        time_t t=time(0);
1.216.2.1  paf        89:        if(const char* stamp=ctime(&t)) { // never saw that
1.173     paf        90:                if(size_t len=strlen(stamp)) // saw once stamp being =""
1.172     paf        91:                        fprintf(f, "[%.*s] ", len-1, stamp);
1.171     paf        92:        }
1.61      paf        93:        // message
1.117     parser     94: 
                     95:        char buf[MAX_STRING];
                     96:        size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
                     97:        remove_crlf(buf, buf+size);
                     98: 
                     99:        fwrite(buf, size, 1, f);
1.61      paf       100:        // newline
                    101:        fprintf(f, "\n");
                    102: 
                    103:        if(opened)
                    104:                fclose(f);
1.85      parser    105:        else
                    106:                fflush(f);
1.124     parser    107: }
                    108: 
                    109: // appends to parser3.log located beside my binary if openable, to stderr otherwize
1.216.2.3  paf       110: void SAPI::log(SAPI_Info&, const char* fmt, ...) {
1.124     parser    111:     va_list args;
                    112:        va_start(args,fmt);
                    113:        ::log(fmt, args);
                    114:        va_end(args);
                    115: }
                    116: 
1.216.2.18  paf       117: static void die_or_abort(const char* fmt, va_list args, bool write_core) {
1.216.2.11  paf       118:        //_asm int 3;
1.137     paf       119: #ifdef DEBUG_POOL_MALLOC
                    120:        extern void log_pool_stats(Pool& pool);
1.216.2.3  paf       121:        log_pool_stats(SAPI_info);
1.137     paf       122: #endif
                    123: 
1.138     paf       124:        // log
                    125: 
                    126:        // logging is more important than user 
1.204     paf       127:        // she can cancel download, we'd get SIGPIPE, 
1.138     paf       128:        // nothing would be logged then
1.124     parser    129:        ::log(fmt, args);
                    130: 
1.138     paf       131:        // inform user
                    132: 
1.134     paf       133:        char body[MAX_STRING];
1.138     paf       134:        int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134     paf       135: 
                    136:        // prepare header
                    137:        // let's be honest, that's bad we couldn't produce valid output
1.216.2.3  paf       138:        SAPI::add_header_attribute(SAPI_info, "status", "500");
                    139:        SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
1.134     paf       140:        char content_length_cstr[MAX_NUMBER];
1.168     paf       141:        snprintf(content_length_cstr, sizeof(content_length_cstr), "%u", content_length);
1.216.2.3  paf       142:        SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
1.134     paf       143: 
                    144:        // send header
1.216.2.3  paf       145:        SAPI::send_header(SAPI_info);
1.134     paf       146: 
                    147:        // body
1.216.2.3  paf       148:        SAPI::send_body(SAPI_info, body, content_length);
1.134     paf       149: 
1.216.2.18  paf       150:        // exit & try to produce core dump[unix] or invoke debugger[Win32 Debug version]
                    151:        if(write_core) {
                    152: #if defined(WIN32) && !defined(_DEBUG)
                    153:                //_asm int 3;
                    154:                // IIS with abort failes to show STDOUT, it just barks "abnormal program termination"
                    155:                exit(1);
1.214     paf       156: #else
1.216.2.18  paf       157:                abort();
1.214     paf       158: #endif
1.216.2.18  paf       159:        } 
                    160:        else
                    161:                exit(1);
                    162: }
                    163: 
                    164: void SAPI::die(const char* fmt, ...) {
                    165:     va_list args;
                    166:        va_start(args, fmt);
                    167:        die_or_abort(fmt, args, false/*write core?*/);
                    168:        va_end(args);
                    169: }
                    170: 
                    171: void SAPI::abort(const char* fmt, ...) {
                    172:     va_list args;
                    173:        va_start(args, fmt);
                    174:        die_or_abort(fmt, args, true/*write core?*/);
                    175:        va_end(args);
1.61      paf       176: }
                    177: 
1.216.2.17  paf       178: char* SAPI::get_env(Pool& pool, SAPI_Info& , const char* name) {
                    179:        if(char *local=getenv(name))
                    180:                return pool.copy(local);
1.216.2.3  paf       181:        else
1.216.2.17  paf       182:                return 0;
1.28      paf       183: }
                    184: 
1.216.2.3  paf       185: const char* const *SAPI::environment(SAPI_Info&, Pool&) {
1.180     paf       186: #ifdef _MSC_VER
                    187:        extern char **_environ;
                    188:        return _environ;
                    189: #else
                    190:        extern char **environ;
                    191:        return environ;
                    192: #endif
                    193: }
                    194: 
1.216.2.3  paf       195: size_t SAPI::read_post(SAPI_Info& , char *buf, size_t max_bytes) {
1.59      paf       196:        size_t read_size=0;
1.12      paf       197:        do {
1.200     paf       198:                ssize_t chunk_size=read(fileno(stdin), 
1.42      paf       199:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129     paf       200:                if(chunk_size<=0)
1.12      paf       201:                        break;
                    202:                read_size+=chunk_size;
                    203:        } while(read_size<max_bytes);
                    204: 
                    205:        return read_size;
1.10      paf       206: }
                    207: 
1.216.2.3  paf       208: void SAPI::add_header_attribute(SAPI_Info& , const char* dont_store_key, const char* dont_store_value) {
1.216.2.15  paf       209:        if(cgi || force_header_output)
1.216.2.2  paf       210:                printf("%s: %s\n", dont_store_key, dont_store_value);
1.19      paf       211: }
                    212: 
1.56      paf       213: /// @todo intelligent cache-control
1.216.2.3  paf       214: void SAPI::send_header(SAPI_Info& ) {
1.216.2.15  paf       215:        if(cgi || force_header_output) {
1.147     paf       216: //             puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       217: 
                    218:                // header | body  delimiter
1.20      paf       219:                puts("");
1.33      paf       220:        }
1.30      paf       221: }
1.20      paf       222: 
1.216.2.3  paf       223: void SAPI::send_body(SAPI_Info& , const void *buf, size_t size) {
1.19      paf       224:        stdout_write(buf, size);
1.58      paf       225: }
                    226: 
1.97      parser    227: //
                    228: 
1.216.2.1  paf       229: static void full_file_spec(const char* file_name, char *buf, size_t buf_size) {
1.210     paf       230:        if(file_name)
1.167     paf       231:                if(file_name[0]=='/' 
                    232: #ifdef WIN32
1.210     paf       233:                        || file_name[0] && file_name[1]==':'
1.167     paf       234: #endif
1.210     paf       235:                        )
1.167     paf       236:                        strncpy(buf, file_name, buf_size);
                    237:                else {
                    238:                        char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    239:                        snprintf(buf, buf_size, "%s/%s", cwd, file_name);
                    240:                }
                    241:        else
                    242:                buf[0]=0;
1.166     paf       243: #ifdef WIN32
                    244:        back_slashes_to_slashes(buf);
                    245: #endif
1.97      parser    246: }
                    247: 
1.216.2.1  paf       248: static void log_signal(const char* signal_name) {
1.216.2.26  paf       249:        if(request_info)
                    250:                SAPI::log(SAPI_info, "%s received while %s. uri=%s, method=%s, qs=%s, cl=%u",
                    251:                        signal_name,
                    252:                        request?"executing code":"reading data",
                    253:                        request_info->uri,
                    254:                        request_info->method,
                    255:                        request_info->query_string,
                    256:                        request_info->content_length);
                    257:        else
                    258:                SAPI::log(SAPI_info, "%s received before or after processing request",
                    259:                        signal_name);
1.205     paf       260: }
                    261: 
1.201     paf       262: #ifdef SIGUSR1
1.205     paf       263: static void SIGUSR1_handler(int /*sig*/){
                    264:        log_signal("SIGUSR1");
1.201     paf       265: }
                    266: #endif
                    267: 
                    268: #ifdef SIGPIPE
1.205     paf       269: static void SIGPIPE_handler(int /*sig*/){
                    270:        log_signal("SIGPIPE");
1.216.2.26  paf       271:        execution_canceled=true;
1.201     paf       272:        if(request)
1.216.2.27  paf       273:                request->set_interrupted(true);
1.201     paf       274: }
                    275: #endif
                    276: 
1.40      paf       277: /**
1.122     parser    278: main workhorse
1.19      paf       279: 
1.122     parser    280:   @todo 
1.40      paf       281:                IIS: remove trailing default-document[index.html] from $request.uri.
                    282:                to do that we need to consult metabase,
                    283:                wich is tested but seems slow.
                    284: */
1.184     paf       285: static void real_parser_handler(
1.216.2.1  paf       286:                                        const char* filespec_to_process,
                    287:                                        const char* request_method, bool header_only) {
1.122     parser    288:        // init socks
1.216.2.3  paf       289:        pa_init_socks();
1.122     parser    290:        
                    291:        // init global variables
1.216.2.3  paf       292:        pa_globals_init();
1.122     parser    293:        
1.198     paf       294:        // request pool, must be different ptr from global [used in VStateless_class.add_method]
1.216.2.3  paf       295:        Pool request_pool;
1.198     paf       296: 
1.186     paf       297:        if(!filespec_to_process || !*filespec_to_process)
1.144     paf       298:                SAPI::die("Parser/%s", PARSER_VERSION);
1.122     parser    299:        
                    300:        // Request info
1.216.2.3  paf       301:        Request_info request_info;
1.166     paf       302:        char document_root_buf[MAX_STRING];
1.122     parser    303:        if(cgi) {
1.216.2.17  paf       304:                if(const char* env_document_root=getenv("DOCUMENT_ROOT"))
1.122     parser    305:                        request_info.document_root=env_document_root;
1.216.2.17  paf       306:                else if(const char* path_info=getenv("PATH_INFO")) {
1.122     parser    307:                        // IIS
1.166     paf       308:                        size_t len=min(sizeof(document_root_buf)-1, strlen(filespec_to_process)-strlen(path_info));
                    309:                        memcpy(document_root_buf, filespec_to_process, len); document_root_buf[len]=0;
                    310:                        request_info.document_root=document_root_buf;
1.122     parser    311:                } else
1.165     paf       312:                        throw Exception("parser.runtime",
1.216.2.3  paf       313:                                Exception::undefined_source,
1.165     paf       314:                                "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.122     parser    315:        } else {
1.166     paf       316:                full_file_spec("", document_root_buf, sizeof(document_root_buf));
                    317:                request_info.document_root=document_root_buf;
1.122     parser    318:        }
                    319:        request_info.path_translated=filespec_to_process;
                    320:        request_info.method=request_method ? request_method : "GET";
1.216.2.17  paf       321:        const char* query_string=getenv("QUERY_STRING");
1.122     parser    322:        request_info.query_string=query_string;
                    323:        if(cgi) {
1.214     paf       324:                // few absolute obligatory
1.216.2.17  paf       325:                const char* path_info=getenv("PATH_INFO");
1.214     paf       326:                if(!path_info)
                    327:                        SAPI::die("CGI: illegal call (missing PATH_INFO)");
1.216.2.17  paf       328:                const char* script_name=getenv("SCRIPT_NAME");
1.214     paf       329:                if(!script_name)
                    330:                                SAPI::die("CGI: illegal call (missing SCRIPT_NAME)");
                    331: 
1.216.2.17  paf       332:                const char* env_request_uri=getenv("REQUEST_URI");
1.214     paf       333:                if(env_request_uri)
1.122     parser    334:                        request_info.uri=env_request_uri;
1.214     paf       335:                else 
1.122     parser    336:                        if(query_string) {
1.216.2.17  paf       337:                                CharPtr reconstructed_uri(new char[
1.122     parser    338:                                        strlen(path_info)+1/*'?'*/+
1.216.2.17  paf       339:                                        strlen(query_string)+1/*0*/]);
1.122     parser    340:                                strcpy(reconstructed_uri, path_info);
                    341:                                strcat(reconstructed_uri, "?");
                    342:                                strcat(reconstructed_uri, query_string);
                    343:                                request_info.uri=reconstructed_uri;
                    344:                        } else
                    345:                                request_info.uri=path_info;
1.214     paf       346: 
                    347:                if(env_request_uri) { // apache & others stuck to standards
                    348:                        /*
                    349:                                http://parser3/env.html?123  =OK
                    350:                                $request:uri=/env.html?123
                    351:                                REQUEST_URI='/env.html?123'
                    352:                                SCRIPT_NAME='/cgi-bin/parser3'
                    353:                                PATH_INFO='/env.html'
                    354: 
                    355:                                http://parser3/cgi-bin/parser3/env.html?123 =ERROR
                    356:                                $request:uri=/cgi-bin/parser3/env.html?123
                    357:                                REQUEST_URI='/cgi-bin/parser3/env.html?123'
                    358:                                SCRIPT_NAME='/cgi-bin/parser3'
                    359:                                PATH_INFO='/env.html'
                    360:                        */
                    361:                        size_t script_name_len=strlen(script_name);
                    362:                        size_t uri_len=strlen(env_request_uri);
                    363:                        if(strncmp(env_request_uri, script_name, script_name_len)==0 &&
                    364:                                script_name_len != uri_len) // under IIS they are the same
                    365:                                SAPI::die("CGI: illegal call (1)");
                    366:                } else { // seen on IIS5
                    367:                        /*
                    368:                                http://nestle/env.html?123 =OK
                    369:                                $request:uri=/env.html?123
                    370:                                REQUEST_URI=''
                    371:                                SCRIPT_NAME='/env.html'
                    372:                                PATH_INFO='/env.html'
                    373: 
                    374:                                http://nestle/cgi-bin/parser3.exe/env.html =ERROR
                    375:                                $request:uri=/env.html
                    376:                                REQUEST_URI=''
                    377:                                SCRIPT_NAME='/cgi-bin/parser3.exe'
                    378:                                PATH_INFO='/env.html'
                    379:                        */
                    380:                        if(strcmp(script_name, path_info)!=0)
                    381:                                SAPI::die("CGI: illegal call (2)");
                    382:                }
1.122     parser    383:        } else
1.177     paf       384:                request_info.uri="";
1.122     parser    385:        
1.216.2.17  paf       386:        request_info.content_type=getenv("CONTENT_TYPE");
                    387:        const char* content_length=getenv("CONTENT_LENGTH");
1.122     parser    388:        request_info.content_length=(content_length?atoi(content_length):0);
1.216.2.17  paf       389:        request_info.cookie=getenv("HTTP_COOKIE");
1.184     paf       390:        request_info.mail_received=mail_received;
                    391: 
1.216.2.26  paf       392:        // get request_info ptr for signal handlers
                    393:        ::request_info=&request_info;
                    394:        if(execution_canceled)
                    395:                SAPI::die("Execution canceled");
1.198     paf       396: 
1.122     parser    397:        // prepare to process request
1.216.2.4  paf       398:        Request request(request_pool, SAPI_info, 
1.122     parser    399:                request_info,
1.184     paf       400: /*#ifdef _DEBUG
1.143     paf       401:                String::UL_HTML|String::UL_OPTIMIZE_BIT
1.184     paf       402: #else*/
1.143     paf       403:                cgi ? String::UL_HTML|String::UL_OPTIMIZE_BIT : String::UL_AS_IS
1.184     paf       404: /*#endif*/
1.143     paf       405:                ,
1.130     paf       406:                true /* status_allowed */);
1.201     paf       407: 
                    408:        // get request ptr for signal handlers
                    409:        ::request=&request;
                    410: 
1.181     paf       411:        char config_filespec_buf[MAX_STRING];
1.193     paf       412:        if(!config_filespec_cstr) {
1.216.2.1  paf       413:                const char* config_by_env=getenv(PARSER_CONFIG_ENV_NAME);
1.193     paf       414:                if(!config_by_env)
                    415:                        config_by_env=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
                    416:                if(config_by_env)
                    417:                        config_filespec_cstr=config_by_env;
                    418:                else {
                    419:                        // beside by binary
                    420:                        char beside_binary_path[MAX_STRING];
                    421:                        strncpy(beside_binary_path, argv0, MAX_STRING-1);  beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
                    422:                        if(!(
                    423:                                rsplit(beside_binary_path, '/') || 
                    424:                                rsplit(beside_binary_path, '\\'))) { // strip filename
                    425:                                // no path, just filename
                    426:                                // @todo full path, not ./!
                    427:                                beside_binary_path[0]='.'; beside_binary_path[1]=0;
                    428:                        }
                    429:                        snprintf(config_filespec_buf, MAX_STRING, 
                    430:                                "%s/%s", 
                    431:                                beside_binary_path, AUTO_FILE_NAME);
                    432:                        config_filespec_cstr=config_filespec_buf;
1.197     paf       433:                        fail_on_config_read_problem=entry_exists(config_filespec_cstr);
1.193     paf       434:                }
1.122     parser    435:        }
                    436:        
                    437:        // process the request
                    438:        request.core(
1.193     paf       439:                config_filespec_cstr, fail_on_config_read_problem,
1.122     parser    440:                header_only);
1.201     paf       441: 
                    442:        // no request [prevent signal handlers from accessing invalid memory]
                    443:        ::request=0;
1.122     parser    444:        
                    445:        //
1.216.2.3  paf       446:        pa_done_socks();
1.122     parser    447:        
                    448: #ifdef DEBUG_POOL_MALLOC
                    449:        extern void log_pool_stats(Pool& pool);
1.198     paf       450:        log_pool_stats(request_pool);
1.122     parser    451: #endif
                    452: }
                    453: 
1.216.2.13  paf       454: #if _MSC_VER && !defined(_DEBUG)
1.216.2.11  paf       455: #      define PA_USE___TRY
                    456: struct Parser_executed {
                    457:        bool with_system_exception;
                    458:        LPEXCEPTION_POINTERS system_exception;
                    459: };
1.216.2.13  paf       460: #endif
1.216.2.11  paf       461: 
                    462: static 
                    463: #ifdef PA_USE___TRY
                    464: Parser_executed 
                    465: #else
                    466: void
                    467: #endif
                    468: call_real_parser_handler__do_SEH_return_it(
1.216.2.1  paf       469:                                                                 const char* filespec_to_process,
                    470:                                                                 const char* request_method, bool header_only) {
1.216.2.11  paf       471: #ifdef PA_USE___TRY
                    472:        Parser_executed result={false};
1.122     parser    473:        __try {
                    474: #endif
                    475:                real_parser_handler(
                    476:                        filespec_to_process,
                    477:                        request_method, header_only);
                    478:                
1.216.2.11  paf       479: #ifdef PA_USE___TRY
1.122     parser    480:        } __except (
1.216.2.11  paf       481:                (result.system_exception=GetExceptionInformation()), 
1.122     parser    482:                EXCEPTION_EXECUTE_HANDLER) {
1.216.2.12  paf       483:                //_asm int 3;
1.216.2.11  paf       484:                result.with_system_exception=true;
                    485:        }
                    486:        return result;
                    487: #endif
                    488: }
                    489: 
                    490: static void call_real_parser_handler__do_SEH(
                    491:                                                                 const char* filespec_to_process,
                    492:                                                                 const char* request_method, bool header_only) {
                    493: #ifdef PA_USE___TRY
                    494:        Parser_executed executed=
                    495: #endif
                    496:                call_real_parser_handler__do_SEH_return_it(filespec_to_process, 
                    497:                        request_method, header_only);
                    498: 
                    499: #ifdef PA_USE___TRY
                    500:        if(executed.with_system_exception)
                    501:                if(executed.system_exception)
                    502:                        if(_EXCEPTION_RECORD *er=executed.system_exception->ExceptionRecord)
                    503:                                throw Exception(Exception::undefined_type,
                    504:                                        Exception::undefined_source,
1.165     paf       505:                                        "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
1.122     parser    506:                        else
1.216.2.11  paf       507:                                throw Exception(Exception::undefined_type, 
                    508:                                        Exception::undefined_source, 
                    509:                                        "Exception <no exception record>");
                    510:                else
                    511:                        throw Exception(Exception::undefined_type, 
                    512:                                Exception::undefined_source, 
                    513:                                "Exception <no exception information>");
1.122     parser    514: #endif
                    515: }
                    516: 
1.216.2.1  paf       517: static void usage(const char* program) {
1.188     paf       518:        printf(
1.216.2.1  paf       519:                "Parser/%s Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)\n"
1.184     paf       520:                "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
                    521:                "\n"
                    522:                "Usage: %s [options] file\n"
                    523:                "Options are:\n"
1.185     paf       524: #ifdef WITH_MAILRECEIVE
1.193     paf       525:                "    -m              Parse mail, put received letter to $mail:received\n"
1.184     paf       526: #endif
1.193     paf       527:                "    -f config_file  Use this config file (/path/to/auto.p)\n"
                    528:                "    -h              Display usage information (this message)\n"
1.184     paf       529:                , PARSER_VERSION, 
                    530:                program);
                    531:        exit(EINVAL);
                    532: }
                    533: 
1.195     paf       534: int main(int argc, char *argv[]) {
1.203     paf       535: #ifdef SIGUSR1
1.205     paf       536:     if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR)
1.203     paf       537:                SAPI::die("Can not set handler for SIGUSR1");
                    538: #endif
                    539: #ifdef SIGPIPE
1.205     paf       540:     if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR)
1.203     paf       541:                SAPI::die("Can not set handler for SIGPIPE");
                    542: #endif
                    543: 
                    544: 
1.185     paf       545: #ifdef DEBUG_MAILRECEIVE
                    546:        if(FILE *fake_in=fopen(DEBUG_MAILRECEIVE, "rt")) {
1.184     paf       547:                dup2(fake_in->_file, 0/*STDIN_FILENO*/);
                    548:        }
                    549: #endif
                    550: 
1.178     paf       551: #ifdef _DEBUG
1.216.2.9  paf       552:        //_crtBreakAlloc=4042;
1.178     paf       553: #endif
1.216.2.10  paf       554:        //_asm int 3;
1.193     paf       555:        argv0=argv[0];
1.45      paf       556: 
1.32      paf       557:        umask(2);
                    558: 
1.3       paf       559:        // were we started as CGI?
1.146     paf       560:        cgi=
1.109     parser    561:                getenv("SERVER_SOFTWARE") || 
                    562:                getenv("SERVER_NAME") || 
                    563:                getenv("GATEWAY_INTERFACE") || 
1.216.2.15  paf       564:                getenv("REQUEST_METHOD");
1.5       paf       565:        
1.184     paf       566:        char *raw_filespec_to_process;
1.210     paf       567:        if(cgi) {
1.184     paf       568:                raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.210     paf       569:                if(raw_filespec_to_process && !*raw_filespec_to_process)
                    570:                        raw_filespec_to_process=0;
                    571:        } else {
1.184     paf       572:                optind = 1;
                    573:                opterr = 0;
                    574:                int c;
1.216.2.19  paf       575:                while((c = getopt(argc, argv, "hf:"
1.185     paf       576: #ifdef WITH_MAILRECEIVE
1.184     paf       577:                        "m"
                    578: #endif
                    579:                        )) > 0) {
                    580:                        switch (c) {
                    581:                        case 'h':
                    582:                                usage(argv[0]);
1.216.2.15  paf       583:                                break;
1.193     paf       584:                        case 'f':
                    585:                                config_filespec_cstr=optarg;
1.184     paf       586:                                break;
1.185     paf       587: #ifdef WITH_MAILRECEIVE
1.184     paf       588:                        case 'm':
                    589:                                mail_received=true;
                    590:                                break;
                    591: #endif
                    592:                        default:
                    593:                                fprintf(stderr, "%s: invalid option '%c'\n", argv[0], optopt);
                    594:                                usage(argv[0]);
                    595:                                break;
                    596:                        }
                    597:                }
                    598:                if (optind != argc - 1) {
                    599:                        fprintf(stderr, "%s: file not specified\n", argv[0]);
                    600:                        usage(argv[0]);
1.10      paf       601:                }
1.184     paf       602:                
                    603:                raw_filespec_to_process=argv[optind++];
1.10      paf       604:        }
                    605: 
1.100     parser    606: #ifdef WIN32
                    607:        setmode(fileno(stdin), _O_BINARY);
                    608:        setmode(fileno(stdout), _O_BINARY);
                    609:        setmode(fileno(stderr), _O_BINARY);
                    610: #endif
                    611: 
1.216.2.19  paf       612: #if defined(_MSC_VER) && defined(_DEBUG)
1.148     paf       613:        // Get current flag
                    614:        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
                    615: 
                    616:        // Turn on leak-checking bit
1.216.2.19  paf       617:        tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
1.148     paf       618: 
                    619:        // Set flag to the new value
                    620:        _CrtSetDbgFlag( tmpFlag );
1.216.2.30! paf       621:        _CrtSetBreakAlloc(62143); //147302
        !           622: 
        !           623:        _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
        !           624:        _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138     paf       625: #endif
                    626: 
1.166     paf       627:        char filespec_to_process[MAX_STRING];
                    628:        full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10      paf       629: 
1.216.2.1  paf       630:        const char* request_method=getenv("REQUEST_METHOD");
1.35      paf       631:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131     paf       632: 
1.122     parser    633:        try { // global try
                    634:                call_real_parser_handler__do_SEH(
                    635:                        filespec_to_process,
                    636:                        request_method, header_only);
                    637:        } catch(const Exception& e) { // global problem 
1.44      paf       638:                // don't allocate anything on pool here:
                    639:                //   possible pool' exception not catch-ed now
                    640:                //   and there could be out-of-memory exception
1.43      paf       641: 
1.144     paf       642:                SAPI::die("exception in request exception handler: %s", e.comment());
1.16      paf       643:        }
1.109     parser    644: 
1.216.2.11  paf       645: //     _asm int 3;
1.134     paf       646:        return 0;
1.1       paf       647: }

E-mail: