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

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.155     paf         4:        Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.154     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.27      paf         6: 
1.159   ! paf         7:        $Id: parser3.C,v 1.158 2002/02/18 08:45:45 paf Exp $
1.1       paf         8: */
                      9: 
1.40      paf        10: #include "pa_config_includes.h"
1.3       paf        11: 
1.139     paf        12: #if _MSC_VER
1.131     paf        13: #      include <new.h>
1.148     paf        14: #      include <crtdbg.h>
1.151     paf        15: #else
                     16: #      include "pa_config_paths.h"
1.3       paf        17: #endif
1.27      paf        18: 
1.37      paf        19: #include "pa_sapi.h"
1.76      paf        20: #include "classes.h"
1.24      paf        21: #include "pa_common.h"
1.2       paf        22: #include "pa_request.h"
1.57      paf        23: #include "pa_socks.h"
1.68      paf        24: #include "pa_version.h"
1.125     parser     25: #include "pool_storage.h"
1.69      paf        26: 
1.149     paf        27: #ifdef WIN32
                     28: #      include <windows.h>
1.120     parser     29: #endif
                     30: 
1.158     paf        31: //#define DEBUG_POOL_MALLOC
1.84      parser     32: 
1.109     parser     33: // consts
1.113     parser     34: 
                     35: extern const char *main_RCSIds[];
1.116     parser     36: #ifdef USE_SMTP
1.113     parser     37: extern const char *smtp_RCSIds[];
1.114     parser     38: #endif
1.113     parser     39: extern const char *gd_RCSIds[];
                     40: extern const char *classes_RCSIds[];
                     41: extern const char *types_RCSIds[];
1.115     parser     42: extern const char *parser3_RCSIds[];
1.113     parser     43: const char **RCSIds[]={
                     44:        main_RCSIds,
1.116     parser     45: #ifdef USE_SMTP
1.113     parser     46:        smtp_RCSIds,
1.114     parser     47: #endif
1.113     parser     48:        gd_RCSIds,
                     49:        classes_RCSIds,
                     50:        types_RCSIds,
1.115     parser     51:        parser3_RCSIds,
1.113     parser     52:        0
                     53: };
1.84      parser     54: 
1.159   ! paf        55: const char *PARSER_ROOT_CONFIG_ENV_NAME="PARSER_ROOT_CONFIG";
        !            56: 
1.42      paf        57: /// IIS refuses to read bigger chunks
                     58: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M 
                     59: 
1.45      paf        60: const char *argv0;
1.125     parser     61: Pool_storage pool_storage;
                     62: Pool pool(&pool_storage); // global pool [dont describe to doxygen: it confuses it with param names]
1.27      paf        63: bool cgi; ///< we were started as CGI?
1.5       paf        64: 
1.46      paf        65: // SAPI
1.86      parser     66: 
1.124     parser     67: static void log(const char *fmt, va_list args) {
1.61      paf        68:        bool opened;
                     69:        FILE *f=0;
                     70: 
                     71:        if(argv0) {
                     72:                // beside by binary
                     73:                char file_spec[MAX_STRING];
1.98      parser     74:                strncpy(file_spec, argv0, MAX_STRING-1);  file_spec[MAX_STRING-1]=0; // filespec of my binary
1.61      paf        75:                rsplit(file_spec, '/');  rsplit(file_spec, '\\');// strip filename
                     76:                strcat(file_spec, "/parser3.log");
                     77:                f=fopen(file_spec, "at");
                     78:        }
                     79:        opened=f!=0;
                     80:        if(!opened)
                     81:                f=stderr;
                     82: 
                     83:        // prefix
                     84:        time_t t=time(0);
                     85:        const char *stamp=ctime(&t);
                     86:        fprintf(f, "[%.*s] ", strlen(stamp)-1, stamp);
                     87:        // message
1.117     parser     88: 
                     89:        char buf[MAX_STRING];
                     90:        size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
                     91:        remove_crlf(buf, buf+size);
                     92: 
                     93:        fwrite(buf, size, 1, f);
1.61      paf        94:        // newline
                     95:        fprintf(f, "\n");
                     96: 
                     97:        if(opened)
                     98:                fclose(f);
1.85      parser     99:        else
                    100:                fflush(f);
1.124     parser    101: }
                    102: 
                    103: // appends to parser3.log located beside my binary if openable, to stderr otherwize
                    104: void SAPI::log(Pool& , const char *fmt, ...) {
                    105:     va_list args;
                    106:        va_start(args,fmt);
                    107:        ::log(fmt, args);
                    108:        va_end(args);
                    109: }
                    110: 
                    111: void SAPI::die(const char *fmt, ...) {
1.137     paf       112: #ifdef DEBUG_POOL_MALLOC
                    113:        extern void log_pool_stats(Pool& pool);
                    114:        log_pool_stats(pool);
                    115: #endif
                    116: 
1.144     paf       117:     va_list args;
                    118:        va_start(args,fmt);
1.138     paf       119:        // log
                    120: 
                    121:        // logging is more important than user 
                    122:        // she can cancel download, we'd get SIG_PIPE, 
                    123:        // nothing would be logged then
1.124     parser    124:        ::log(fmt, args);
                    125: 
1.138     paf       126:        // inform user
                    127: 
1.134     paf       128:        char body[MAX_STRING];
1.138     paf       129:        int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134     paf       130: 
1.144     paf       131:        va_end(args);
                    132: 
1.134     paf       133:        // prepare header
                    134:        // let's be honest, that's bad we couldn't produce valid output
                    135:        SAPI::add_header_attribute(pool, "status", "500");
                    136:        SAPI::add_header_attribute(pool, "content-type", "text/plain");
                    137:        char content_length_cstr[MAX_NUMBER];
                    138:        snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
                    139:        SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
                    140: 
                    141:        // send header
                    142:        SAPI::send_header(pool);
                    143: 
                    144:        // body
                    145:        SAPI::send_body(pool, body, content_length);
                    146: 
1.124     parser    147:        exit(1);
1.61      paf       148: }
                    149: 
1.122     parser    150: const char *SAPI::get_env(Pool& , const char *name) {
1.109     parser    151:        return getenv(name);
1.28      paf       152: }
                    153: 
1.122     parser    154: size_t SAPI::read_post(Pool& , char *buf, size_t max_bytes) {
1.59      paf       155:        size_t read_size=0;
1.12      paf       156:        do {
1.36      paf       157:                int chunk_size=read(fileno(stdin), 
1.42      paf       158:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129     paf       159:                if(chunk_size<=0)
1.12      paf       160:                        break;
                    161:                read_size+=chunk_size;
                    162:        } while(read_size<max_bytes);
                    163: 
                    164:        return read_size;
1.10      paf       165: }
                    166: 
1.122     parser    167: void SAPI::add_header_attribute(Pool& , const char *key, const char *value) {
1.68      paf       168:        if(cgi)
1.20      paf       169:                printf("%s: %s\n", key, value);
1.19      paf       170: }
                    171: 
1.56      paf       172: /// @todo intelligent cache-control
1.122     parser    173: void SAPI::send_header(Pool& ) {
1.33      paf       174:        if(cgi) {
1.147     paf       175: //             puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       176: 
                    177:                // header | body  delimiter
1.20      paf       178:                puts("");
1.33      paf       179:        }
1.30      paf       180: }
1.20      paf       181: 
1.122     parser    182: void SAPI::send_body(Pool& , const void *buf, size_t size) {
1.19      paf       183:        stdout_write(buf, size);
1.58      paf       184: }
                    185: 
1.97      parser    186: //
                    187: 
                    188: char *full_file_spec(char *file_name) {
1.108     parser    189:        if(file_name && !strchr(file_name, '/')) {
1.97      parser    190:                static char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    191:                static char buf[MAX_STRING];
                    192:                snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
                    193:                return buf;
                    194:        }
                    195:        return file_name;
                    196: }
                    197: 
1.40      paf       198: /**
1.122     parser    199: main workhorse
1.19      paf       200: 
1.122     parser    201:   @todo 
1.40      paf       202:                IIS: remove trailing default-document[index.html] from $request.uri.
                    203:                to do that we need to consult metabase,
                    204:                wich is tested but seems slow.
1.144     paf       205:                IIS5 todo find out proper 'illegal call' check 
1.40      paf       206: */
1.122     parser    207: void real_parser_handler(
                    208:                                        const char *filespec_to_process,
                    209:                                        const char *request_method, bool header_only) {
                    210:        // init socks
                    211:        init_socks(pool);
                    212:        
                    213:        // init global classes
                    214:        init_methoded_array(pool);
                    215:        // init global variables
                    216:        pa_globals_init(pool);
                    217:        
                    218:        if(!filespec_to_process)
1.144     paf       219:                SAPI::die("Parser/%s", PARSER_VERSION);
1.122     parser    220:        
                    221:        // Request info
                    222:        Request::Info request_info;
                    223:        if(cgi) {
                    224:                if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
                    225:                        request_info.document_root=env_document_root;
                    226:                else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
                    227:                        // IIS
                    228:                        size_t len=strlen(filespec_to_process)-strlen(path_info);
                    229:                        char *buf=(char *)pool.malloc(len+1);
                    230:                        memcpy(buf, filespec_to_process, len); buf[len]=0;
                    231:                        request_info.document_root=buf;
                    232:                } else
                    233:                        throw Exception(0, 0,
                    234:                        0,
                    235:                        "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
                    236:        } else {
                    237:                static char buf[MAX_STRING];
                    238:                strncpy(buf, filespec_to_process, MAX_STRING-1); buf[MAX_STRING-1]=0;
                    239:                if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
                    240:                        request_info.document_root=buf;
                    241:                else
                    242:                        request_info.document_root="";
                    243:        }
                    244:        request_info.path_translated=filespec_to_process;
                    245:        request_info.method=request_method ? request_method : "GET";
                    246:        const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
                    247:        request_info.query_string=query_string;
                    248:        if(cgi) {
                    249:                if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
                    250:                        request_info.uri=env_request_uri;
                    251:                else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
                    252:                        if(query_string) {
                    253:                                char *reconstructed_uri=(char *)pool.malloc(
                    254:                                        strlen(path_info)+1/*'?'*/+
                    255:                                        strlen(query_string)+1/*0*/);
                    256:                                strcpy(reconstructed_uri, path_info);
                    257:                                strcat(reconstructed_uri, "?");
                    258:                                strcat(reconstructed_uri, query_string);
                    259:                                request_info.uri=reconstructed_uri;
                    260:                        } else
                    261:                                request_info.uri=path_info;
                    262:                        else
                    263:                                throw Exception(0, 0,
                    264:                                0,
                    265:                                "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.145     paf       266:                        
                    267: #ifndef WIN32
                    268:                        // they've changed this under IIS5.
1.122     parser    269:                        if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
                    270:                                size_t script_name_len=strlen(script_name);
                    271:                                size_t uri_len=strlen(request_info.uri);
                    272:                                if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
                    273:                                        script_name_len != uri_len) // under IIS they are the same
1.144     paf       274:                                        SAPI::die("CGI: illegal call");
1.122     parser    275:                        }
1.145     paf       276: #endif
1.122     parser    277:        } else
                    278:                request_info.uri=0;
                    279:        
                    280:        request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
                    281:        const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
                    282:        request_info.content_length=(content_length?atoi(content_length):0);
                    283:        request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                    284:        request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
                    285:        
                    286:        // prepare to process request
                    287:        Request request(pool,
                    288:                request_info,
1.143     paf       289: #ifdef _DEBUG
                    290:                String::UL_HTML|String::UL_OPTIMIZE_BIT
                    291: #else
                    292:                cgi ? String::UL_HTML|String::UL_OPTIMIZE_BIT : String::UL_AS_IS
                    293: #endif
                    294:                ,
1.130     paf       295:                true /* status_allowed */);
1.122     parser    296:        
                    297:        // some root-controlled location
1.159   ! paf       298:        const char *root_config_filespec;
        !           299:        if(const char *root_config_by_env=getenv(PARSER_ROOT_CONFIG_ENV_NAME))
        !           300:                root_config_filespec=root_config_by_env;
        !           301:        else {
1.152     paf       302: #ifdef ROOT_CONFIG_DIR
1.159   ! paf       303:                root_config_filespec=ROOT_CONFIG_DIR "/" CONFIG_FILE_NAME;
1.122     parser    304: #else
                    305: #      ifdef WIN32
1.159   ! paf       306:                // c:\windows
        !           307:                char windows_dir[MAX_STRING];
        !           308:                GetWindowsDirectory(windows_dir, MAX_STRING);
        !           309: 
        !           310:                char buf[MAX_STRING];
        !           311:                snprintf(buf, MAX_STRING, 
        !           312:                        "%s/%s", 
        !           313:                        windows_dir, CONFIG_FILE_NAME);
        !           314:                
        !           315:                root_config_filespec=buf;
1.122     parser    316: #      else
                    317: #error must be compiled either configure/make or MSVC++
                    318: #      endif
                    319: #endif
1.159   ! paf       320:        }
1.122     parser    321:        
                    322:        // beside by binary
                    323:        // @todo full path, not ./!
                    324:        static char site_config_path[MAX_STRING];
                    325:        strncpy(site_config_path, argv0, MAX_STRING-1);  site_config_path[MAX_STRING-1]=0; // filespec of my binary
                    326:        if(!(
                    327:                rsplit(site_config_path, '/') || 
                    328:                rsplit(site_config_path, '\\'))) { // strip filename
                    329:                // no path, just filename
                    330:                site_config_path[0]='.'; site_config_path[1]=0;
                    331:        }
                    332:        
                    333:        char site_config_filespec[MAX_STRING];
                    334:        snprintf(site_config_filespec, MAX_STRING, 
                    335:                "%s/%s", 
                    336:                site_config_path, CONFIG_FILE_NAME);
                    337:        
                    338:        // process the request
                    339:        request.core(
                    340:                root_config_filespec, false,
                    341:                site_config_filespec, false,
                    342:                header_only);
                    343:        
                    344:        //
                    345:        done_socks();
                    346:        
                    347: #ifdef DEBUG_POOL_MALLOC
                    348:        extern void log_pool_stats(Pool& pool);
                    349:        log_pool_stats(pool);
                    350: #endif
                    351: }
                    352: 
                    353: void call_real_parser_handler__do_SEH(
                    354:                                                                 const char *filespec_to_process,
                    355:                                                                 const char *request_method, bool header_only) {
1.133     paf       356: #if _MSC_VER && !defined(_DEBUG)
1.122     parser    357:        LPEXCEPTION_POINTERS system_exception=0;
                    358:        __try {
                    359: #endif
                    360:                real_parser_handler(
                    361:                        filespec_to_process,
                    362:                        request_method, header_only);
                    363:                
1.133     paf       364: #if _MSC_VER && !defined(_DEBUG)
1.122     parser    365:        } __except (
                    366:                (system_exception=GetExceptionInformation()), 
                    367:                EXCEPTION_EXECUTE_HANDLER) {
                    368:                
                    369:                if(system_exception)
                    370:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
                    371:                                throw Exception(0, 0,
                    372:                                0,
                    373:                                "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
                    374:                        else
                    375:                                throw Exception(0, 0, 0, "Exception <no exception record>");
                    376:                        else
                    377:                                throw Exception(0, 0, 0, "Exception <no exception information>");
                    378:        }
                    379: #endif
                    380: }
                    381: 
1.139     paf       382: #if _MSC_VER
1.135     paf       383: int failed_new(size_t size) {
                    384:        SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
                    385:        return 0; // not reached
1.131     paf       386: }
1.135     paf       387: #endif
1.131     paf       388: 
1.135     paf       389: #ifdef HAVE_SET_NEW_HANDLER
1.134     paf       390: void failed_new() {
1.135     paf       391:     SAPI::die("out of memory in 'new'");
1.131     paf       392: }
                    393: #endif
                    394: 
1.5       paf       395: int main(int argc, char *argv[]) {
1.144     paf       396: //     _asm int 3;
1.45      paf       397:        argv0=argv[0];
                    398: 
1.32      paf       399:        umask(2);
                    400: 
1.3       paf       401:        // were we started as CGI?
1.146     paf       402:        cgi=
1.109     parser    403:                getenv("SERVER_SOFTWARE") || 
                    404:                getenv("SERVER_NAME") || 
                    405:                getenv("GATEWAY_INTERFACE") || 
                    406:                getenv("REQUEST_METHOD");
1.5       paf       407:        
1.10      paf       408:        if(!cgi) {
                    409:                if(argc<2) {
1.69      paf       410:                        printf(
1.155     paf       411:                                "Parser/%s Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)\n"
1.154     paf       412:                                "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
1.100     parser    413:                                "\n"
                    414:                                "Usage: %s <file>\n",
1.69      paf       415:                                PARSER_VERSION, 
                    416:                                argv0?argv0:"parser3");
1.67      paf       417:                        return 1;
1.10      paf       418:                }
                    419:        }
                    420: 
1.100     parser    421: #ifdef WIN32
                    422:        setmode(fileno(stdin), _O_BINARY);
                    423:        setmode(fileno(stdout), _O_BINARY);
                    424:        setmode(fileno(stderr), _O_BINARY);
                    425: #endif
                    426: 
1.139     paf       427: #if _MSC_VER
1.138     paf       428:        _set_new_handler(failed_new);
1.148     paf       429: 
                    430: #ifdef _DEBUG
                    431:        // Get current flag
                    432:        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
                    433: 
                    434:        // Turn on leak-checking bit
                    435:        tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
                    436: 
                    437:        // Set flag to the new value
                    438:        _CrtSetDbgFlag( tmpFlag );
                    439: //     _CrtSetBreakAlloc(471);
                    440: 
                    441: #endif
                    442: 
1.138     paf       443: #endif
                    444: 
                    445: #ifdef HAVE_SET_NEW_HANDLER
                    446:        std::set_new_handler(failed_new);
                    447: #endif
                    448: 
1.109     parser    449:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36      paf       450: #ifdef WIN32
1.43      paf       451:        back_slashes_to_slashes(filespec_to_process);
1.36      paf       452: #endif
1.97      parser    453:        filespec_to_process=full_file_spec(filespec_to_process);
1.10      paf       454: 
1.109     parser    455:        const char *request_method=getenv("REQUEST_METHOD");
1.35      paf       456:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131     paf       457: 
1.122     parser    458:        try { // global try
                    459:                call_real_parser_handler__do_SEH(
                    460:                        filespec_to_process,
                    461:                        request_method, header_only);
                    462:        } catch(const Exception& e) { // global problem 
1.44      paf       463:                // don't allocate anything on pool here:
                    464:                //   possible pool' exception not catch-ed now
                    465:                //   and there could be out-of-memory exception
1.43      paf       466: 
1.144     paf       467:                SAPI::die("exception in request exception handler: %s", e.comment());
1.134     paf       468: #ifndef _DEBUG
1.131     paf       469:        } catch(...) { 
1.134     paf       470:                SAPI::die("<unknown exception>");
1.133     paf       471: #endif
1.16      paf       472:        }
1.122     parser    473:        
1.109     parser    474: 
                    475: #ifndef WIN32
                    476:        // 
                    477:        if(!cgi)
                    478:                SAPI::send_body(pool, "\n", 1);
                    479: #endif
1.156     paf       480: //_asm int 3;
1.134     paf       481:        return 0;
1.1       paf       482: }

E-mail: