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

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

E-mail: