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

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

E-mail: