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

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

E-mail: