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

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

E-mail: