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

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

E-mail: