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

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.218     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.222   ! paf         8: static const char * const IDENT_PARSER3_C="$Date: 2003/11/07 13:59:22 $";
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.218     paf       279:        pa_init_socks();
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:        
                    426:        //
1.218     paf       427:        pa_done_socks();
                    428: }
                    429: 
                    430: #if _MSC_VER && !defined(_DEBUG)
                    431: #      define PA_USE___TRY
                    432: struct Parser_executed {
                    433:        bool with_system_exception;
                    434:        LPEXCEPTION_POINTERS system_exception;
                    435: };
1.122     parser    436: #endif
1.160     paf       437: 
1.218     paf       438: static 
                    439: #ifdef PA_USE___TRY
                    440: Parser_executed 
                    441: #else
                    442: void
1.160     paf       443: #endif
1.218     paf       444: call_real_parser_handler__do_SEH_return_it(
                    445:                                           const char* filespec_to_process,
                    446:                                           const char* request_method, bool header_only) {
                    447: #ifdef PA_USE___TRY
                    448:        Parser_executed result={false};
1.122     parser    449:        __try {
                    450: #endif
                    451:                real_parser_handler(
                    452:                        filespec_to_process,
                    453:                        request_method, header_only);
                    454:                
1.218     paf       455: #ifdef PA_USE___TRY
1.122     parser    456:        } __except (
1.218     paf       457:                (result.system_exception=GetExceptionInformation()), 
1.122     parser    458:                EXCEPTION_EXECUTE_HANDLER) {
1.218     paf       459:                result.with_system_exception=true;
1.122     parser    460:        }
1.218     paf       461:        return result;
1.122     parser    462: #endif
                    463: }
                    464: 
1.218     paf       465: static void call_real_parser_handler__do_SEH(
                    466:                                             const char* filespec_to_process,
                    467:                                             const char* request_method, bool header_only) {
                    468: #ifdef PA_USE___TRY
                    469:        Parser_executed executed=
1.135     paf       470: #endif
1.218     paf       471:                call_real_parser_handler__do_SEH_return_it(filespec_to_process, 
                    472:                        request_method, header_only);
1.131     paf       473: 
1.218     paf       474: #ifdef PA_USE___TRY
                    475:        if(executed.with_system_exception)
                    476:                if(executed.system_exception)
                    477:                        if(_EXCEPTION_RECORD *er=executed.system_exception->ExceptionRecord)
                    478:                                throw Exception(0,
                    479:                                        0,
                    480:                                        "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
                    481:                        else
                    482:                                throw Exception(0, 
                    483:                                        0, 
                    484:                                        "Exception <no exception record>");
                    485:                else
                    486:                        throw Exception(0, 
                    487:                                0, 
                    488:                                "Exception <no exception information>");
                    489: #endif
1.131     paf       490: }
                    491: 
1.218     paf       492: static void usage(const char* program) {
1.188     paf       493:        printf(
1.218     paf       494:                "Parser/%s Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)\n"
1.184     paf       495:                "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
                    496:                "\n"
                    497:                "Usage: %s [options] file\n"
                    498:                "Options are:\n"
1.185     paf       499: #ifdef WITH_MAILRECEIVE
1.193     paf       500:                "    -m              Parse mail, put received letter to $mail:received\n"
1.184     paf       501: #endif
1.193     paf       502:                "    -f config_file  Use this config file (/path/to/auto.p)\n"
                    503:                "    -h              Display usage information (this message)\n"
1.184     paf       504:                , PARSER_VERSION, 
                    505:                program);
                    506:        exit(EINVAL);
                    507: }
                    508: 
1.218     paf       509: int main(int argc, char *argv[]) {
                    510:        GC_java_finalization=0;
1.217     paf       511: 
1.218     paf       512: #ifndef PA_DEBUG_DISABLE_GC
                    513:        // Dont collect unless explicitly requested 
                    514:        // this is quicker (~30% ), but less memory-efficient(~8%)
                    515:        // so deciding for speed
                    516:        GC_dont_gc=1; 
                    517: #endif
                    518: /*
                    519:        
                    520:        Array<int> test;
                    521:        test+=3;
                    522:        test+=4;
                    523: //     int a=test.count();
                    524:        int i=0;
                    525:        scanf("%d", &i);
                    526:        int b=test.get(i);
                    527: //     int b=test.get(10);
                    528:        printf("%d", b);//test.count());*/
1.217     paf       529: 
1.203     paf       530: #ifdef SIGUSR1
1.205     paf       531:     if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR)
1.203     paf       532:                SAPI::die("Can not set handler for SIGUSR1");
                    533: #endif
                    534: #ifdef SIGPIPE
1.205     paf       535:     if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR)
1.203     paf       536:                SAPI::die("Can not set handler for SIGPIPE");
                    537: #endif
                    538: 
                    539: 
1.185     paf       540: #ifdef DEBUG_MAILRECEIVE
                    541:        if(FILE *fake_in=fopen(DEBUG_MAILRECEIVE, "rt")) {
1.184     paf       542:                dup2(fake_in->_file, 0/*STDIN_FILENO*/);
                    543:        }
                    544: #endif
                    545: 
1.178     paf       546: #ifdef _DEBUG
1.220     paf       547:        //_crtBreakAlloc=46;
1.178     paf       548: #endif
1.193     paf       549:        argv0=argv[0];
1.45      paf       550: 
1.32      paf       551:        umask(2);
                    552: 
1.3       paf       553:        // were we started as CGI?
1.146     paf       554:        cgi=
1.109     parser    555:                getenv("SERVER_SOFTWARE") || 
                    556:                getenv("SERVER_NAME") || 
                    557:                getenv("GATEWAY_INTERFACE") || 
                    558:                getenv("REQUEST_METHOD");
1.5       paf       559:        
1.184     paf       560:        char *raw_filespec_to_process;
1.210     paf       561:        if(cgi) {
1.184     paf       562:                raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.210     paf       563:                if(raw_filespec_to_process && !*raw_filespec_to_process)
                    564:                        raw_filespec_to_process=0;
                    565:        } else {
1.184     paf       566:                optind = 1;
                    567:                opterr = 0;
                    568:                int c;
1.193     paf       569:                while((c = getopt(argc, argv, "hf:"
1.185     paf       570: #ifdef WITH_MAILRECEIVE
1.184     paf       571:                        "m"
                    572: #endif
                    573:                        )) > 0) {
                    574:                        switch (c) {
                    575:                        case 'h':
                    576:                                usage(argv[0]);
1.193     paf       577:                                break;
                    578:                        case 'f':
                    579:                                config_filespec_cstr=optarg;
1.184     paf       580:                                break;
1.185     paf       581: #ifdef WITH_MAILRECEIVE
1.184     paf       582:                        case 'm':
                    583:                                mail_received=true;
                    584:                                break;
                    585: #endif
                    586:                        default:
                    587:                                fprintf(stderr, "%s: invalid option '%c'\n", argv[0], optopt);
                    588:                                usage(argv[0]);
                    589:                                break;
                    590:                        }
                    591:                }
                    592:                if (optind != argc - 1) {
                    593:                        fprintf(stderr, "%s: file not specified\n", argv[0]);
                    594:                        usage(argv[0]);
1.10      paf       595:                }
1.184     paf       596:                
                    597:                raw_filespec_to_process=argv[optind++];
1.10      paf       598:        }
                    599: 
1.100     parser    600: #ifdef WIN32
                    601:        setmode(fileno(stdin), _O_BINARY);
                    602:        setmode(fileno(stdout), _O_BINARY);
                    603:        setmode(fileno(stderr), _O_BINARY);
                    604: #endif
                    605: 
1.218     paf       606: #if defined(_MSC_VER) && defined(_DEBUG)
1.148     paf       607:        // Get current flag
                    608:        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
                    609: 
                    610:        // Turn on leak-checking bit
                    611:        tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
                    612: 
                    613:        // Set flag to the new value
                    614:        _CrtSetDbgFlag( tmpFlag );
1.218     paf       615: //     _CrtSetBreakAlloc(61);
1.138     paf       616: 
1.218     paf       617:        _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
                    618:        _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138     paf       619: #endif
                    620: 
1.166     paf       621:        char filespec_to_process[MAX_STRING];
                    622:        full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10      paf       623: 
1.218     paf       624:        const char* request_method=getenv("REQUEST_METHOD");
1.35      paf       625:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131     paf       626: 
1.122     parser    627:        try { // global try
                    628:                call_real_parser_handler__do_SEH(
                    629:                        filespec_to_process,
                    630:                        request_method, header_only);
                    631:        } catch(const Exception& e) { // global problem 
1.44      paf       632:                // don't allocate anything on pool here:
                    633:                //   possible pool' exception not catch-ed now
                    634:                //   and there could be out-of-memory exception
1.218     paf       635:                const char* body=e.comment();
                    636:                char buf[MAX_STRING];
                    637:                snprintf(buf, MAX_STRING, "exception in request exception handler: %s",
                    638:                        body);
                    639:                // log it
                    640:                SAPI::log(SAPI_info, "%s", buf);
                    641: 
                    642:                //
                    643:                int content_length=strlen(buf);
                    644: 
                    645:                // prepare header
                    646:                SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
                    647:                char content_length_cstr[MAX_NUMBER];
                    648:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
                    649:                SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
                    650: 
                    651:                // send header
                    652:                SAPI::send_header(SAPI_info);
                    653: 
                    654:                // send body
                    655:                if(!header_only)
                    656:                        SAPI::send_body(SAPI_info, buf, content_length);
1.43      paf       657: 
1.218     paf       658:                // unsuccessful finish
1.16      paf       659:        }
1.109     parser    660: 
1.134     paf       661:        return 0;
1.1       paf       662: }

E-mail: