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

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

E-mail: