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

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

E-mail: