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

1.1       paf         1: /*
                      2:        Parser
                      3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      5: 
1.4     ! paf         6:        $Id: parser3.C,v 1.3 2001/03/13 18:32:48 paf Exp $
1.1       paf         7: */
                      8: 
1.3       paf         9: #include "pa_config.h"
                     10: 
                     11: #ifdef WIN32
                     12: #      include <windows.h>
                     13: #      include <io.h>
                     14: #endif
                     15: #include <stdlib.h>
1.4     ! paf        16: #include <stdio.h>
        !            17: #include <string.h>
        !            18: #include <fcntl.h>
1.3       paf        19: 
1.1       paf        20: #include "core.h"
1.2       paf        21: #include "pa_request.h"
                     22: #include "pa_common.h"
1.1       paf        23: 
1.3       paf        24: #ifdef WIN32
                     25: // TODO:  LONG WINAPI TopLevelExceptionFilter (
                     26: #endif
                     27: 
1.1       paf        28: int main(int argc, char *argv[]) {
1.3       paf        29: #ifdef WIN32
                     30:        _fmode=_O_BINARY;                       /*sets default for file streams to binary */
1.4     ! paf        31:        setmode(_fileno(stdin), _O_BINARY);             /* make the stdio mode be binary */
        !            32:        setmode(_fileno(stdout), _O_BINARY);            /* make the stdio mode be binary */
        !            33:        setmode(_fileno(stderr), _O_BINARY);            /* make the stdio mode be binary */
1.3       paf        34: 
                     35:        //TODO: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                     36:        //TODO: initSocks();
                     37: #endif
                     38: 
                     39:        
1.2       paf        40:        Pool pool;
                     41:        core(pool);
                     42: 
1.3       paf        43:        // were we started as CGI?
1.2       paf        44:        bool cgi=
                     45:                getenv("SERVER_SOFTWARE") || 
                     46:                getenv("SERVER_NAME") || 
                     47:                getenv("GATEWAY_INTERFACE") || 
                     48:                getenv("REQUEST_METHOD");
                     49: 
                     50:        // TODO: ifdef WIN32 flip \\ to /
1.3       paf        51:        const char *document_root="Y:/parser3/src/";
                     52:        const char *page_filespec="Y:/parser3/src/test.p";
1.2       paf        53: 
                     54:        // request
                     55:        Request request(pool,
1.3       paf        56:                cgi ? String::Untaint_lang::HTML_TYPO : String::Untaint_lang::NO,
1.2       paf        57:                document_root,
                     58:                page_filespec
                     59:                );
1.3       paf        60:        
                     61:        // some root-controlled location
                     62:        char *sys_auto_path1;
                     63: #ifdef WIN32
                     64:        sys_auto_path1=(char *)pool.malloc(MAX_STRING);
                     65:        GetWindowsDirectory(sys_auto_path1, MAX_STRING-1/*for \*/);
1.4     ! paf        66:        strcat(sys_auto_path1, "\\");
1.3       paf        67: #else
                     68:        sys_auto_path1=getenv("HOME");
                     69: #endif
                     70:        
                     71:        // beside by binary
1.4     ! paf        72:        char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
        !            73:        strncpy(sys_auto_path2, argv[0], MAX_STRING-20);  // filespec of my binary
1.3       paf        74:        rsplit(sys_auto_path2, '\\');  rsplit(sys_auto_path2, '/'); // strip filename
                     75:        
                     76:        char *result=request.core(
                     77:                sys_auto_path1, 
                     78:                sys_auto_path2);
                     79:        
1.4     ! paf        80:        const char *error="nested exception";
1.3       paf        81:        if(cgi) {
1.4     ! paf        82:                if(result) {
        !            83:                        const char *content_type="text/html";
        !            84:                        printf(
        !            85:                                "Content-type: %s\n"
        !            86:                                "Content-length: %d\n"
        !            87:                                "\n", 
1.3       paf        88:                                content_type,
                     89:                                strlen(result));
1.4     ! paf        90:                        stdout_write(result);
        !            91:                } else {
        !            92:                        printf(
        !            93:                                "Content-type: text/plain\n"
        !            94:                                "Content-length: %d\n"
        !            95:                                "\n", 
        !            96:                                strlen(error));
        !            97:                        stdout_write(error);
        !            98:                }
1.3       paf        99:        } else
1.4     ! paf       100:                if(result)
        !           101:                        printf("%s", result);
        !           102:                else
        !           103:                        fputs(error, stderr);
1.2       paf       104: 
1.1       paf       105:        return 0;
                    106: }

E-mail: