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

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

E-mail: