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

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

E-mail: