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

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

E-mail: