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

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.250     misha       4:        Copyright(c) 2001-2009 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.254   ! misha       8: static const char * const IDENT_PARSER3_C="$Date: 2009-08-30 05:29:46 $";
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.120     parser     25: #endif
                     26: 
1.232     paf        27: // defines
1.231     paf        28: 
1.233     paf        29: // comment remove me after debugging
1.234     paf        30: //#define PA_DEBUG_CGI_ENTRY_EXIT      "c:\\parser\\debug-parser3.log"
1.233     paf        31: 
1.231     paf        32: #if _MSC_VER && !defined(_DEBUG)
                     33: #      define PA_SUPPRESS_SYSTEM_EXCEPTION
                     34: #endif
                     35: 
1.196     paf        36: //#define DEBUG_MAILRECEIVE "mailreceive.eml"
1.160     paf        37: 
1.109     parser     38: // consts
1.84      parser     39: 
1.175     paf        40: #define REDIRECT_PREFIX "REDIRECT_"
1.181     paf        41: #define PARSER_CONFIG_ENV_NAME "CGI_PARSER_CONFIG"
1.226     paf        42: #define PARSER_LOG_ENV_NAME "CGI_PARSER_LOG"
1.159     paf        43: 
1.42      paf        44: /// IIS refuses to read bigger chunks
                     45: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M 
                     46: 
1.218     paf        47: static const char* argv0;
                     48: static const char* config_filespec_cstr=0;
1.193     paf        49: static bool fail_on_config_read_problem=true;
1.192     paf        50: 
1.245     misha      51: static int args_skip=1;
                     52: static char** argv_all = NULL;
                     53: 
1.184     paf        54: static bool cgi; ///< we were started as CGI?
                     55: static bool mail_received=false; ///< we were started with -m option? [asked to parse incoming message to $mail:received]
1.5       paf        56: 
1.201     paf        57: // for signal handlers
                     58: Request *request=0;
1.218     paf        59: Request_info *request_info=0;
                     60: bool execution_canceled=false;
1.201     paf        61: 
1.46      paf        62: // SAPI
1.86      parser     63: 
1.218     paf        64: class SAPI_Info{} SAPI_info;
                     65: 
                     66: static void log(const char* fmt, va_list args) {
1.193     paf        67:        bool opened=false;
1.61      paf        68:        FILE *f=0;
                     69: 
1.226     paf        70:        const char* log_by_env=getenv(PARSER_LOG_ENV_NAME);
                     71:        if(!log_by_env)
                     72:                log_by_env=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME);
                     73:        if(log_by_env) {
                     74:                f=fopen(log_by_env, "at");
                     75:                opened=f!=0;
                     76:        }
1.233     paf        77: #ifdef PA_DEBUG_CGI_ENTRY_EXIT 
                     78:        f=fopen(PA_DEBUG_CGI_ENTRY_EXIT, "at");
                     79:        opened=f!=0;
                     80: #endif
1.226     paf        81: 
                     82:        if(!opened && config_filespec_cstr) {
1.193     paf        83:                char beside_config_path[MAX_STRING];
                     84:                strncpy(beside_config_path, config_filespec_cstr, MAX_STRING-1);  beside_config_path[MAX_STRING-1]=0;
                     85:                if(!(
                     86:                        rsplit(beside_config_path, '/') || 
                     87:                        rsplit(beside_config_path, '\\'))) { // strip filename
                     88:                        // no path, just filename
                     89:                        beside_config_path[0]='.'; beside_config_path[1]=0;
                     90:                }
                     91: 
                     92:                char file_spec[MAX_STRING];
                     93:                snprintf(file_spec, MAX_STRING, 
                     94:                        "%s/parser3.log", beside_config_path);
                     95:                f=fopen(file_spec, "at");
                     96:                opened=f!=0;
                     97:        }
1.192     paf        98:        // fallback to stderr
1.61      paf        99:        if(!opened)
                    100:                f=stderr;
1.208     paf       101: 
                    102:        // use no memory [so that we could log out-of-memory error]
                    103:        setbuf(f, 0); // stderr stream is unbuffered by default, but still...
1.61      paf       104: 
                    105:        // prefix
                    106:        time_t t=time(0);
1.218     paf       107:        if(const char* stamp=ctime(&t)) { // never saw that
1.173     paf       108:                if(size_t len=strlen(stamp)) // saw once stamp being =""
1.233     paf       109:                        fprintf(f, "[%.*s] [%u] ", len-1, stamp,
1.236     paf       110:                        (unsigned int)getpid()
1.233     paf       111:                        );
1.171     paf       112:        }
1.61      paf       113:        // message
1.117     parser    114: 
1.246     misha     115:        char buf[MAX_LOG_STRING];
                    116:        size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args);
                    117:        size=remove_crlf(buf, buf+size);
1.239     paf       118:        fwrite(buf, size, 1, f);
                    119: 
                    120:        if(request_info)
                    121:                fprintf(f, " [uri=%s, method=%s, cl=%u]",
                    122:                        request_info->uri? request_info->uri: "<unknown>",
                    123:                        request_info->method? request_info->method: "<unknown>",
                    124:                        request_info->content_length);
                    125:        else
                    126:                fputs(" [no request info]", f);
1.117     parser    127: 
1.61      paf       128:        // newline
1.239     paf       129:        fputs("\n", f);
1.61      paf       130: 
                    131:        if(opened)
                    132:                fclose(f);
1.85      parser    133:        else
                    134:                fflush(f);
1.124     parser    135: }
1.236     paf       136: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
1.233     paf       137: static void log(const char* fmt, ...) {
                    138:     va_list args;
                    139:        va_start(args,fmt);
                    140:        log(fmt, args);
                    141:        va_end(args);
                    142: }
1.236     paf       143: #endif
1.124     parser    144: 
                    145: // appends to parser3.log located beside my binary if openable, to stderr otherwize
1.218     paf       146: void SAPI::log(SAPI_Info&, const char* fmt, ...) {
1.124     parser    147:     va_list args;
                    148:        va_start(args,fmt);
                    149:        ::log(fmt, args);
                    150:        va_end(args);
                    151: }
                    152: 
1.218     paf       153: static void die_or_abort(const char* fmt, va_list args, bool write_core) {
1.138     paf       154:        // log
                    155: 
                    156:        // logging is more important than user 
1.204     paf       157:        // she can cancel download, we'd get SIGPIPE, 
1.138     paf       158:        // nothing would be logged then
1.124     parser    159:        ::log(fmt, args);
                    160: 
1.138     paf       161:        // inform user
                    162: 
1.134     paf       163:        char body[MAX_STRING];
1.138     paf       164:        int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134     paf       165: 
                    166:        // prepare header
                    167:        // let's be honest, that's bad we couldn't produce valid output
1.253     misha     168:        SAPI::add_header_attribute(SAPI_info, HTTP_STATUS, "500");
1.248     misha     169:        SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_TYPE, "text/plain");
1.253     misha     170:        SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_LENGTH, format(content_length, "%u"));
1.134     paf       171: 
                    172:        // send header
1.218     paf       173:        SAPI::send_header(SAPI_info);
1.134     paf       174: 
                    175:        // body
1.218     paf       176:        SAPI::send_body(SAPI_info, body, content_length);
1.134     paf       177: 
1.218     paf       178:        // exit & try to produce core dump[unix] or invoke debugger[Win32 Debug version]
                    179:        if(write_core) {
                    180: #if defined(WIN32) && !defined(_DEBUG)
                    181:                // IIS with abort failes to show STDOUT, it just barks "abnormal program termination"
                    182:                exit(1);
1.214     paf       183: #else
1.218     paf       184: #if _MSC_VER
                    185:                _asm int 3;
                    186: #endif
                    187:                abort();
1.214     paf       188: #endif
1.218     paf       189:        } 
                    190:        else
                    191:                exit(1);
                    192: }
                    193: 
                    194: void SAPI::die(const char* fmt, ...) {
                    195:     va_list args;
                    196:        va_start(args, fmt);
                    197:        die_or_abort(fmt, args, false/*write core?*/);
1.238     paf       198: //unreachable anyway   va_end(args);
1.61      paf       199: }
                    200: 
1.218     paf       201: void SAPI::abort(const char* fmt, ...) {
                    202:     va_list args;
                    203:        va_start(args, fmt);
                    204:        die_or_abort(fmt, args, true/*write core?*/);
1.238     paf       205: //unreachable anyway   va_end(args);
1.28      paf       206: }
                    207: 
1.218     paf       208: char* SAPI::get_env(SAPI_Info& , const char* name) {
                    209:        if(char *local=getenv(name))
                    210:                return pa_strdup(local);
                    211:        else
                    212:                return 0;
                    213: }
                    214: 
                    215: const char* const *SAPI::environment(SAPI_Info&) {
1.180     paf       216: #ifdef _MSC_VER
                    217:        extern char **_environ;
                    218:        return _environ;
                    219: #else
                    220:        extern char **environ;
                    221:        return environ;
                    222: #endif
                    223: }
                    224: 
1.218     paf       225: size_t SAPI::read_post(SAPI_Info& , char *buf, size_t max_bytes) {
1.59      paf       226:        size_t read_size=0;
1.12      paf       227:        do {
1.200     paf       228:                ssize_t chunk_size=read(fileno(stdin), 
1.42      paf       229:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129     paf       230:                if(chunk_size<=0)
1.12      paf       231:                        break;
                    232:                read_size+=chunk_size;
                    233:        } while(read_size<max_bytes);
                    234: 
                    235:        return read_size;
1.10      paf       236: }
                    237: 
1.218     paf       238: void SAPI::add_header_attribute(SAPI_Info& , const char* dont_store_key, const char* dont_store_value) {
1.242     misha     239:        if( cgi && (!request || !request->console.was_used()) )
1.254   ! misha     240:                printf("%s: %s\n", capitalize(dont_store_key), dont_store_value);
1.19      paf       241: }
                    242: 
1.56      paf       243: /// @todo intelligent cache-control
1.218     paf       244: void SAPI::send_header(SAPI_Info& ) {
1.33      paf       245:        if(cgi) {
1.147     paf       246: //             puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       247: 
                    248:                // header | body  delimiter
1.20      paf       249:                puts("");
1.33      paf       250:        }
1.30      paf       251: }
1.20      paf       252: 
1.229     paf       253: size_t SAPI::send_body(SAPI_Info& , const void *buf, size_t size) {
                    254:        return stdout_write(buf, size);
1.58      paf       255: }
                    256: 
1.97      parser    257: //
                    258: 
1.218     paf       259: static void full_file_spec(const char* file_name, char *buf, size_t buf_size) {
1.210     paf       260:        if(file_name)
1.167     paf       261:                if(file_name[0]=='/' 
                    262: #ifdef WIN32
1.210     paf       263:                        || file_name[0] && file_name[1]==':'
1.167     paf       264: #endif
1.210     paf       265:                        )
1.218     paf       266:                                strncpy(buf, file_name, buf_size);
1.167     paf       267:                else {
                    268:                        char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    269:                        snprintf(buf, buf_size, "%s/%s", cwd, file_name);
                    270:                }
                    271:        else
                    272:                buf[0]=0;
1.166     paf       273: #ifdef WIN32
                    274:        back_slashes_to_slashes(buf);
                    275: #endif
1.97      parser    276: }
                    277: 
1.218     paf       278: static void log_signal(const char* signal_name) {
                    279:        if(request_info)
1.219     paf       280:                SAPI::log(SAPI_info, "%s received while %s. uri=%s, method=%s, cl=%u",
1.218     paf       281:                        signal_name,
                    282:                        request?"executing code":"reading data",
                    283:                        request_info->uri,
                    284:                        request_info->method,
                    285:                        request_info->content_length);
                    286:        else
                    287:                SAPI::log(SAPI_info, "%s received before or after processing request",
                    288:                        signal_name);
1.205     paf       289: }
                    290: 
1.201     paf       291: #ifdef SIGUSR1
1.205     paf       292: static void SIGUSR1_handler(int /*sig*/){
                    293:        log_signal("SIGUSR1");
1.201     paf       294: }
                    295: #endif
                    296: 
                    297: #ifdef SIGPIPE
1.243     misha     298: #define SIGPIPE_NAME "SIGPIPE"
                    299: static const String sigpipe_name(SIGPIPE_NAME);
1.205     paf       300: static void SIGPIPE_handler(int /*sig*/){
1.243     misha     301:        Value* sigpipe=0;
                    302:        if(request)
1.251     misha     303:                sigpipe=request->main_class.get_element(sigpipe_name);
1.243     misha     304:        if(sigpipe && sigpipe->as_bool())
1.244     misha     305:                log_signal(SIGPIPE_NAME);
1.243     misha     306: 
1.218     paf       307:        execution_canceled=true;
1.201     paf       308:        if(request)
1.218     paf       309:                request->set_interrupted(true);
1.201     paf       310: }
                    311: #endif
                    312: 
1.227     paf       313: #ifdef WIN32
                    314: const char* maybe_reconstruct_IIS_status_in_qs(const char* original) 
                    315: {
1.228     paf       316:        // 404;http://servername/page[?param=value...]
1.227     paf       317:        // ';' should be urlencoded by HTTP standard, so we shouldn't get it from browser 
                    318:        // and can consider that as an indication that this is IIS way to report errors
                    319: 
1.228     paf       320:        if(original 
1.230     paf       321:                && isdigit((unsigned char)original[0])
                    322:                && isdigit((unsigned char)original[1])
                    323:                && isdigit((unsigned char)original[2])
1.227     paf       324:                && original[3]==';') 
                    325:        {
                    326:                size_t original_len=strlen(original);
                    327:         char* reconstructed=new(PointerFreeGC) char[original_len
                    328:                        +12/*IIS-STATUS=&*/
                    329:                        +14/*IIS-DOCUMENT=&*/
                    330:                        +1];
                    331:                char* cur=reconstructed;
                    332:                memcpy(cur, "IIS-STATUS=", 11);  cur+=11;
                    333:                memcpy(cur, original, 3); cur+=3;
                    334:                *cur++='&';
                    335: 
1.228     paf       336:                const char* qmark_at=strchr(original, '?');
1.227     paf       337:                memcpy(cur, "IIS-DOCUMENT=", 13);  cur+=13;
                    338:                {
                    339:                        size_t value_len=(qmark_at? qmark_at-original: original_len)-4;
                    340:                        memcpy(cur, original+4, value_len);  cur+=value_len;
                    341:                }
                    342: 
                    343:                if(qmark_at) {
                    344:                        *cur++='&';
                    345:                        strcpy(cur, qmark_at+1/*skip ? itself*/);
                    346:                } else
                    347:                        *cur=0;
                    348: 
                    349:                return reconstructed;
                    350:        }
                    351:        
                    352:        return original;
                    353: }
                    354: #endif
                    355: 
1.40      paf       356: /**
1.122     parser    357: main workhorse
1.19      paf       358: 
1.122     parser    359:   @todo 
1.40      paf       360:                IIS: remove trailing default-document[index.html] from $request.uri.
                    361:                to do that we need to consult metabase,
                    362:                wich is tested but seems slow.
                    363: */
1.218     paf       364: static void real_parser_handler(const char* filespec_to_process,
1.231     paf       365:                                const char* request_method, bool header_only) 
                    366: {
1.122     parser    367:        // init socks
1.225     paf       368:        pa_socks_init();
1.231     paf       369: 
1.122     parser    370:        // init global variables
1.218     paf       371:        pa_globals_init();
1.122     parser    372:        
1.186     paf       373:        if(!filespec_to_process || !*filespec_to_process)
1.233     paf       374:                SAPI::die("Parser/%s"
                    375: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
                    376:                " with entry/exit tracing"
                    377: #endif
                    378:                , PARSER_VERSION);
1.122     parser    379:        
                    380:        // Request info
1.218     paf       381:        Request_info request_info;  memset(&request_info, 0, sizeof(request_info));
1.166     paf       382:        char document_root_buf[MAX_STRING];
1.122     parser    383:        if(cgi) {
1.218     paf       384:                if(const char* env_document_root=getenv("DOCUMENT_ROOT"))
1.122     parser    385:                        request_info.document_root=env_document_root;
1.218     paf       386:                else if(const char* path_info=getenv("PATH_INFO")) {
1.122     parser    387:                        // IIS
1.166     paf       388:                        size_t len=min(sizeof(document_root_buf)-1, strlen(filespec_to_process)-strlen(path_info));
                    389:                        memcpy(document_root_buf, filespec_to_process, len); document_root_buf[len]=0;
                    390:                        request_info.document_root=document_root_buf;
1.122     parser    391:                } else
1.243     misha     392:                        throw Exception(PARSER_RUNTIME,
1.165     paf       393:                                0,
                    394:                                "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.122     parser    395:        } else {
1.166     paf       396:                full_file_spec("", document_root_buf, sizeof(document_root_buf));
                    397:                request_info.document_root=document_root_buf;
1.122     parser    398:        }
                    399:        request_info.path_translated=filespec_to_process;
                    400:        request_info.method=request_method ? request_method : "GET";
1.227     paf       401:        const char* query_string=
                    402: #ifdef WIN32
                    403:                maybe_reconstruct_IIS_status_in_qs
                    404: #endif
                    405:                (getenv("QUERY_STRING"));
1.122     parser    406:        request_info.query_string=query_string;
                    407:        if(cgi) {
1.214     paf       408:                // few absolute obligatory
1.218     paf       409:                const char* path_info=getenv("PATH_INFO");
1.214     paf       410:                if(!path_info)
                    411:                        SAPI::die("CGI: illegal call (missing PATH_INFO)");
1.218     paf       412:                const char* script_name=getenv("SCRIPT_NAME");
1.214     paf       413:                if(!script_name)
                    414:                                SAPI::die("CGI: illegal call (missing SCRIPT_NAME)");
                    415: 
1.218     paf       416:                const char* env_request_uri=getenv("REQUEST_URI");
1.214     paf       417:                if(env_request_uri)
1.122     parser    418:                        request_info.uri=env_request_uri;
1.214     paf       419:                else 
1.122     parser    420:                        if(query_string) {
1.218     paf       421:                                char* reconstructed_uri=new(PointerFreeGC) char[
1.122     parser    422:                                        strlen(path_info)+1/*'?'*/+
1.218     paf       423:                                        strlen(query_string)+1/*0*/];
1.122     parser    424:                                strcpy(reconstructed_uri, path_info);
                    425:                                strcat(reconstructed_uri, "?");
                    426:                                strcat(reconstructed_uri, query_string);
                    427:                                request_info.uri=reconstructed_uri;
                    428:                        } else
                    429:                                request_info.uri=path_info;
1.214     paf       430: 
                    431:                if(env_request_uri) { // apache & others stuck to standards
                    432:                        /*
                    433:                                http://parser3/env.html?123  =OK
                    434:                                $request:uri=/env.html?123
                    435:                                REQUEST_URI='/env.html?123'
                    436:                                SCRIPT_NAME='/cgi-bin/parser3'
                    437:                                PATH_INFO='/env.html'
                    438: 
                    439:                                http://parser3/cgi-bin/parser3/env.html?123 =ERROR
                    440:                                $request:uri=/cgi-bin/parser3/env.html?123
                    441:                                REQUEST_URI='/cgi-bin/parser3/env.html?123'
                    442:                                SCRIPT_NAME='/cgi-bin/parser3'
                    443:                                PATH_INFO='/env.html'
                    444:                        */
                    445:                        size_t script_name_len=strlen(script_name);
                    446:                        size_t uri_len=strlen(env_request_uri);
                    447:                        if(strncmp(env_request_uri, script_name, script_name_len)==0 &&
                    448:                                script_name_len != uri_len) // under IIS they are the same
                    449:                                SAPI::die("CGI: illegal call (1)");
                    450:                } else { // seen on IIS5
                    451:                        /*
                    452:                                http://nestle/env.html?123 =OK
                    453:                                $request:uri=/env.html?123
                    454:                                REQUEST_URI=''
                    455:                                SCRIPT_NAME='/env.html'
                    456:                                PATH_INFO='/env.html'
                    457: 
                    458:                                http://nestle/cgi-bin/parser3.exe/env.html =ERROR
                    459:                                $request:uri=/env.html
                    460:                                REQUEST_URI=''
                    461:                                SCRIPT_NAME='/cgi-bin/parser3.exe'
                    462:                                PATH_INFO='/env.html'
                    463:                        */
                    464:                        if(strcmp(script_name, path_info)!=0)
                    465:                                SAPI::die("CGI: illegal call (2)");
                    466:                }
1.122     parser    467:        } else
1.177     paf       468:                request_info.uri="";
1.122     parser    469:        
1.218     paf       470:        request_info.content_type=getenv("CONTENT_TYPE");
                    471:        const char* content_length=getenv("CONTENT_LENGTH");
1.122     parser    472:        request_info.content_length=(content_length?atoi(content_length):0);
1.218     paf       473:        request_info.cookie=getenv("HTTP_COOKIE");
1.184     paf       474:        request_info.mail_received=mail_received;
                    475: 
1.245     misha     476:        request_info.argv=argv_all;
                    477:        request_info.args_skip=args_skip;
                    478: 
1.218     paf       479:        // get request_info ptr for signal handlers
                    480:        ::request_info=&request_info;
                    481:        if(execution_canceled)
                    482:                SAPI::die("Execution canceled");
1.198     paf       483: 
1.233     paf       484: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
                    485:        log("request_info: method=%s, uri=%s, q=%s, dr=%s, pt=%s, cookies=%s, cl=%u", 
                    486:                request_info.method,
                    487:                request_info.uri,
                    488:                request_info.query_string,
                    489:                request_info.document_root,
                    490:                request_info.path_translated,
                    491:                request_info.cookie,
                    492:                request_info.content_length);
                    493: #endif
                    494: 
1.122     parser    495:        // prepare to process request
1.221     paf       496:        Request request(SAPI_info, request_info,
1.218     paf       497:                cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS,
1.130     paf       498:                true /* status_allowed */);
1.201     paf       499: 
                    500:        // get request ptr for signal handlers
                    501:        ::request=&request;
                    502: 
1.181     paf       503:        char config_filespec_buf[MAX_STRING];
1.193     paf       504:        if(!config_filespec_cstr) {
1.218     paf       505:                const char* config_by_env=getenv(PARSER_CONFIG_ENV_NAME);
1.193     paf       506:                if(!config_by_env)
                    507:                        config_by_env=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
                    508:                if(config_by_env)
                    509:                        config_filespec_cstr=config_by_env;
                    510:                else {
                    511:                        // beside by binary
                    512:                        char beside_binary_path[MAX_STRING];
                    513:                        strncpy(beside_binary_path, argv0, MAX_STRING-1);  beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
                    514:                        if(!(
                    515:                                rsplit(beside_binary_path, '/') || 
                    516:                                rsplit(beside_binary_path, '\\'))) { // strip filename
                    517:                                // no path, just filename
                    518:                                // @todo full path, not ./!
                    519:                                beside_binary_path[0]='.'; beside_binary_path[1]=0;
                    520:                        }
                    521:                        snprintf(config_filespec_buf, MAX_STRING, 
                    522:                                "%s/%s", 
                    523:                                beside_binary_path, AUTO_FILE_NAME);
                    524:                        config_filespec_cstr=config_filespec_buf;
1.197     paf       525:                        fail_on_config_read_problem=entry_exists(config_filespec_cstr);
1.193     paf       526:                }
1.122     parser    527:        }
                    528:        
                    529:        // process the request
                    530:        request.core(
1.193     paf       531:                config_filespec_cstr, fail_on_config_read_problem,
1.122     parser    532:                header_only);
1.201     paf       533: 
                    534:        // no request [prevent signal handlers from accessing invalid memory]
                    535:        ::request=0;
1.231     paf       536: 
1.225     paf       537:        // finalize global variables
                    538:        pa_globals_done();
                    539: 
1.122     parser    540:        //
1.225     paf       541:        pa_socks_done();
1.218     paf       542: }
                    543: 
1.231     paf       544: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
                    545: static const Exception 
                    546: call_real_parser_handler__do_PEH_return_it(
                    547:        const char* filespec_to_process,
                    548:        const char* request_method, bool header_only) 
                    549: {
                    550:        try {
1.122     parser    551:                real_parser_handler(
                    552:                        filespec_to_process,
                    553:                        request_method, header_only);
1.231     paf       554:        } catch(const Exception& e) {
                    555:                return e;
1.122     parser    556:        }
1.231     paf       557: 
                    558:        return Exception();
1.122     parser    559: }
1.231     paf       560: static void call_real_parser_handler__supress_system_exception(
                    561:        const char* filespec_to_process,
                    562:        const char* request_method, bool header_only) 
                    563: {
                    564:        Exception parser_exception;
                    565:        LPEXCEPTION_POINTERS system_exception=0;
1.122     parser    566: 
1.231     paf       567:        __try {
                    568:                parser_exception=call_real_parser_handler__do_PEH_return_it(
                    569:                        filespec_to_process,
1.232     paf       570:                        request_method, header_only);
1.231     paf       571:        } __except (
                    572:                (system_exception=GetExceptionInformation()), 
                    573:                EXCEPTION_EXECUTE_HANDLER) 
                    574:        {
1.131     paf       575: 
1.231     paf       576:                if(system_exception)
                    577:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
                    578:                                throw Exception("system",
1.218     paf       579:                                        0,
1.231     paf       580:                                        "0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
1.218     paf       581:                        else
1.231     paf       582:                                throw Exception("system", 
1.218     paf       583:                                        0, 
1.231     paf       584:                                        "<no exception record>");
1.218     paf       585:                else
1.231     paf       586:                        throw Exception("system", 
1.218     paf       587:                                0, 
1.231     paf       588:                                "<no exception information>");
                    589:        }
                    590: 
                    591:        if(parser_exception)
                    592:                throw Exception(parser_exception);
                    593: }
1.218     paf       594: #endif
1.131     paf       595: 
1.218     paf       596: static void usage(const char* program) {
1.188     paf       597:        printf(
1.235     paf       598:                "Parser/%s\n"
1.252     misha     599:                "Copyright(c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com)\n"
1.184     paf       600:                "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
                    601:                "\n"
                    602:                "Usage: %s [options] file\n"
                    603:                "Options are:\n"
1.185     paf       604: #ifdef WITH_MAILRECEIVE
1.193     paf       605:                "    -m              Parse mail, put received letter to $mail:received\n"
1.184     paf       606: #endif
1.193     paf       607:                "    -f config_file  Use this config file (/path/to/auto.p)\n"
                    608:                "    -h              Display usage information (this message)\n"
1.184     paf       609:                , PARSER_VERSION, 
                    610:                program);
                    611:        exit(EINVAL);
                    612: }
                    613: 
1.249     misha     614: int main(int argc, char *argv[]) {
1.233     paf       615: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
                    616:        log("main: entry");
                    617: #endif
1.224     paf       618:        //_asm int 3;
1.218     paf       619:        GC_java_finalization=0;
1.217     paf       620: 
1.218     paf       621: #ifndef PA_DEBUG_DISABLE_GC
                    622:        // Dont collect unless explicitly requested 
                    623:        // this is quicker (~30% ), but less memory-efficient(~8%)
                    624:        // so deciding for speed
                    625:        GC_dont_gc=1; 
                    626: #endif
                    627: /*
                    628:        
                    629:        Array<int> test;
                    630:        test+=3;
                    631:        test+=4;
                    632: //     int a=test.count();
                    633:        int i=0;
                    634:        scanf("%d", &i);
                    635:        int b=test.get(i);
                    636: //     int b=test.get(10);
                    637:        printf("%d", b);//test.count());*/
1.217     paf       638: 
1.203     paf       639: #ifdef SIGUSR1
1.205     paf       640:     if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR)
1.203     paf       641:                SAPI::die("Can not set handler for SIGUSR1");
                    642: #endif
                    643: #ifdef SIGPIPE
1.205     paf       644:     if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR)
1.203     paf       645:                SAPI::die("Can not set handler for SIGPIPE");
                    646: #endif
                    647: 
                    648: 
1.185     paf       649: #ifdef DEBUG_MAILRECEIVE
                    650:        if(FILE *fake_in=fopen(DEBUG_MAILRECEIVE, "rt")) {
1.184     paf       651:                dup2(fake_in->_file, 0/*STDIN_FILENO*/);
                    652:        }
                    653: #endif
                    654: 
1.178     paf       655: #ifdef _DEBUG
1.220     paf       656:        //_crtBreakAlloc=46;
1.178     paf       657: #endif
1.245     misha     658:        argv_all=argv;
1.193     paf       659:        argv0=argv[0];
1.45      paf       660: 
1.32      paf       661:        umask(2);
                    662: 
1.3       paf       663:        // were we started as CGI?
1.146     paf       664:        cgi=
1.109     parser    665:                getenv("SERVER_SOFTWARE") || 
                    666:                getenv("SERVER_NAME") || 
                    667:                getenv("GATEWAY_INTERFACE") || 
                    668:                getenv("REQUEST_METHOD");
1.5       paf       669:        
1.184     paf       670:        char *raw_filespec_to_process;
1.210     paf       671:        if(cgi) {
1.184     paf       672:                raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.210     paf       673:                if(raw_filespec_to_process && !*raw_filespec_to_process)
                    674:                        raw_filespec_to_process=0;
                    675:        } else {
1.250     misha     676:                int optind=1;
1.245     misha     677:                while(optind < argc){
                    678:                        char *carg = argv[optind];
                    679:                        if(carg[0] != '-')
1.184     paf       680:                                break;
1.245     misha     681: 
                    682:                        for(size_t k = 1; k < strlen(carg); k++){
                    683:                                char c = carg[k];
                    684:                                switch (c) {
                    685:                                        case 'h':
                    686:                                                usage(argv[0]);
                    687:                                                break;
                    688:                                        case 'f':
                    689:                                                if(optind < argc - 1){
                    690:                                                        optind++;
                    691:                                                        config_filespec_cstr=argv[optind];
                    692:                                                }
                    693:                                                break;
1.185     paf       694: #ifdef WITH_MAILRECEIVE
1.245     misha     695:                                        case 'm':
                    696:                                                mail_received=true;
                    697:                                                break;
                    698: #endif
                    699:                                        default:
                    700:                                                fprintf(stderr, "%s: invalid option '%c'\n", argv[0], c);
                    701:                                                usage(argv[0]);
                    702:                                                break;
                    703:                                }
1.184     paf       704:                        }
1.245     misha     705:                        optind++;
1.184     paf       706:                }
1.245     misha     707:                
                    708:                if (optind > argc - 1) {
1.184     paf       709:                        fprintf(stderr, "%s: file not specified\n", argv[0]);
                    710:                        usage(argv[0]);
1.10      paf       711:                }
1.245     misha     712:                raw_filespec_to_process=argv[optind];
                    713:                args_skip=optind;
1.10      paf       714:        }
                    715: 
1.100     parser    716: #ifdef WIN32
                    717:        setmode(fileno(stdin), _O_BINARY);
                    718:        setmode(fileno(stdout), _O_BINARY);
                    719:        setmode(fileno(stderr), _O_BINARY);
                    720: #endif
                    721: 
1.218     paf       722: #if defined(_MSC_VER) && defined(_DEBUG)
1.148     paf       723:        // Get current flag
                    724:        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
                    725: 
                    726:        // Turn on leak-checking bit
                    727:        tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
                    728: 
                    729:        // Set flag to the new value
                    730:        _CrtSetDbgFlag( tmpFlag );
1.218     paf       731: //     _CrtSetBreakAlloc(61);
1.138     paf       732: 
1.218     paf       733:        _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
                    734:        _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138     paf       735: #endif
                    736: 
1.166     paf       737:        char filespec_to_process[MAX_STRING];
                    738:        full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10      paf       739: 
1.218     paf       740:        const char* request_method=getenv("REQUEST_METHOD");
1.35      paf       741:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131     paf       742: 
1.122     parser    743:        try { // global try
1.231     paf       744: #ifdef PA_SUPPRESS_SYSTEM_EXCEPTION
                    745:                call_real_parser_handler__supress_system_exception(
                    746: #else
                    747:                real_parser_handler(
                    748: #endif
1.122     parser    749:                        filespec_to_process,
                    750:                        request_method, header_only);
                    751:        } catch(const Exception& e) { // global problem 
1.44      paf       752:                // don't allocate anything on pool here:
                    753:                //   possible pool' exception not catch-ed now
                    754:                //   and there could be out-of-memory exception
1.218     paf       755:                char buf[MAX_STRING];
1.232     paf       756:                snprintf(buf, MAX_STRING, "Unhandled exception %s",
                    757:                        e.comment());
1.218     paf       758:                // log it
                    759:                SAPI::log(SAPI_info, "%s", buf);
                    760: 
                    761:                //
                    762:                int content_length=strlen(buf);
                    763: 
                    764:                // prepare header
1.248     misha     765:                SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_TYPE, "text/plain");
1.253     misha     766:                SAPI::add_header_attribute(SAPI_info, HTTP_CONTENT_LENGTH, format(content_length, "%u"));
1.218     paf       767: 
                    768:                // send header
                    769:                SAPI::send_header(SAPI_info);
                    770: 
                    771:                // send body
                    772:                if(!header_only)
                    773:                        SAPI::send_body(SAPI_info, buf, content_length);
1.43      paf       774: 
1.218     paf       775:                // unsuccessful finish
1.16      paf       776:        }
1.109     parser    777: 
1.233     paf       778: #ifdef PA_DEBUG_CGI_ENTRY_EXIT
                    779:        log("main: successful return");
                    780: #endif
1.134     paf       781:        return 0;
1.1       paf       782: }

E-mail: