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

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

E-mail: