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

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.216.2.1  paf         4:        Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.154     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.189     paf         6: */
1.27      paf         7: 
1.216.2.30.2.  (paf        8:): static const char* IDENT_PARSER3_C="$Date: 2003/03/26 07:45:24 $";
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.216.2.30  paf        23: 
                     24: 
1.149     paf        25: #ifdef WIN32
                     26: #      include <windows.h>
1.184     paf        27: #      include "getopt.h"
                     28: #else
                     29: #      include <getopt.h>
1.120     parser     30: #endif
                     31: 
1.158     paf        32: //#define DEBUG_POOL_MALLOC
1.196     paf        33: //#define DEBUG_MAILRECEIVE "mailreceive.eml"
1.160     paf        34: 
1.109     parser     35: // consts
1.84      parser     36: 
1.175     paf        37: #define REDIRECT_PREFIX "REDIRECT_"
1.181     paf        38: #define PARSER_CONFIG_ENV_NAME "CGI_PARSER_CONFIG"
1.159     paf        39: 
1.42      paf        40: /// IIS refuses to read bigger chunks
                     41: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M 
                     42: 
1.216.2.1  paf        43: static const char* argv0;
                     44: static const char* config_filespec_cstr=0;
1.193     paf        45: static bool fail_on_config_read_problem=true;
1.192     paf        46: 
1.184     paf        47: static bool cgi; ///< we were started as CGI?
                     48: static bool mail_received=false; ///< we were started with -m option? [asked to parse incoming message to $mail:received]
1.5       paf        49: 
1.201     paf        50: // for signal handlers
                     51: Request *request=0;
1.216.2.26  paf        52: Request_info *request_info=0;
                     53: bool execution_canceled=false;
1.201     paf        54: 
1.46      paf        55: // SAPI
1.86      parser     56: 
1.216.2.3  paf        57: class SAPI_Info{} SAPI_info;
                     58: 
1.216.2.1  paf        59: static void log(const char* fmt, va_list args) {
1.193     paf        60:        bool opened=false;
1.61      paf        61:        FILE *f=0;
                     62: 
1.193     paf        63:        if(config_filespec_cstr) {
                     64:                char beside_config_path[MAX_STRING];
                     65:                strncpy(beside_config_path, config_filespec_cstr, MAX_STRING-1);  beside_config_path[MAX_STRING-1]=0;
                     66:                if(!(
                     67:                        rsplit(beside_config_path, '/') || 
                     68:                        rsplit(beside_config_path, '\\'))) { // strip filename
                     69:                        // no path, just filename
                     70:                        beside_config_path[0]='.'; beside_config_path[1]=0;
                     71:                }
                     72: 
                     73:                char file_spec[MAX_STRING];
                     74:                snprintf(file_spec, MAX_STRING, 
                     75:                        "%s/parser3.log", beside_config_path);
                     76:                f=fopen(file_spec, "at");
                     77:                opened=f!=0;
                     78:        }
1.192     paf        79:        // fallback to stderr
1.61      paf        80:        if(!opened)
                     81:                f=stderr;
1.208     paf        82: 
                     83:        // use no memory [so that we could log out-of-memory error]
                     84:        setbuf(f, 0); // stderr stream is unbuffered by default, but still...
1.61      paf        85: 
                     86:        // prefix
                     87:        time_t t=time(0);
1.216.2.1  paf        88:        if(const char* stamp=ctime(&t)) { // never saw that
1.173     paf        89:                if(size_t len=strlen(stamp)) // saw once stamp being =""
1.172     paf        90:                        fprintf(f, "[%.*s] ", len-1, stamp);
1.171     paf        91:        }
1.61      paf        92:        // message
1.117     parser     93: 
                     94:        char buf[MAX_STRING];
                     95:        size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
                     96:        remove_crlf(buf, buf+size);
                     97: 
                     98:        fwrite(buf, size, 1, f);
1.61      paf        99:        // newline
                    100:        fprintf(f, "\n");
                    101: 
                    102:        if(opened)
                    103:                fclose(f);
1.85      parser    104:        else
                    105:                fflush(f);
1.124     parser    106: }
                    107: 
                    108: // appends to parser3.log located beside my binary if openable, to stderr otherwize
1.216.2.3  paf       109: void SAPI::log(SAPI_Info&, const char* fmt, ...) {
1.124     parser    110:     va_list args;
                    111:        va_start(args,fmt);
                    112:        ::log(fmt, args);
                    113:        va_end(args);
                    114: }
                    115: 
1.216.2.18  paf       116: static void die_or_abort(const char* fmt, va_list args, bool write_core) {
1.216.2.11  paf       117:        //_asm int 3;
1.137     paf       118: #ifdef DEBUG_POOL_MALLOC
1.216.2.30.2.  (paf      119:):      extern void log_pool_stats();
1.216.2.3  paf       120:        log_pool_stats(SAPI_info);
1.137     paf       121: #endif
                    122: 
1.138     paf       123:        // log
                    124: 
                    125:        // logging is more important than user 
1.204     paf       126:        // she can cancel download, we'd get SIGPIPE, 
1.138     paf       127:        // nothing would be logged then
1.124     parser    128:        ::log(fmt, args);
                    129: 
1.138     paf       130:        // inform user
                    131: 
1.134     paf       132:        char body[MAX_STRING];
1.138     paf       133:        int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134     paf       134: 
                    135:        // prepare header
                    136:        // let's be honest, that's bad we couldn't produce valid output
1.216.2.3  paf       137:        SAPI::add_header_attribute(SAPI_info, "status", "500");
                    138:        SAPI::add_header_attribute(SAPI_info, "content-type", "text/plain");
1.134     paf       139:        char content_length_cstr[MAX_NUMBER];
1.168     paf       140:        snprintf(content_length_cstr, sizeof(content_length_cstr), "%u", content_length);
1.216.2.3  paf       141:        SAPI::add_header_attribute(SAPI_info, "content-length", content_length_cstr);
1.134     paf       142: 
                    143:        // send header
1.216.2.3  paf       144:        SAPI::send_header(SAPI_info);
1.134     paf       145: 
                    146:        // body
1.216.2.3  paf       147:        SAPI::send_body(SAPI_info, body, content_length);
1.134     paf       148: 
1.216.2.18  paf       149:        // exit & try to produce core dump[unix] or invoke debugger[Win32 Debug version]
                    150:        if(write_core) {
                    151: #if defined(WIN32) && !defined(_DEBUG)
                    152:                //_asm int 3;
                    153:                // IIS with abort failes to show STDOUT, it just barks "abnormal program termination"
                    154:                exit(1);
1.214     paf       155: #else
1.216.2.18  paf       156:                abort();
1.214     paf       157: #endif
1.216.2.18  paf       158:        } 
                    159:        else
                    160:                exit(1);
                    161: }
                    162: 
                    163: void SAPI::die(const char* fmt, ...) {
                    164:     va_list args;
                    165:        va_start(args, fmt);
                    166:        die_or_abort(fmt, args, false/*write core?*/);
                    167:        va_end(args);
                    168: }
                    169: 
                    170: void SAPI::abort(const char* fmt, ...) {
                    171:     va_list args;
                    172:        va_start(args, fmt);
                    173:        die_or_abort(fmt, args, true/*write core?*/);
                    174:        va_end(args);
1.61      paf       175: }
                    176: 
1.216.2.30.2.  (paf      177:): char* SAPI::get_env(SAPI_Info& , const char* name) {
1.216.2.17  paf       178:        if(char *local=getenv(name))
1.216.2.30.2.  (paf      179:):              return pa_strdup(local);
1.216.2.3  paf       180:        else
1.216.2.17  paf       181:                return 0;
1.28      paf       182: }
                    183: 
1.216.2.30.2.  (paf      184:): const char* const *SAPI::environment(SAPI_Info&) {
1.180     paf       185: #ifdef _MSC_VER
                    186:        extern char **_environ;
                    187:        return _environ;
                    188: #else
                    189:        extern char **environ;
                    190:        return environ;
                    191: #endif
                    192: }
                    193: 
1.216.2.3  paf       194: size_t SAPI::read_post(SAPI_Info& , char *buf, size_t max_bytes) {
1.59      paf       195:        size_t read_size=0;
1.12      paf       196:        do {
1.200     paf       197:                ssize_t chunk_size=read(fileno(stdin), 
1.42      paf       198:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129     paf       199:                if(chunk_size<=0)
1.12      paf       200:                        break;
                    201:                read_size+=chunk_size;
                    202:        } while(read_size<max_bytes);
                    203: 
                    204:        return read_size;
1.10      paf       205: }
                    206: 
1.216.2.3  paf       207: void SAPI::add_header_attribute(SAPI_Info& , const char* dont_store_key, const char* dont_store_value) {
1.216.2.30.2.  (paf      208:):      if(cgi)
1.216.2.2  paf       209:                printf("%s: %s\n", dont_store_key, dont_store_value);
1.19      paf       210: }
                    211: 
1.56      paf       212: /// @todo intelligent cache-control
1.216.2.3  paf       213: void SAPI::send_header(SAPI_Info& ) {
1.216.2.30.2.  (paf      214:):      if(cgi) {
1.147     paf       215: //             puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       216: 
                    217:                // header | body  delimiter
1.20      paf       218:                puts("");
1.33      paf       219:        }
1.30      paf       220: }
1.20      paf       221: 
1.216.2.3  paf       222: void SAPI::send_body(SAPI_Info& , const void *buf, size_t size) {
1.19      paf       223:        stdout_write(buf, size);
1.58      paf       224: }
                    225: 
1.97      parser    226: //
                    227: 
1.216.2.1  paf       228: static void full_file_spec(const char* file_name, char *buf, size_t buf_size) {
1.210     paf       229:        if(file_name)
1.167     paf       230:                if(file_name[0]=='/' 
                    231: #ifdef WIN32
1.210     paf       232:                        || file_name[0] && file_name[1]==':'
1.167     paf       233: #endif
1.210     paf       234:                        )
1.167     paf       235:                        strncpy(buf, file_name, buf_size);
                    236:                else {
                    237:                        char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    238:                        snprintf(buf, buf_size, "%s/%s", cwd, file_name);
                    239:                }
                    240:        else
                    241:                buf[0]=0;
1.166     paf       242: #ifdef WIN32
                    243:        back_slashes_to_slashes(buf);
                    244: #endif
1.97      parser    245: }
                    246: 
1.216.2.1  paf       247: static void log_signal(const char* signal_name) {
1.216.2.26  paf       248:        if(request_info)
                    249:                SAPI::log(SAPI_info, "%s received while %s. uri=%s, method=%s, qs=%s, cl=%u",
                    250:                        signal_name,
                    251:                        request?"executing code":"reading data",
                    252:                        request_info->uri,
                    253:                        request_info->method,
                    254:                        request_info->query_string,
                    255:                        request_info->content_length);
                    256:        else
                    257:                SAPI::log(SAPI_info, "%s received before or after processing request",
                    258:                        signal_name);
1.205     paf       259: }
                    260: 
1.201     paf       261: #ifdef SIGUSR1
1.205     paf       262: static void SIGUSR1_handler(int /*sig*/){
                    263:        log_signal("SIGUSR1");
1.201     paf       264: }
                    265: #endif
                    266: 
                    267: #ifdef SIGPIPE
1.205     paf       268: static void SIGPIPE_handler(int /*sig*/){
                    269:        log_signal("SIGPIPE");
1.216.2.26  paf       270:        execution_canceled=true;
1.201     paf       271:        if(request)
1.216.2.27  paf       272:                request->set_interrupted(true);
1.201     paf       273: }
                    274: #endif
                    275: 
1.40      paf       276: /**
1.122     parser    277: main workhorse
1.19      paf       278: 
1.122     parser    279:   @todo 
1.40      paf       280:                IIS: remove trailing default-document[index.html] from $request.uri.
                    281:                to do that we need to consult metabase,
                    282:                wich is tested but seems slow.
                    283: */
1.184     paf       284: static void real_parser_handler(
1.216.2.1  paf       285:                                        const char* filespec_to_process,
                    286:                                        const char* request_method, bool header_only) {
1.122     parser    287:        // init socks
1.216.2.3  paf       288:        pa_init_socks();
1.122     parser    289:        
                    290:        // init global variables
1.216.2.3  paf       291:        pa_globals_init();
1.122     parser    292:        
1.186     paf       293:        if(!filespec_to_process || !*filespec_to_process)
1.144     paf       294:                SAPI::die("Parser/%s", PARSER_VERSION);
1.122     parser    295:        
                    296:        // Request info
1.216.2.3  paf       297:        Request_info request_info;
1.166     paf       298:        char document_root_buf[MAX_STRING];
1.122     parser    299:        if(cgi) {
1.216.2.17  paf       300:                if(const char* env_document_root=getenv("DOCUMENT_ROOT"))
1.122     parser    301:                        request_info.document_root=env_document_root;
1.216.2.17  paf       302:                else if(const char* path_info=getenv("PATH_INFO")) {
1.122     parser    303:                        // IIS
1.166     paf       304:                        size_t len=min(sizeof(document_root_buf)-1, strlen(filespec_to_process)-strlen(path_info));
                    305:                        memcpy(document_root_buf, filespec_to_process, len); document_root_buf[len]=0;
                    306:                        request_info.document_root=document_root_buf;
1.122     parser    307:                } else
1.165     paf       308:                        throw Exception("parser.runtime",
1.216.2.30.2.  (paf      309:):                              0,
1.165     paf       310:                                "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.122     parser    311:        } else {
1.166     paf       312:                full_file_spec("", document_root_buf, sizeof(document_root_buf));
                    313:                request_info.document_root=document_root_buf;
1.122     parser    314:        }
                    315:        request_info.path_translated=filespec_to_process;
                    316:        request_info.method=request_method ? request_method : "GET";
1.216.2.17  paf       317:        const char* query_string=getenv("QUERY_STRING");
1.122     parser    318:        request_info.query_string=query_string;
                    319:        if(cgi) {
1.214     paf       320:                // few absolute obligatory
1.216.2.17  paf       321:                const char* path_info=getenv("PATH_INFO");
1.214     paf       322:                if(!path_info)
                    323:                        SAPI::die("CGI: illegal call (missing PATH_INFO)");
1.216.2.17  paf       324:                const char* script_name=getenv("SCRIPT_NAME");
1.214     paf       325:                if(!script_name)
                    326:                                SAPI::die("CGI: illegal call (missing SCRIPT_NAME)");
                    327: 
1.216.2.17  paf       328:                const char* env_request_uri=getenv("REQUEST_URI");
1.214     paf       329:                if(env_request_uri)
1.122     parser    330:                        request_info.uri=env_request_uri;
1.214     paf       331:                else 
1.122     parser    332:                        if(query_string) {
1.216.2.30.2.  (paf      333:):                              char* reconstructed_uri=new(PointerFreeGC) char[
1.122     parser    334:                                        strlen(path_info)+1/*'?'*/+
1.216.2.30.2.  (paf      335:):                                      strlen(query_string)+1/*0*/];
1.122     parser    336:                                strcpy(reconstructed_uri, path_info);
                    337:                                strcat(reconstructed_uri, "?");
                    338:                                strcat(reconstructed_uri, query_string);
                    339:                                request_info.uri=reconstructed_uri;
                    340:                        } else
                    341:                                request_info.uri=path_info;
1.214     paf       342: 
                    343:                if(env_request_uri) { // apache & others stuck to standards
                    344:                        /*
                    345:                                http://parser3/env.html?123  =OK
                    346:                                $request:uri=/env.html?123
                    347:                                REQUEST_URI='/env.html?123'
                    348:                                SCRIPT_NAME='/cgi-bin/parser3'
                    349:                                PATH_INFO='/env.html'
                    350: 
                    351:                                http://parser3/cgi-bin/parser3/env.html?123 =ERROR
                    352:                                $request:uri=/cgi-bin/parser3/env.html?123
                    353:                                REQUEST_URI='/cgi-bin/parser3/env.html?123'
                    354:                                SCRIPT_NAME='/cgi-bin/parser3'
                    355:                                PATH_INFO='/env.html'
                    356:                        */
                    357:                        size_t script_name_len=strlen(script_name);
                    358:                        size_t uri_len=strlen(env_request_uri);
                    359:                        if(strncmp(env_request_uri, script_name, script_name_len)==0 &&
                    360:                                script_name_len != uri_len) // under IIS they are the same
                    361:                                SAPI::die("CGI: illegal call (1)");
                    362:                } else { // seen on IIS5
                    363:                        /*
                    364:                                http://nestle/env.html?123 =OK
                    365:                                $request:uri=/env.html?123
                    366:                                REQUEST_URI=''
                    367:                                SCRIPT_NAME='/env.html'
                    368:                                PATH_INFO='/env.html'
                    369: 
                    370:                                http://nestle/cgi-bin/parser3.exe/env.html =ERROR
                    371:                                $request:uri=/env.html
                    372:                                REQUEST_URI=''
                    373:                                SCRIPT_NAME='/cgi-bin/parser3.exe'
                    374:                                PATH_INFO='/env.html'
                    375:                        */
                    376:                        if(strcmp(script_name, path_info)!=0)
                    377:                                SAPI::die("CGI: illegal call (2)");
                    378:                }
1.122     parser    379:        } else
1.177     paf       380:                request_info.uri="";
1.122     parser    381:        
1.216.2.17  paf       382:        request_info.content_type=getenv("CONTENT_TYPE");
                    383:        const char* content_length=getenv("CONTENT_LENGTH");
1.122     parser    384:        request_info.content_length=(content_length?atoi(content_length):0);
1.216.2.17  paf       385:        request_info.cookie=getenv("HTTP_COOKIE");
1.184     paf       386:        request_info.mail_received=mail_received;
                    387: 
1.216.2.26  paf       388:        // get request_info ptr for signal handlers
                    389:        ::request_info=&request_info;
                    390:        if(execution_canceled)
                    391:                SAPI::die("Execution canceled");
1.198     paf       392: 
1.122     parser    393:        // prepare to process request
1.216.2.30.2.  (paf      394:):      Request request(SAPI_info, 
1.122     parser    395:                request_info,
1.184     paf       396: /*#ifdef _DEBUG
1.216.2.30.2.  (paf      397:):              String::L_HTML|String::L_OPTIMIZE_BIT
1.184     paf       398: #else*/
1.216.2.30.2.  (paf      399:):              cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS
1.184     paf       400: /*#endif*/
1.143     paf       401:                ,
1.130     paf       402:                true /* status_allowed */);
1.201     paf       403: 
                    404:        // get request ptr for signal handlers
                    405:        ::request=&request;
                    406: 
1.181     paf       407:        char config_filespec_buf[MAX_STRING];
1.193     paf       408:        if(!config_filespec_cstr) {
1.216.2.1  paf       409:                const char* config_by_env=getenv(PARSER_CONFIG_ENV_NAME);
1.193     paf       410:                if(!config_by_env)
                    411:                        config_by_env=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
                    412:                if(config_by_env)
                    413:                        config_filespec_cstr=config_by_env;
                    414:                else {
                    415:                        // beside by binary
                    416:                        char beside_binary_path[MAX_STRING];
                    417:                        strncpy(beside_binary_path, argv0, MAX_STRING-1);  beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
                    418:                        if(!(
                    419:                                rsplit(beside_binary_path, '/') || 
                    420:                                rsplit(beside_binary_path, '\\'))) { // strip filename
                    421:                                // no path, just filename
                    422:                                // @todo full path, not ./!
                    423:                                beside_binary_path[0]='.'; beside_binary_path[1]=0;
                    424:                        }
                    425:                        snprintf(config_filespec_buf, MAX_STRING, 
                    426:                                "%s/%s", 
                    427:                                beside_binary_path, AUTO_FILE_NAME);
                    428:                        config_filespec_cstr=config_filespec_buf;
1.197     paf       429:                        fail_on_config_read_problem=entry_exists(config_filespec_cstr);
1.193     paf       430:                }
1.122     parser    431:        }
                    432:        
                    433:        // process the request
                    434:        request.core(
1.193     paf       435:                config_filespec_cstr, fail_on_config_read_problem,
1.122     parser    436:                header_only);
1.201     paf       437: 
                    438:        // no request [prevent signal handlers from accessing invalid memory]
                    439:        ::request=0;
1.122     parser    440:        
                    441:        //
1.216.2.3  paf       442:        pa_done_socks();
1.122     parser    443:        
                    444: #ifdef DEBUG_POOL_MALLOC
1.216.2.30.2.  (paf      445:):      extern void log_pool_stats();
1.198     paf       446:        log_pool_stats(request_pool);
1.122     parser    447: #endif
                    448: }
                    449: 
1.216.2.13  paf       450: #if _MSC_VER && !defined(_DEBUG)
1.216.2.11  paf       451: #      define PA_USE___TRY
                    452: struct Parser_executed {
                    453:        bool with_system_exception;
                    454:        LPEXCEPTION_POINTERS system_exception;
                    455: };
1.216.2.13  paf       456: #endif
1.216.2.11  paf       457: 
                    458: static 
                    459: #ifdef PA_USE___TRY
                    460: Parser_executed 
                    461: #else
                    462: void
                    463: #endif
                    464: call_real_parser_handler__do_SEH_return_it(
1.216.2.1  paf       465:                                                                 const char* filespec_to_process,
                    466:                                                                 const char* request_method, bool header_only) {
1.216.2.11  paf       467: #ifdef PA_USE___TRY
                    468:        Parser_executed result={false};
1.122     parser    469:        __try {
                    470: #endif
                    471:                real_parser_handler(
                    472:                        filespec_to_process,
                    473:                        request_method, header_only);
                    474:                
1.216.2.11  paf       475: #ifdef PA_USE___TRY
1.122     parser    476:        } __except (
1.216.2.11  paf       477:                (result.system_exception=GetExceptionInformation()), 
1.122     parser    478:                EXCEPTION_EXECUTE_HANDLER) {
1.216.2.12  paf       479:                //_asm int 3;
1.216.2.11  paf       480:                result.with_system_exception=true;
                    481:        }
                    482:        return result;
                    483: #endif
                    484: }
                    485: 
                    486: static void call_real_parser_handler__do_SEH(
                    487:                                                                 const char* filespec_to_process,
                    488:                                                                 const char* request_method, bool header_only) {
                    489: #ifdef PA_USE___TRY
                    490:        Parser_executed executed=
                    491: #endif
                    492:                call_real_parser_handler__do_SEH_return_it(filespec_to_process, 
                    493:                        request_method, header_only);
                    494: 
                    495: #ifdef PA_USE___TRY
                    496:        if(executed.with_system_exception)
                    497:                if(executed.system_exception)
                    498:                        if(_EXCEPTION_RECORD *er=executed.system_exception->ExceptionRecord)
1.216.2.30.2.  (paf      499:):                              throw Exception(0,
                    500:):                                      0,
1.165     paf       501:                                        "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
1.122     parser    502:                        else
1.216.2.30.2.  (paf      503:):                              throw Exception(0, 
                    504:):                                      0, 
1.216.2.11  paf       505:                                        "Exception <no exception record>");
                    506:                else
1.216.2.30.2.  (paf      507:):                      throw Exception(0, 
                    508:):                              0, 
1.216.2.11  paf       509:                                "Exception <no exception information>");
1.122     parser    510: #endif
                    511: }
                    512: 
1.216.2.1  paf       513: static void usage(const char* program) {
1.188     paf       514:        printf(
1.216.2.1  paf       515:                "Parser/%s Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)\n"
1.184     paf       516:                "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
                    517:                "\n"
                    518:                "Usage: %s [options] file\n"
                    519:                "Options are:\n"
1.185     paf       520: #ifdef WITH_MAILRECEIVE
1.193     paf       521:                "    -m              Parse mail, put received letter to $mail:received\n"
1.184     paf       522: #endif
1.193     paf       523:                "    -f config_file  Use this config file (/path/to/auto.p)\n"
                    524:                "    -h              Display usage information (this message)\n"
1.184     paf       525:                , PARSER_VERSION, 
                    526:                program);
                    527:        exit(EINVAL);
                    528: }
                    529: 
1.195     paf       530: int main(int argc, char *argv[]) {
1.216.2.30.2.  (paf      531:): /*   Array<int> test;
                    532:):      test+=3;
                    533:): //   test+=4;
                    534:): //   int a=test.count();
                    535:): //   int b=test.get(0);
                    536:):      int b=test.get(10);
                    537:):      printf("%d", b);*/
1.203     paf       538: #ifdef SIGUSR1
1.205     paf       539:     if(signal(SIGUSR1, SIGUSR1_handler)==SIG_ERR)
1.203     paf       540:                SAPI::die("Can not set handler for SIGUSR1");
                    541: #endif
                    542: #ifdef SIGPIPE
1.205     paf       543:     if(signal(SIGPIPE, SIGPIPE_handler)==SIG_ERR)
1.203     paf       544:                SAPI::die("Can not set handler for SIGPIPE");
                    545: #endif
                    546: 
                    547: 
1.185     paf       548: #ifdef DEBUG_MAILRECEIVE
                    549:        if(FILE *fake_in=fopen(DEBUG_MAILRECEIVE, "rt")) {
1.184     paf       550:                dup2(fake_in->_file, 0/*STDIN_FILENO*/);
                    551:        }
                    552: #endif
                    553: 
1.178     paf       554: #ifdef _DEBUG
1.216.2.9  paf       555:        //_crtBreakAlloc=4042;
1.178     paf       556: #endif
1.216.2.10  paf       557:        //_asm int 3;
1.193     paf       558:        argv0=argv[0];
1.45      paf       559: 
1.32      paf       560:        umask(2);
                    561: 
1.3       paf       562:        // were we started as CGI?
1.146     paf       563:        cgi=
1.109     parser    564:                getenv("SERVER_SOFTWARE") || 
                    565:                getenv("SERVER_NAME") || 
                    566:                getenv("GATEWAY_INTERFACE") || 
1.216.2.15  paf       567:                getenv("REQUEST_METHOD");
1.5       paf       568:        
1.184     paf       569:        char *raw_filespec_to_process;
1.210     paf       570:        if(cgi) {
1.184     paf       571:                raw_filespec_to_process=getenv("PATH_TRANSLATED");
1.210     paf       572:                if(raw_filespec_to_process && !*raw_filespec_to_process)
                    573:                        raw_filespec_to_process=0;
                    574:        } else {
1.184     paf       575:                optind = 1;
                    576:                opterr = 0;
                    577:                int c;
1.216.2.19  paf       578:                while((c = getopt(argc, argv, "hf:"
1.185     paf       579: #ifdef WITH_MAILRECEIVE
1.184     paf       580:                        "m"
                    581: #endif
                    582:                        )) > 0) {
                    583:                        switch (c) {
                    584:                        case 'h':
                    585:                                usage(argv[0]);
1.216.2.15  paf       586:                                break;
1.193     paf       587:                        case 'f':
                    588:                                config_filespec_cstr=optarg;
1.184     paf       589:                                break;
1.185     paf       590: #ifdef WITH_MAILRECEIVE
1.184     paf       591:                        case 'm':
                    592:                                mail_received=true;
                    593:                                break;
                    594: #endif
                    595:                        default:
                    596:                                fprintf(stderr, "%s: invalid option '%c'\n", argv[0], optopt);
                    597:                                usage(argv[0]);
                    598:                                break;
                    599:                        }
                    600:                }
                    601:                if (optind != argc - 1) {
                    602:                        fprintf(stderr, "%s: file not specified\n", argv[0]);
                    603:                        usage(argv[0]);
1.10      paf       604:                }
1.184     paf       605:                
                    606:                raw_filespec_to_process=argv[optind++];
1.10      paf       607:        }
                    608: 
1.100     parser    609: #ifdef WIN32
                    610:        setmode(fileno(stdin), _O_BINARY);
                    611:        setmode(fileno(stdout), _O_BINARY);
                    612:        setmode(fileno(stderr), _O_BINARY);
                    613: #endif
                    614: 
1.216.2.19  paf       615: #if defined(_MSC_VER) && defined(_DEBUG)
1.148     paf       616:        // Get current flag
                    617:        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
                    618: 
                    619:        // Turn on leak-checking bit
1.216.2.19  paf       620:        tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
1.148     paf       621: 
                    622:        // Set flag to the new value
                    623:        _CrtSetDbgFlag( tmpFlag );
1.216.2.30.2.  (paf      624:):      //_CrtSetBreakAlloc(62143); //147302
1.216.2.30  paf       625: 
                    626:        _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
                    627:        _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR );
1.138     paf       628: #endif
                    629: 
1.166     paf       630:        char filespec_to_process[MAX_STRING];
                    631:        full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10      paf       632: 
1.216.2.1  paf       633:        const char* request_method=getenv("REQUEST_METHOD");
1.35      paf       634:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131     paf       635: 
1.122     parser    636:        try { // global try
                    637:                call_real_parser_handler__do_SEH(
                    638:                        filespec_to_process,
                    639:                        request_method, header_only);
                    640:        } catch(const Exception& e) { // global problem 
1.44      paf       641:                // don't allocate anything on pool here:
                    642:                //   possible pool' exception not catch-ed now
                    643:                //   and there could be out-of-memory exception
1.43      paf       644: 
1.144     paf       645:                SAPI::die("exception in request exception handler: %s", e.comment());
1.16      paf       646:        }
1.109     parser    647: 
1.216.2.11  paf       648: //     _asm int 3;
1.134     paf       649:        return 0;
1.1       paf       650: }

E-mail: