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

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.223     paf         4:        Copyright(c) 2001-2004 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.225   ! paf         8: static const char * const IDENT_PARSER3_C="$Date: 2004/03/23 09:42:14 $";
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.196     paf        30: //#define DEBUG_MAILRECEIVE "mailreceive.eml"
1.160     paf        31: 
1.109     parser     32: // consts
1.84      parser     33: 
1.175     paf        34: #define REDIRECT_PREFIX "REDIRECT_"
1.181     paf        35: #define PARSER_CONFIG_ENV_NAME "CGI_PARSER_CONFIG"
1.159     paf        36: 
1.42      paf        37: /// IIS refuses to read bigger chunks
                     38: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M 
                     39: 
1.218     paf        40: static const char* argv0;
                     41: static const char* config_filespec_cstr=0;
1.193     paf        42: static bool fail_on_config_read_problem=true;
1.192     paf        43: 
1.184     paf        44: static bool cgi; ///< we were started as CGI?
                     45: static bool mail_received=false; ///< we were started with -m option? [asked to parse incoming message to $mail:received]
1.5       paf        46: 
1.201     paf        47: // for signal handlers
                     48: Request *request=0;
1.218     paf        49: Request_info *request_info=0;
                     50: bool execution_canceled=false;
1.201     paf        51: 
1.46      paf        52: // SAPI
1.86      parser     53: 
1.218     paf        54: class SAPI_Info{} SAPI_info;
                     55: 
                     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.218     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.218     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.218     paf       113: static void die_or_abort(const char* fmt, va_list args, bool write_core) {
1.138     paf       114:        // log
                    115: 
                    116:        // logging is more important than user 
1.204     paf       117:        // she can cancel download, we'd get SIGPIPE, 
1.138     paf       118:        // nothing would be logged then
1.124     parser    119:        ::log(fmt, args);
                    120: 
1.138     paf       121:        // inform user
                    122: 
1.134     paf       123:        char body[MAX_STRING];
1.138     paf       124:        int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134     paf       125: 
                    126:        // prepare header
                    127:        // let's be honest, that's bad we couldn't produce valid output
1.218     paf       128:        SAPI::add_header_attribute(SAPI_info, "status", "500");
                    129:        SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
1.134     paf       130:        char content_length_cstr[MAX_NUMBER];
1.168     paf       131:        snprintf(content_length_cstr, sizeof(content_length_cstr), "%u", content_length);
1.218     paf       132:        SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
1.134     paf       133: 
                    134:        // send header
1.218     paf       135:        SAPI::send_header(SAPI_info);
1.134     paf       136: 
                    137:        // body
1.218     paf       138:        SAPI::send_body(SAPI_info, body, content_length);
1.134     paf       139: 
1.218     paf       140:        // exit & try to produce core dump[unix] or invoke debugger[Win32 Debug version]
                    141:        if(write_core) {
                    142: #if defined(WIN32) && !defined(_DEBUG)
                    143:                // IIS with abort failes to show STDOUT, it just barks "abnormal program termination"
                    144:                exit(1);
1.214     paf       145: #else
1.218     paf       146: #if _MSC_VER
                    147:                _asm int 3;
                    148: #endif
                    149:                abort();
1.214     paf       150: #endif
1.218     paf       151:        } 
                    152:        else
                    153:                exit(1);
                    154: }
                    155: 
                    156: void SAPI::die(const char* fmt, ...) {
                    157:     va_list args;
                    158:        va_start(args, fmt);
                    159:        die_or_abort(fmt, args, false/*write core?*/);
                    160:        va_end(args);
1.61      paf       161: }
                    162: 
1.218     paf       163: void SAPI::abort(const char* fmt, ...) {
                    164:     va_list args;
                    165:        va_start(args, fmt);
                    166:        die_or_abort(fmt, args, true/*write core?*/);
                    167:        va_end(args);
1.28      paf       168: }
                    169: 
1.218     paf       170: char* SAPI::get_env(SAPI_Info& , const char* name) {
                    171:        if(char *local=getenv(name))
                    172:                return pa_strdup(local);
                    173:        else
                    174:                return 0;
                    175: }
                    176: 
                    177: const char* const *SAPI::environment(SAPI_Info&) {
1.180     paf       178: #ifdef _MSC_VER
                    179:        extern char **_environ;
                    180:        return _environ;
                    181: #else
                    182:        extern char **environ;
                    183:        return environ;
                    184: #endif
                    185: }
                    186: 
1.218     paf       187: size_t SAPI::read_post(SAPI_Info& , char *buf, size_t max_bytes) {
1.59      paf       188:        size_t read_size=0;
1.12      paf       189:        do {
1.200     paf       190:                ssize_t chunk_size=read(fileno(stdin), 
1.42      paf       191:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129     paf       192:                if(chunk_size<=0)
1.12      paf       193:                        break;
                    194:                read_size+=chunk_size;
                    195:        } while(read_size<max_bytes);
                    196: 
                    197:        return read_size;
1.10      paf       198: }
                    199: 
1.218     paf       200: void SAPI::add_header_attribute(SAPI_Info& , const char* dont_store_key, const char* dont_store_value) {
1.68      paf       201:        if(cgi)
1.218     paf       202:                printf("%s: %s\n", dont_store_key, dont_store_value);
1.19      paf       203: }
                    204: 
1.56      paf       205: /// @todo intelligent cache-control
1.218     paf       206: void SAPI::send_header(SAPI_Info& ) {
1.33      paf       207:        if(cgi) {
1.147     paf       208: //             puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       209: 
                    210:                // header | body  delimiter
1.20      paf       211:                puts("");
1.33      paf       212:        }
1.30      paf       213: }
1.20      paf       214: 
1.218     paf       215: void SAPI::send_body(SAPI_Info& , const void *buf, size_t size) {
1.19      paf       216:        stdout_write(buf, size);
1.58      paf       217: }
                    218: 
1.97      parser    219: //
                    220: 
1.218     paf       221: static void full_file_spec(const char* file_name, char *buf, size_t buf_size) {
1.210     paf       222:        if(file_name)
1.167     paf       223:                if(file_name[0]=='/' 
                    224: #ifdef WIN32
1.210     paf       225:                        || file_name[0] && file_name[1]==':'
1.167     paf       226: #endif
1.210     paf       227:                        )
1.218     paf       228:                                strncpy(buf, file_name, buf_size);
1.167     paf       229:                else {
                    230:                        char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    231:                        snprintf(buf, buf_size, "%s/%s", cwd, file_name);
                    232:                }
                    233:        else
                    234:                buf[0]=0;
1.166     paf       235: #ifdef WIN32
                    236:        back_slashes_to_slashes(buf);
                    237: #endif
1.97      parser    238: }
                    239: 
1.218     paf       240: static void log_signal(const char* signal_name) {
                    241:        if(request_info)
1.219     paf       242:                SAPI::log(SAPI_info, "%s received while %s. uri=%s, method=%s, cl=%u",
1.218     paf       243:                        signal_name,
                    244:                        request?"executing code":"reading data",
                    245:                        request_info->uri,
                    246:                        request_info->method,
                    247:                        request_info->content_length);
                    248:        else
                    249:                SAPI::log(SAPI_info, "%s received before or after processing request",
                    250:                        signal_name);
1.205     paf       251: }
                    252: 
1.201     paf       253: #ifdef SIGUSR1
1.205     paf       254: static void SIGUSR1_handler(int /*sig*/){
                    255:        log_signal("SIGUSR1");
1.201     paf       256: }
                    257: #endif
                    258: 
                    259: #ifdef SIGPIPE
1.205     paf       260: static void SIGPIPE_handler(int /*sig*/){
                    261:        log_signal("SIGPIPE");
1.218     paf       262:        execution_canceled=true;
1.201     paf       263:        if(request)
1.218     paf       264:                request->set_interrupted(true);
1.201     paf       265: }
                    266: #endif
                    267: 
1.40      paf       268: /**
1.122     parser    269: main workhorse
1.19      paf       270: 
1.122     parser    271:   @todo 
1.40      paf       272:                IIS: remove trailing default-document[index.html] from $request.uri.
                    273:                to do that we need to consult metabase,
                    274:                wich is tested but seems slow.
                    275: */
1.218     paf       276: static void real_parser_handler(const char* filespec_to_process,
                    277:                                const char* request_method, bool header_only) {
1.122     parser    278:        // init socks
1.225   ! paf       279:        pa_socks_init();
1.122     parser    280:        
                    281:        // init global variables
1.218     paf       282:        pa_globals_init();
1.122     parser    283:        
1.186     paf       284:        if(!filespec_to_process || !*filespec_to_process)
1.144     paf       285:                SAPI::die("Parser/%s", PARSER_VERSION);
1.122     parser    286:        
                    287:        // Request info
1.218     paf       288:        Request_info request_info;  memset(&request_info, 0, sizeof(request_info));
1.166     paf       289:        char document_root_buf[MAX_STRING];
1.122     parser    290:        if(cgi) {
1.218     paf       291:                if(const char* env_document_root=getenv("DOCUMENT_ROOT"))
1.122     parser    292:                        request_info.document_root=env_document_root;
1.218     paf       293:                else if(const char* path_info=getenv("PATH_INFO")) {
1.122     parser    294:                        // IIS
1.166     paf       295:                        size_t len=min(sizeof(document_root_buf)-1, strlen(filespec_to_process)-strlen(path_info));
                    296:                        memcpy(document_root_buf, filespec_to_process, len); document_root_buf[len]=0;
                    297:                        request_info.document_root=document_root_buf;
1.122     parser    298:                } else
1.165     paf       299:                        throw Exception("parser.runtime",
                    300:                                0,
                    301:                                "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.122     parser    302:        } else {
1.166     paf       303:                full_file_spec("", document_root_buf, sizeof(document_root_buf));
                    304:                request_info.document_root=document_root_buf;
1.122     parser    305:        }
                    306:        request_info.path_translated=filespec_to_process;
                    307:        request_info.method=request_method ? request_method : "GET";
1.218     paf       308:        const char* query_string=getenv("QUERY_STRING");
1.122     parser    309:        request_info.query_string=query_string;
                    310:        if(cgi) {
1.214     paf       311:                // few absolute obligatory
1.218     paf       312:                const char* path_info=getenv("PATH_INFO");
1.214     paf       313:                if(!path_info)
                    314:                        SAPI::die("CGI: illegal call (missing PATH_INFO)");
1.218     paf       315:                const char* script_name=getenv("SCRIPT_NAME");
1.214     paf       316:                if(!script_name)
                    317:                                SAPI::die("CGI: illegal call (missing SCRIPT_NAME)");
                    318: 
1.218     paf       319:                const char* env_request_uri=getenv("REQUEST_URI");
1.214     paf       320:                if(env_request_uri)
1.122     parser    321:                        request_info.uri=env_request_uri;
1.214     paf       322:                else 
1.122     parser    323:                        if(query_string) {
1.218     paf       324:                                char* reconstructed_uri=new(PointerFreeGC) char[
1.122     parser    325:                                        strlen(path_info)+1/*'?'*/+
1.218     paf       326:                                        strlen(query_string)+1/*0*/];
1.122     parser    327:                                strcpy(reconstructed_uri, path_info);
                    328:                                strcat(reconstructed_uri, "?");
                    329:                                strcat(reconstructed_uri, query_string);
                    330:                                request_info.uri=reconstructed_uri;
                    331:                        } else
                    332:                                request_info.uri=path_info;
1.214     paf       333: 
                    334:                if(env_request_uri) { // apache & others stuck to standards
                    335:                        /*
                    336:                                http://parser3/env.html?123  =OK
                    337:                                $request:uri=/env.html?123
                    338:                                REQUEST_URI='/env.html?123'
                    339:                                SCRIPT_NAME='/cgi-bin/parser3'
                    340:                                PATH_INFO='/env.html'
                    341: 
                    342:                                http://parser3/cgi-bin/parser3/env.html?123 =ERROR
                    343:                                $request:uri=/cgi-bin/parser3/env.html?123
                    344:                                REQUEST_URI='/cgi-bin/parser3/env.html?123'
                    345:                                SCRIPT_NAME='/cgi-bin/parser3'
                    346:                                PATH_INFO='/env.html'
                    347:                        */
                    348:                        size_t script_name_len=strlen(script_name);
                    349:                        size_t uri_len=strlen(env_request_uri);
                    350:                        if(strncmp(env_request_uri, script_name, script_name_len)==0 &&
                    351:                                script_name_len != uri_len) // under IIS they are the same
                    352:                                SAPI::die("CGI: illegal call (1)");
                    353:                } else { // seen on IIS5
                    354:                        /*
                    355:                                http://nestle/env.html?123 =OK
                    356:                                $request:uri=/env.html?123
                    357:                                REQUEST_URI=''
                    358:                                SCRIPT_NAME='/env.html'
                    359:                                PATH_INFO='/env.html'
                    360: 
                    361:                                http://nestle/cgi-bin/parser3.exe/env.html =ERROR
                    362:                                $request:uri=/env.html
                    363:                                REQUEST_URI=''
                    364:                                SCRIPT_NAME='/cgi-bin/parser3.exe'
                    365:                                PATH_INFO='/env.html'
                    366:                        */
                    367:                        if(strcmp(script_name, path_info)!=0)
                    368:                                SAPI::die("CGI: illegal call (2)");
                    369:                }
1.122     parser    370:        } else
1.177     paf       371:                request_info.uri="";
1.122     parser    372:        
1.218     paf       373:        request_info.content_type=getenv("CONTENT_TYPE");
                    374:        const char* content_length=getenv("CONTENT_LENGTH");
1.122     parser    375:        request_info.content_length=(content_length?atoi(content_length):0);
1.218     paf       376:        request_info.cookie=getenv("HTTP_COOKIE");
1.184     paf       377:        request_info.mail_received=mail_received;
                    378: 
1.218     paf       379:        // get request_info ptr for signal handlers
                    380:        ::request_info=&request_info;
                    381:        if(execution_canceled)
                    382:                SAPI::die("Execution canceled");
1.198     paf       383: 
1.122     parser    384:        // prepare to process request
1.221     paf       385:        Request request(SAPI_info, request_info,
1.218     paf       386:                cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS,
1.130     paf       387:                true /* status_allowed */);
1.201     paf       388: 
                    389:        // get request ptr for signal handlers
                    390:        ::request=&request;
                    391: 
1.181     paf       392:        char config_filespec_buf[MAX_STRING];
1.193     paf       393:        if(!config_filespec_cstr) {
1.218     paf       394:                const char* config_by_env=getenv(PARSER_CONFIG_ENV_NAME);
1.193     paf       395:                if(!config_by_env)
                    396:                        config_by_env=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
                    397:                if(config_by_env)
                    398:                        config_filespec_cstr=config_by_env;
                    399:                else {
                    400:                        // beside by binary
                    401:                        char beside_binary_path[MAX_STRING];
                    402:                        strncpy(beside_binary_path, argv0, MAX_STRING-1);  beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
                    403:                        if(!(
                    404:                                rsplit(beside_binary_path, '/') || 
                    405:                                rsplit(beside_binary_path, '\\'))) { // strip filename
                    406:                                // no path, just filename
                    407:                                // @todo full path, not ./!
                    408:                                beside_binary_path[0]='.'; beside_binary_path[1]=0;
                    409:                        }
                    410:                        snprintf(config_filespec_buf, MAX_STRING, 
                    411:                                "%s/%s", 
                    412:                                beside_binary_path, AUTO_FILE_NAME);
                    413:                        config_filespec_cstr=config_filespec_buf;
1.197     paf       414:                        fail_on_config_read_problem=entry_exists(config_filespec_cstr);
1.193     paf       415:                }
1.122     parser    416:        }
                    417:        
                    418:        // process the request
                    419:        request.core(
1.193     paf       420:                config_filespec_cstr, fail_on_config_read_problem,
1.122     parser    421:                header_only);
1.201     paf       422: 
                    423:        // no request [prevent signal handlers from accessing invalid memory]
                    424:        ::request=0;
1.122     parser    425:        
1.225   ! paf       426:        // finalize global variables
        !           427:        pa_globals_done();
        !           428: 
1.122     parser    429:        //
1.225   ! paf       430:        pa_socks_done();
1.218     paf       431: }
                    432: 
                    433: #if _MSC_VER && !defined(_DEBUG)
                    434: #      define PA_USE___TRY
                    435: struct Parser_executed {
                    436:        bool with_system_exception;
                    437:        LPEXCEPTION_POINTERS system_exception;
                    438: };
1.122     parser    439: #endif
1.160     paf       440: 
1.218     paf       441: static 
                    442: #ifdef PA_USE___TRY
                    443: Parser_executed 
                    444: #else
                    445: void
1.160     paf       446: #endif
1.218     paf       447: call_real_parser_handler__do_SEH_return_it(
                    448:                                           const char* filespec_to_process,
                    449:                                           const char* request_method, bool header_only) {
                    450: #ifdef PA_USE___TRY
                    451:        Parser_executed result={false};
1.122     parser    452:        __try {
                    453: #endif
                    454:                real_parser_handler(
                    455:                        filespec_to_process,
                    456:                        request_method, header_only);
                    457:                
1.218     paf       458: #ifdef PA_USE___TRY
1.122     parser    459:        } __except (
1.218     paf       460:                (result.system_exception=GetExceptionInformation()), 
1.122     parser    461:                EXCEPTION_EXECUTE_HANDLER) {
1.218     paf       462:                result.with_system_exception=true;
1.122     parser    463:        }
1.218     paf       464:        return result;
1.122     parser    465: #endif
                    466: }
                    467: 
1.218     paf       468: static void call_real_parser_handler__do_SEH(
                    469:                                             const char* filespec_to_process,
                    470:                                             const char* request_method, bool header_only) {
                    471: #ifdef PA_USE___TRY
                    472:        Parser_executed executed=
1.135     paf       473: #endif
1.218     paf       474:                call_real_parser_handler__do_SEH_return_it(filespec_to_process, 
                    475:                        request_method, header_only);
1.131     paf       476: 
1.218     paf       477: #ifdef PA_USE___TRY
                    478:        if(executed.with_system_exception)
                    479:                if(executed.system_exception)
                    480:                        if(_EXCEPTION_RECORD *er=executed.system_exception->ExceptionRecord)
                    481:                                throw Exception(0,
                    482:                                        0,
                    483:                                        "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
                    484:                        else
                    485:                                throw Exception(0, 
                    486:                                        0, 
                    487:                                        "Exception <no exception record>");
                    488:                else
                    489:                        throw Exception(0, 
                    490:                                0, 
                    491:                                "Exception <no exception information>");
                    492: #endif
1.131     paf       493: }
                    494: 
1.218     paf       495: static void usage(const char* program) {
1.188     paf       496:        printf(
1.223     paf       497:                "Parser/%s Copyright(c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com)\n"
1.184     paf       498:                "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
                    499:                "\n"
                    500:                "Usage: %s [options] file\n"
                    501:                "Options are:\n"
1.185     paf       502: #ifdef WITH_MAILRECEIVE
1.193     paf       503:                "    -m              Parse mail, put received letter to $mail:received\n"
1.184     paf       504: #endif
1.193     paf       505:                "    -f config_file  Use this config file (/path/to/auto.p)\n"
                    506:                "    -h              Display usage information (this message)\n"
1.184     paf       507:                , PARSER_VERSION, 
                    508:                program);
                    509:        exit(EINVAL);
                    510: }
                    511: 
1.218     paf       512: int main(int argc, char *argv[]) {
1.224     paf       513:        //_asm int 3;
1.218     paf       514:        GC_java_finalization=0;
1.217     paf       515: 
1.218     paf       516: #ifndef PA_DEBUG_DISABLE_GC
                    517:        // Dont collect unless explicitly requested 
                    518:        // this is quicker (~30% ), but less memory-efficient(~8%)
                    519:        // so deciding for speed
                    520:        GC_dont_gc=1; 
                    521: #endif
                    522: /*
                    523:        
                    524:        Array<int> test;
                    525:        test+=3;
                    526:        test+=4;
                    527: //     int a=test.count();
                    528:        int i=0;
                    529:        scanf("%d", &i);
                    530:        int b=test.get(i);
                    531: //     int b=test.get(10);
                    532:        printf("%d", b);//test.count());*/
1.217     paf       533: 
1.203     paf       534: #ifdef SIGUSR1
1.205     paf       535:     if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR)
1.203     paf       536:                SAPI::die("Can not set handler for SIGUSR1");
                    537: #endif
                    538: #ifdef SIGPIPE
1.205     paf       539:     if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR)
1.203     paf       540:                SAPI::die("Can not set handler for SIGPIPE");
                    541: #endif
                    542: 
                    543: 
1.185     paf       544: #ifdef DEBUG_MAILRECEIVE
                    545:        if(FILE *fake_in=fopen(DEBUG_MAILRECEIVE, "rt")) {
1.184     paf       546:                dup2(fake_in->_file, 0/*STDIN_FILENO*/);
                    547:        }
                    548: #endif
                    549: 
1.178     paf       550: #ifdef _DEBUG
1.220     paf       551:        //_crtBreakAlloc=46;
1.178     paf       552: #endif
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") || 
                    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.193     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.193     paf       581:                                break;
                    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.218     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
                    615:        tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
                    616: 
                    617:        // Set flag to the new value
                    618:        _CrtSetDbgFlag( tmpFlag );
1.218     paf       619: //     _CrtSetBreakAlloc(61);
1.138     paf       620: 
1.218     paf       621:        _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
                    622:        _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138     paf       623: #endif
                    624: 
1.166     paf       625:        char filespec_to_process[MAX_STRING];
                    626:        full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10      paf       627: 
1.218     paf       628:        const char* request_method=getenv("REQUEST_METHOD");
1.35      paf       629:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131     paf       630: 
1.122     parser    631:        try { // global try
                    632:                call_real_parser_handler__do_SEH(
                    633:                        filespec_to_process,
                    634:                        request_method, header_only);
                    635:        } catch(const Exception& e) { // global problem 
1.44      paf       636:                // don't allocate anything on pool here:
                    637:                //   possible pool' exception not catch-ed now
                    638:                //   and there could be out-of-memory exception
1.218     paf       639:                const char* body=e.comment();
                    640:                char buf[MAX_STRING];
                    641:                snprintf(buf, MAX_STRING, "exception in request exception handler: %s",
                    642:                        body);
                    643:                // log it
                    644:                SAPI::log(SAPI_info, "%s", buf);
                    645: 
                    646:                //
                    647:                int content_length=strlen(buf);
                    648: 
                    649:                // prepare header
                    650:                SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
                    651:                char content_length_cstr[MAX_NUMBER];
                    652:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
                    653:                SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
                    654: 
                    655:                // send header
                    656:                SAPI::send_header(SAPI_info);
                    657: 
                    658:                // send body
                    659:                if(!header_only)
                    660:                        SAPI::send_body(SAPI_info, buf, content_length);
1.43      paf       661: 
1.218     paf       662:                // unsuccessful finish
1.16      paf       663:        }
1.109     parser    664: 
1.134     paf       665:        return 0;
1.1       paf       666: }

E-mail: