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

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.140   ! paf         7:        $Id: parser3.C,v 1.139 2001/11/19 08:21:39 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.138     paf       122:        // log
                    123: 
                    124:        // logging is more important than user 
                    125:        // she can cancel download, we'd get SIG_PIPE, 
                    126:        // nothing would be logged then
1.124     parser    127:     va_list args;
                    128:        va_start(args,fmt);
                    129:        ::log(fmt, args);
                    130:        va_end(args);
                    131: 
1.138     paf       132:        // inform user
                    133: 
1.134     paf       134:        char body[MAX_STRING];
1.138     paf       135:        int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134     paf       136: 
                    137:        // prepare header
                    138:        // let's be honest, that's bad we couldn't produce valid output
                    139:        SAPI::add_header_attribute(pool, "status", "500");
                    140:        SAPI::add_header_attribute(pool, "content-type", "text/plain");
                    141:        char content_length_cstr[MAX_NUMBER];
                    142:        snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
                    143:        SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
                    144: 
                    145:        // send header
                    146:        SAPI::send_header(pool);
                    147: 
                    148:        // body
                    149:        SAPI::send_body(pool, body, content_length);
                    150: 
1.124     parser    151:        exit(1);
1.61      paf       152: }
                    153: 
1.122     parser    154: const char *SAPI::get_env(Pool& , const char *name) {
1.109     parser    155:        return getenv(name);
1.28      paf       156: }
                    157: 
1.122     parser    158: size_t SAPI::read_post(Pool& , char *buf, size_t max_bytes) {
1.59      paf       159:        size_t read_size=0;
1.12      paf       160:        do {
1.36      paf       161:                int chunk_size=read(fileno(stdin), 
1.42      paf       162:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129     paf       163:                if(chunk_size<=0)
1.12      paf       164:                        break;
                    165:                read_size+=chunk_size;
                    166:        } while(read_size<max_bytes);
                    167: 
                    168:        return read_size;
1.10      paf       169: }
                    170: 
1.122     parser    171: void SAPI::add_header_attribute(Pool& , const char *key, const char *value) {
1.68      paf       172:        if(cgi)
1.20      paf       173:                printf("%s: %s\n", key, value);
1.19      paf       174: }
                    175: 
1.56      paf       176: /// @todo intelligent cache-control
1.122     parser    177: void SAPI::send_header(Pool& ) {
1.33      paf       178:        if(cgi) {
1.55      paf       179:                puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       180: 
                    181:                // header | body  delimiter
1.20      paf       182:                puts("");
1.33      paf       183:        }
1.30      paf       184: }
1.20      paf       185: 
1.122     parser    186: void SAPI::send_body(Pool& , const void *buf, size_t size) {
1.19      paf       187:        stdout_write(buf, size);
1.58      paf       188: }
                    189: 
1.97      parser    190: //
                    191: 
                    192: char *full_file_spec(char *file_name) {
1.108     parser    193:        if(file_name && !strchr(file_name, '/')) {
1.97      parser    194:                static char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    195:                static char buf[MAX_STRING];
                    196:                snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
                    197:                return buf;
                    198:        }
                    199:        return file_name;
                    200: }
                    201: 
1.40      paf       202: /**
1.122     parser    203: main workhorse
1.19      paf       204: 
1.122     parser    205:   @todo 
1.40      paf       206:                IIS: remove trailing default-document[index.html] from $request.uri.
                    207:                to do that we need to consult metabase,
                    208:                wich is tested but seems slow.
                    209: */
1.122     parser    210: void real_parser_handler(
                    211:                                        const char *filespec_to_process,
                    212:                                        const char *request_method, bool header_only) {
                    213:        // init socks
                    214:        init_socks(pool);
                    215:        
                    216: #ifdef XML
                    217:        /**
                    218:        * Initialize Xerces and Xalan.
                    219:        *
                    220:        * Should be called only once per process before making
                    221:        * any other API calls.
                    222:        */
                    223:        //_asm int 3;
                    224:        XalanInitialize();
                    225: #endif
                    226:        
                    227:        // init global classes
                    228:        init_methoded_array(pool);
                    229:        // init global variables
                    230:        pa_globals_init(pool);
                    231:        
                    232:        if(!filespec_to_process)
                    233:                throw Exception(0, 0,
                    234:                0,
                    235:                "Parser/%s", PARSER_VERSION);
                    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)");
                    282:                        
                    283:                        if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
                    284:                                size_t script_name_len=strlen(script_name);
                    285:                                size_t uri_len=strlen(request_info.uri);
                    286:                                if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
                    287:                                        script_name_len != uri_len) // under IIS they are the same
                    288:                                        throw Exception(0, 0,
                    289:                                        0,
                    290:                                        "CGI: illegal call");
                    291:                        }
                    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.140   ! paf       304:                cgi ? String::UL_HTML|String::UL_OPTIMIZE_BIT : String::UL_AS_IS,
1.130     paf       305:                true /* status_allowed */);
1.122     parser    306:        
                    307:        // some root-controlled location
                    308: #ifdef SYSCONFDIR
                    309:        const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME;
                    310: #else
                    311: #      ifdef WIN32
                    312:        // c:\windows
                    313:        char root_config_path[MAX_STRING];
                    314:        GetWindowsDirectory(root_config_path, MAX_STRING);
                    315:        
                    316:        char root_config_filespec[MAX_STRING];
                    317:        snprintf(root_config_filespec, MAX_STRING, 
                    318:                "%s/%s", 
                    319:                root_config_path, CONFIG_FILE_NAME);
                    320: #      else
                    321: #error must be compiled either configure/make or MSVC++
                    322: #      endif
                    323: #endif
                    324:        
                    325:        // beside by binary
                    326:        // @todo full path, not ./!
                    327:        static char site_config_path[MAX_STRING];
                    328:        strncpy(site_config_path, argv0, MAX_STRING-1);  site_config_path[MAX_STRING-1]=0; // filespec of my binary
                    329:        if(!(
                    330:                rsplit(site_config_path, '/') || 
                    331:                rsplit(site_config_path, '\\'))) { // strip filename
                    332:                // no path, just filename
                    333:                site_config_path[0]='.'; site_config_path[1]=0;
                    334:        }
                    335:        
                    336:        char site_config_filespec[MAX_STRING];
                    337:        snprintf(site_config_filespec, MAX_STRING, 
                    338:                "%s/%s", 
                    339:                site_config_path, CONFIG_FILE_NAME);
                    340:        
                    341:        // process the request
                    342:        request.core(
                    343:                root_config_filespec, false,
                    344:                site_config_filespec, false,
                    345:                header_only);
                    346:        
                    347:        //
                    348:        done_socks();
                    349:        
                    350: #ifdef DEBUG_POOL_MALLOC
                    351:        extern void log_pool_stats(Pool& pool);
                    352:        log_pool_stats(pool);
                    353: #endif
                    354: }
                    355: 
                    356: void call_real_parser_handler__do_SEH(
                    357:                                                                 const char *filespec_to_process,
                    358:                                                                 const char *request_method, bool header_only) {
1.133     paf       359: #if _MSC_VER && !defined(_DEBUG)
1.122     parser    360:        LPEXCEPTION_POINTERS system_exception=0;
                    361:        __try {
                    362: #endif
                    363:                real_parser_handler(
                    364:                        filespec_to_process,
                    365:                        request_method, header_only);
                    366:                
1.133     paf       367: #if _MSC_VER && !defined(_DEBUG)
1.122     parser    368:        } __except (
                    369:                (system_exception=GetExceptionInformation()), 
                    370:                EXCEPTION_EXECUTE_HANDLER) {
                    371:                
                    372:                if(system_exception)
                    373:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
                    374:                                throw Exception(0, 0,
                    375:                                0,
                    376:                                "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
                    377:                        else
                    378:                                throw Exception(0, 0, 0, "Exception <no exception record>");
                    379:                        else
                    380:                                throw Exception(0, 0, 0, "Exception <no exception information>");
                    381:        }
                    382: #endif
                    383: }
                    384: 
1.139     paf       385: #if _MSC_VER
1.135     paf       386: int failed_new(size_t size) {
                    387:        SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
                    388:        return 0; // not reached
1.131     paf       389: }
1.135     paf       390: #endif
1.131     paf       391: 
1.135     paf       392: #ifdef HAVE_SET_NEW_HANDLER
1.134     paf       393: void failed_new() {
1.135     paf       394:     SAPI::die("out of memory in 'new'");
1.131     paf       395: }
                    396: #endif
                    397: 
1.5       paf       398: int main(int argc, char *argv[]) {
1.45      paf       399:        argv0=argv[0];
                    400: 
1.32      paf       401:        umask(2);
                    402: 
1.3       paf       403:        // were we started as CGI?
1.20      paf       404:        cgi=
1.109     parser    405:                getenv("SERVER_SOFTWARE") || 
                    406:                getenv("SERVER_NAME") || 
                    407:                getenv("GATEWAY_INTERFACE") || 
                    408:                getenv("REQUEST_METHOD");
1.5       paf       409:        
1.10      paf       410:        if(!cgi) {
                    411:                if(argc<2) {
1.69      paf       412:                        printf(
1.100     parser    413:                                "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n"
1.128     paf       414:                                "Author: Alexander Petrosyan <paf@design.ru>(http://paf.design.ru)\n"
1.100     parser    415:                                "\n"
                    416:                                "Usage: %s <file>\n",
1.69      paf       417:                                PARSER_VERSION, 
                    418:                                argv0?argv0:"parser3");
1.67      paf       419:                        return 1;
1.10      paf       420:                }
                    421:        }
                    422: 
1.100     parser    423: #ifdef WIN32
                    424:        setmode(fileno(stdin), _O_BINARY);
                    425:        setmode(fileno(stdout), _O_BINARY);
                    426:        setmode(fileno(stderr), _O_BINARY);
                    427: #endif
                    428: 
1.139     paf       429: #if _MSC_VER
1.138     paf       430:        _set_new_handler(failed_new);
                    431: #endif
                    432: 
                    433: #ifdef HAVE_SET_NEW_HANDLER
                    434:        std::set_new_handler(failed_new);
                    435: #endif
                    436: 
1.109     parser    437:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36      paf       438: #ifdef WIN32
1.43      paf       439:        back_slashes_to_slashes(filespec_to_process);
1.36      paf       440: #endif
1.97      parser    441:        filespec_to_process=full_file_spec(filespec_to_process);
1.10      paf       442: 
1.109     parser    443:        const char *request_method=getenv("REQUEST_METHOD");
1.35      paf       444:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131     paf       445: 
1.122     parser    446:        try { // global try
                    447:                call_real_parser_handler__do_SEH(
                    448:                        filespec_to_process,
                    449:                        request_method, header_only);
                    450:        } catch(const Exception& e) { // global problem 
1.44      paf       451:                // don't allocate anything on pool here:
                    452:                //   possible pool' exception not catch-ed now
                    453:                //   and there could be out-of-memory exception
1.43      paf       454: 
1.134     paf       455:                SAPI::die("exception in request exception handler: ", e.comment());
                    456: #ifndef _DEBUG
1.131     paf       457:        } catch(...) { 
1.134     paf       458:                SAPI::die("<unknown exception>");
1.133     paf       459: #endif
1.16      paf       460:        }
1.122     parser    461:        
1.109     parser    462: 
                    463: #ifndef WIN32
                    464:        // 
                    465:        if(!cgi)
                    466:                SAPI::send_body(pool, "\n", 1);
                    467: #endif
1.134     paf       468:        return 0;
1.1       paf       469: }

E-mail: