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

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.149   ! paf         7:        $Id: parser3.C,v 1.148 2001/12/13 13:02:43 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.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.149   ! paf        25: #ifdef WIN32
        !            26: #      include <windows.h>
1.120     parser     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.144     paf       119:     va_list args;
                    120:        va_start(args,fmt);
1.138     paf       121:        // log
                    122: 
                    123:        // logging is more important than user 
                    124:        // she can cancel download, we'd get SIG_PIPE, 
                    125:        // nothing would be logged then
1.124     parser    126:        ::log(fmt, args);
                    127: 
1.138     paf       128:        // inform user
                    129: 
1.134     paf       130:        char body[MAX_STRING];
1.138     paf       131:        int content_length=vsnprintf(body, MAX_STRING, fmt, args);
1.134     paf       132: 
1.144     paf       133:        va_end(args);
                    134: 
1.134     paf       135:        // prepare header
                    136:        // let's be honest, that's bad we couldn't produce valid output
                    137:        SAPI::add_header_attribute(pool, "status", "500");
                    138:        SAPI::add_header_attribute(pool, "content-type", "text/plain");
                    139:        char content_length_cstr[MAX_NUMBER];
                    140:        snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
                    141:        SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
                    142: 
                    143:        // send header
                    144:        SAPI::send_header(pool);
                    145: 
                    146:        // body
                    147:        SAPI::send_body(pool, body, content_length);
                    148: 
1.124     parser    149:        exit(1);
1.61      paf       150: }
                    151: 
1.122     parser    152: const char *SAPI::get_env(Pool& , const char *name) {
1.109     parser    153:        return getenv(name);
1.28      paf       154: }
                    155: 
1.122     parser    156: size_t SAPI::read_post(Pool& , char *buf, size_t max_bytes) {
1.59      paf       157:        size_t read_size=0;
1.12      paf       158:        do {
1.36      paf       159:                int chunk_size=read(fileno(stdin), 
1.42      paf       160:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129     paf       161:                if(chunk_size<=0)
1.12      paf       162:                        break;
                    163:                read_size+=chunk_size;
                    164:        } while(read_size<max_bytes);
                    165: 
                    166:        return read_size;
1.10      paf       167: }
                    168: 
1.122     parser    169: void SAPI::add_header_attribute(Pool& , const char *key, const char *value) {
1.68      paf       170:        if(cgi)
1.20      paf       171:                printf("%s: %s\n", key, value);
1.19      paf       172: }
                    173: 
1.56      paf       174: /// @todo intelligent cache-control
1.122     parser    175: void SAPI::send_header(Pool& ) {
1.33      paf       176:        if(cgi) {
1.147     paf       177: //             puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       178: 
                    179:                // header | body  delimiter
1.20      paf       180:                puts("");
1.33      paf       181:        }
1.30      paf       182: }
1.20      paf       183: 
1.122     parser    184: void SAPI::send_body(Pool& , const void *buf, size_t size) {
1.19      paf       185:        stdout_write(buf, size);
1.58      paf       186: }
                    187: 
1.97      parser    188: //
                    189: 
                    190: char *full_file_spec(char *file_name) {
1.108     parser    191:        if(file_name && !strchr(file_name, '/')) {
1.97      parser    192:                static char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    193:                static char buf[MAX_STRING];
                    194:                snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
                    195:                return buf;
                    196:        }
                    197:        return file_name;
                    198: }
                    199: 
1.40      paf       200: /**
1.122     parser    201: main workhorse
1.19      paf       202: 
1.122     parser    203:   @todo 
1.40      paf       204:                IIS: remove trailing default-document[index.html] from $request.uri.
                    205:                to do that we need to consult metabase,
                    206:                wich is tested but seems slow.
1.144     paf       207:                IIS5 todo find out proper 'illegal call' check 
1.40      paf       208: */
1.122     parser    209: void real_parser_handler(
                    210:                                        const char *filespec_to_process,
                    211:                                        const char *request_method, bool header_only) {
                    212:        // init socks
                    213:        init_socks(pool);
                    214:        
                    215:        // init global classes
                    216:        init_methoded_array(pool);
                    217:        // init global variables
                    218:        pa_globals_init(pool);
                    219:        
                    220:        if(!filespec_to_process)
1.144     paf       221:                SAPI::die("Parser/%s", PARSER_VERSION);
1.122     parser    222:        
                    223:        // Request info
                    224:        Request::Info request_info;
                    225:        if(cgi) {
                    226:                if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
                    227:                        request_info.document_root=env_document_root;
                    228:                else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
                    229:                        // IIS
                    230:                        size_t len=strlen(filespec_to_process)-strlen(path_info);
                    231:                        char *buf=(char *)pool.malloc(len+1);
                    232:                        memcpy(buf, filespec_to_process, len); buf[len]=0;
                    233:                        request_info.document_root=buf;
                    234:                } else
                    235:                        throw Exception(0, 0,
                    236:                        0,
                    237:                        "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
                    238:        } else {
                    239:                static char buf[MAX_STRING];
                    240:                strncpy(buf, filespec_to_process, MAX_STRING-1); buf[MAX_STRING-1]=0;
                    241:                if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
                    242:                        request_info.document_root=buf;
                    243:                else
                    244:                        request_info.document_root="";
                    245:        }
                    246:        request_info.path_translated=filespec_to_process;
                    247:        request_info.method=request_method ? request_method : "GET";
                    248:        const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
                    249:        request_info.query_string=query_string;
                    250:        if(cgi) {
                    251:                if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
                    252:                        request_info.uri=env_request_uri;
                    253:                else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
                    254:                        if(query_string) {
                    255:                                char *reconstructed_uri=(char *)pool.malloc(
                    256:                                        strlen(path_info)+1/*'?'*/+
                    257:                                        strlen(query_string)+1/*0*/);
                    258:                                strcpy(reconstructed_uri, path_info);
                    259:                                strcat(reconstructed_uri, "?");
                    260:                                strcat(reconstructed_uri, query_string);
                    261:                                request_info.uri=reconstructed_uri;
                    262:                        } else
                    263:                                request_info.uri=path_info;
                    264:                        else
                    265:                                throw Exception(0, 0,
                    266:                                0,
                    267:                                "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.145     paf       268:                        
                    269: #ifndef WIN32
                    270:                        // they've changed this under IIS5.
1.122     parser    271:                        if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
                    272:                                size_t script_name_len=strlen(script_name);
                    273:                                size_t uri_len=strlen(request_info.uri);
                    274:                                if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
                    275:                                        script_name_len != uri_len) // under IIS they are the same
1.144     paf       276:                                        SAPI::die("CGI: illegal call");
1.122     parser    277:                        }
1.145     paf       278: #endif
1.122     parser    279:        } else
                    280:                request_info.uri=0;
                    281:        
                    282:        request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
                    283:        const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
                    284:        request_info.content_length=(content_length?atoi(content_length):0);
                    285:        request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                    286:        request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
                    287:        
                    288:        // prepare to process request
                    289:        Request request(pool,
                    290:                request_info,
1.143     paf       291: #ifdef _DEBUG
                    292:                String::UL_HTML|String::UL_OPTIMIZE_BIT
                    293: #else
                    294:                cgi ? String::UL_HTML|String::UL_OPTIMIZE_BIT : String::UL_AS_IS
                    295: #endif
                    296:                ,
1.130     paf       297:                true /* status_allowed */);
1.122     parser    298:        
                    299:        // some root-controlled location
                    300: #ifdef SYSCONFDIR
                    301:        const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME;
                    302: #else
                    303: #      ifdef WIN32
                    304:        // c:\windows
                    305:        char root_config_path[MAX_STRING];
                    306:        GetWindowsDirectory(root_config_path, MAX_STRING);
                    307:        
                    308:        char root_config_filespec[MAX_STRING];
                    309:        snprintf(root_config_filespec, MAX_STRING, 
                    310:                "%s/%s", 
                    311:                root_config_path, CONFIG_FILE_NAME);
                    312: #      else
                    313: #error must be compiled either configure/make or MSVC++
                    314: #      endif
                    315: #endif
                    316:        
                    317:        // beside by binary
                    318:        // @todo full path, not ./!
                    319:        static char site_config_path[MAX_STRING];
                    320:        strncpy(site_config_path, argv0, MAX_STRING-1);  site_config_path[MAX_STRING-1]=0; // filespec of my binary
                    321:        if(!(
                    322:                rsplit(site_config_path, '/') || 
                    323:                rsplit(site_config_path, '\\'))) { // strip filename
                    324:                // no path, just filename
                    325:                site_config_path[0]='.'; site_config_path[1]=0;
                    326:        }
                    327:        
                    328:        char site_config_filespec[MAX_STRING];
                    329:        snprintf(site_config_filespec, MAX_STRING, 
                    330:                "%s/%s", 
                    331:                site_config_path, CONFIG_FILE_NAME);
                    332:        
                    333:        // process the request
                    334:        request.core(
                    335:                root_config_filespec, false,
                    336:                site_config_filespec, false,
                    337:                header_only);
                    338:        
                    339:        //
                    340:        done_socks();
                    341:        
                    342: #ifdef DEBUG_POOL_MALLOC
                    343:        extern void log_pool_stats(Pool& pool);
                    344:        log_pool_stats(pool);
                    345: #endif
                    346: }
                    347: 
                    348: void call_real_parser_handler__do_SEH(
                    349:                                                                 const char *filespec_to_process,
                    350:                                                                 const char *request_method, bool header_only) {
1.133     paf       351: #if _MSC_VER && !defined(_DEBUG)
1.122     parser    352:        LPEXCEPTION_POINTERS system_exception=0;
                    353:        __try {
                    354: #endif
                    355:                real_parser_handler(
                    356:                        filespec_to_process,
                    357:                        request_method, header_only);
                    358:                
1.133     paf       359: #if _MSC_VER && !defined(_DEBUG)
1.122     parser    360:        } __except (
                    361:                (system_exception=GetExceptionInformation()), 
                    362:                EXCEPTION_EXECUTE_HANDLER) {
                    363:                
                    364:                if(system_exception)
                    365:                        if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
                    366:                                throw Exception(0, 0,
                    367:                                0,
                    368:                                "Exception 0x%08X at 0x%08X", er->ExceptionCode,  er->ExceptionAddress);
                    369:                        else
                    370:                                throw Exception(0, 0, 0, "Exception <no exception record>");
                    371:                        else
                    372:                                throw Exception(0, 0, 0, "Exception <no exception information>");
                    373:        }
                    374: #endif
                    375: }
                    376: 
1.139     paf       377: #if _MSC_VER
1.135     paf       378: int failed_new(size_t size) {
                    379:        SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
                    380:        return 0; // not reached
1.131     paf       381: }
1.135     paf       382: #endif
1.131     paf       383: 
1.135     paf       384: #ifdef HAVE_SET_NEW_HANDLER
1.134     paf       385: void failed_new() {
1.135     paf       386:     SAPI::die("out of memory in 'new'");
1.131     paf       387: }
                    388: #endif
                    389: 
1.5       paf       390: int main(int argc, char *argv[]) {
1.144     paf       391: //     _asm int 3;
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.146     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.139     paf       422: #if _MSC_VER
1.138     paf       423:        _set_new_handler(failed_new);
1.148     paf       424: 
                    425: #ifdef _DEBUG
                    426:        // Get current flag
                    427:        int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
                    428: 
                    429:        // Turn on leak-checking bit
                    430:        tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
                    431: 
                    432:        // Set flag to the new value
                    433:        _CrtSetDbgFlag( tmpFlag );
                    434: //     _CrtSetBreakAlloc(471);
                    435: 
                    436: #endif
                    437: 
1.138     paf       438: #endif
                    439: 
                    440: #ifdef HAVE_SET_NEW_HANDLER
                    441:        std::set_new_handler(failed_new);
                    442: #endif
                    443: 
1.109     parser    444:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36      paf       445: #ifdef WIN32
1.43      paf       446:        back_slashes_to_slashes(filespec_to_process);
1.36      paf       447: #endif
1.97      parser    448:        filespec_to_process=full_file_spec(filespec_to_process);
1.10      paf       449: 
1.109     parser    450:        const char *request_method=getenv("REQUEST_METHOD");
1.35      paf       451:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131     paf       452: 
1.122     parser    453:        try { // global try
                    454:                call_real_parser_handler__do_SEH(
                    455:                        filespec_to_process,
                    456:                        request_method, header_only);
                    457:        } catch(const Exception& e) { // global problem 
1.44      paf       458:                // don't allocate anything on pool here:
                    459:                //   possible pool' exception not catch-ed now
                    460:                //   and there could be out-of-memory exception
1.43      paf       461: 
1.144     paf       462:                SAPI::die("exception in request exception handler: %s", e.comment());
1.134     paf       463: #ifndef _DEBUG
1.131     paf       464:        } catch(...) { 
1.134     paf       465:                SAPI::die("<unknown exception>");
1.133     paf       466: #endif
1.16      paf       467:        }
1.122     parser    468:        
1.109     parser    469: 
                    470: #ifndef WIN32
                    471:        // 
                    472:        if(!cgi)
                    473:                SAPI::send_body(pool, "\n", 1);
                    474: #endif
1.134     paf       475:        return 0;
1.1       paf       476: }

E-mail: