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

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

E-mail: