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

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

E-mail: