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

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

E-mail: