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

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.1       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.27      paf         5: 
1.1       paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: 
1.40    ! paf         8:        $Id: parser3.C,v 1.39 2001/03/23 11:07:07 paf Exp $
1.1       paf         9: */
                     10: 
1.40    ! paf        11: #include "pa_config_includes.h"
1.3       paf        12: 
                     13: #ifdef WIN32
                     14: #      include <windows.h>
                     15: #endif
1.27      paf        16: 
                     17: #include <io.h>
1.3       paf        18: #include <stdlib.h>
1.4       paf        19: #include <stdio.h>
                     20: #include <fcntl.h>
1.3       paf        21: 
1.37      paf        22: #include "pa_sapi.h"
1.24      paf        23: #include "pa_common.h"
1.5       paf        24: #include "pa_globals.h"
1.2       paf        25: #include "pa_request.h"
1.1       paf        26: 
1.40    ! paf        27: Pool pool; // global pool [dont describe to doxygen: it confuses it with param names]
1.27      paf        28: bool cgi; ///< we were started as CGI?
1.5       paf        29: 
1.40    ! paf        30: #ifdef WIN32
        !            31: /// global system errors into parser exceptions converter
1.36      paf        32: static LONG WINAPI TopLevelExceptionFilter (struct _EXCEPTION_POINTERS *ExceptionInfo) {
1.5       paf        33:        char buf[MAX_STRING];
                     34:        if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
1.36      paf        35:                struct _EXCEPTION_RECORD *er=ExceptionInfo->ExceptionRecord;
1.31      paf        36:                snprintf(buf, MAX_STRING, "Exception %#X at %p", 
                     37:                        er->ExceptionCode, 
                     38:                        er->ExceptionAddress);
1.5       paf        39:        } else 
                     40:                strcpy(buf, "Exception <unknown>");
                     41:        
                     42:        PTHROW(0, 0,
                     43:                0,
                     44:                buf);
                     45: 
                     46:        return EXCEPTION_EXECUTE_HANDLER; // never reached
                     47: }
1.27      paf        48: #endif
                     49: 
1.40    ! paf        50: #ifdef WIN32
1.30      paf        51: static void fix_slashes(char *s) {
1.27      paf        52:        if(s)
                     53:                for(; *s; s++)
                     54:                        if(*s=='\\')
                     55:                                *s='/';
1.26      paf        56: }
1.40    ! paf        57: #endif
1.3       paf        58: 
1.37      paf        59: //@{
                     60: /// SAPI funcs decl
                     61: const char *SAPI::get_env(Pool& pool, const char *name) {
1.28      paf        62:        return getenv(name);
                     63: }
                     64: 
1.37      paf        65: uint SAPI::read_post(Pool& pool, char *buf, uint max_bytes) {
1.36      paf        66:        uint read_size=0;
1.12      paf        67:        do {
1.36      paf        68:                int chunk_size=read(fileno(stdin), 
                     69:                        buf+read_size, min(0x400*0x400, max_bytes-read_size));
1.12      paf        70:                if(chunk_size<0)
                     71:                        break;
                     72:                read_size+=chunk_size;
                     73:        } while(read_size<max_bytes);
                     74: 
                     75:        return read_size;
1.10      paf        76: }
                     77: 
1.37      paf        78: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.20      paf        79:        if(cgi)
                     80:                printf("%s: %s\n", key, value);
1.19      paf        81: }
                     82: 
1.33      paf        83: /// @todo intelligent cache-control
1.37      paf        84: void SAPI::send_header(Pool& pool) {
1.33      paf        85:        if(cgi) {
1.36      paf        86:                puts("Expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf        87: 
                     88:                // header | body  delimiter
1.20      paf        89:                puts("");
1.33      paf        90:        }
1.30      paf        91: }
1.20      paf        92: 
1.37      paf        93: void SAPI::send_body(Pool& pool, const char *buf, size_t size) {
1.19      paf        94:        stdout_write(buf, size);
1.17      paf        95: }
1.37      paf        96: //@}
1.30      paf        97: 
1.40    ! paf        98: /**
        !            99:        main workhorse
1.19      paf       100: 
1.40    ! paf       101:        @todo 
        !           102:                IIS: remove trailing default-document[index.html] from $request.uri.
        !           103:                to do that we need to consult metabase,
        !           104:                wich is tested but seems slow.
        !           105: */
1.5       paf       106: int main(int argc, char *argv[]) {
1.32      paf       107:        umask(2);
                    108: 
                    109: #ifdef WIN32
1.27      paf       110:        setmode(fileno(stdin), _O_BINARY);
                    111:        setmode(fileno(stdout), _O_BINARY);
                    112:        setmode(fileno(stderr), _O_BINARY);
1.32      paf       113: #endif
1.12      paf       114: 
1.3       paf       115:        // were we started as CGI?
1.20      paf       116:        cgi=
1.2       paf       117:                getenv("SERVER_SOFTWARE") || 
                    118:                getenv("SERVER_NAME") || 
                    119:                getenv("GATEWAY_INTERFACE") || 
                    120:                getenv("REQUEST_METHOD");
1.5       paf       121:        
1.10      paf       122:        if(!cgi) {
                    123:                if(argc<2) {
                    124:                        char *binary=argv[0];
1.13      paf       125:                        printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10      paf       126:                        exit(1);
                    127:                }
                    128:        }
                    129: 
1.26      paf       130:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36      paf       131: #ifdef WIN32
1.26      paf       132:        fix_slashes(filespec_to_process);
1.36      paf       133: #endif
1.10      paf       134: 
1.35      paf       135:        const char *request_method=getenv("REQUEST_METHOD");
                    136:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5       paf       137:        PTRY { // global try
                    138:                // must be first in PTRY{}PCATCH
1.40    ! paf       139: #ifdef WIN32
1.5       paf       140:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    141:                //TODO: initSocks();
                    142: #endif
1.2       paf       143: 
1.10      paf       144:                // init global variables
1.37      paf       145:                pa_globals_init(pool);
1.10      paf       146: 
1.13      paf       147:                if(!filespec_to_process)
                    148:                        PTHROW(0, 0,
                    149:                                0,
                    150:                                "no file to process");
                    151: 
1.10      paf       152:                // Request info
                    153:                Request::Info request_info;
1.39      paf       154:                if(cgi) {
                    155:                        if(const char *env_document_root=getenv("DOCUMENT_ROOT"))
                    156:                                request_info.document_root=env_document_root;
                    157:                        else if(const char *path_info=getenv("PATH_INFO")) {
1.40    ! paf       158:                                // IIS
1.39      paf       159:                                size_t len=strlen(filespec_to_process)-strlen(path_info);
1.40    ! paf       160:                                char *buf=(char *)pool.malloc(len+1);
        !           161:                                strncpy(buf, filespec_to_process, len);
1.39      paf       162:                                buf[len]=0;
                    163:                                request_info.document_root=buf;
                    164:                        } else
                    165:                                PTHROW(0, 0,
                    166:                                        0,
                    167:                                        "CGI: no PATH_INFO defined (in reinventing DOCUMENT_ROOT)");
                    168:                } else {
                    169:                        static char buf[MAX_STRING];
                    170:                        strncpy(buf, filespec_to_process, MAX_STRING);
                    171:                        rsplit(buf, '/');  rsplit(buf, '\\');// strip filename
                    172:                        request_info.document_root=buf;
1.13      paf       173:                }
1.10      paf       174:                request_info.path_translated=filespec_to_process;
1.35      paf       175:                request_info.method=request_method;
1.40    ! paf       176:                const char *query_string=getenv("QUERY_STRING");
        !           177:                request_info.query_string=query_string;
        !           178:                if(const char *env_request_uri=getenv("REQUEST_URI"))
        !           179:                        request_info.uri=env_request_uri;
        !           180:                else if (const char *path_info=getenv("PATH_INFO")) {
        !           181:                        if(query_string) {
        !           182:                                char *reconstructed_uri=(char *)malloc(
        !           183:                                        strlen(path_info)+1/*'?'*/+
        !           184:                                        strlen(query_string)+1/*0*/);
        !           185:                                strcpy(reconstructed_uri, path_info);
        !           186:                                strcat(reconstructed_uri, "?");
        !           187:                                strcat(reconstructed_uri, query_string);
        !           188:                                request_info.uri=reconstructed_uri;
        !           189:                        } else
        !           190:                                request_info.uri=path_info;
        !           191:                } else
        !           192:                        PTHROW(0, 0,
        !           193:                                0,
        !           194:                                "CGI: no PATH_INFO defined (in reinventing REQUEST_URI)");
        !           195: 
1.10      paf       196:                request_info.content_type=getenv("CONTENT_TYPE");
                    197:                const char *content_length=getenv("CONTENT_LENGTH");
                    198:                request_info.content_length=(content_length?atoi(content_length):0);
1.21      paf       199:                request_info.cookie=getenv("HTTP_COOKIE");
1.10      paf       200: 
1.5       paf       201:                // prepare to process request
1.38      paf       202:                Request request(pool,
1.10      paf       203:                        request_info,
1.27      paf       204:                        cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5       paf       205:                        );
                    206:                
                    207:                // some root-controlled location
1.3       paf       208: #ifdef WIN32
1.10      paf       209:                // c:\windows
1.36      paf       210:                static char root_auto_path[MAX_STRING];
1.30      paf       211:                GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3       paf       212: #else
1.40    ! paf       213:                // ~nobody  todo: figure out a better place
1.36      paf       214:                char *root_auto_path=getenv("HOME");
1.5       paf       215: #endif
                    216:                
                    217:                // beside by binary
1.36      paf       218:                static char site_auto_path[MAX_STRING];
1.30      paf       219:                strncpy(site_auto_path, argv[0], MAX_STRING);  // filespec of my binary
                    220:                rsplit(site_auto_path, '/');  rsplit(site_auto_path, '\\');// strip filename
1.5       paf       221:                
                    222:                // process the request
1.35      paf       223:                request.core(
1.30      paf       224:                        root_auto_path, false,
1.31      paf       225:                        site_auto_path, false,
1.35      paf       226:                        header_only);
1.16      paf       227: 
1.22      paf       228:                // must be last in PTRY{}PCATCH
1.40    ! paf       229: #ifdef WIN32
1.5       paf       230:                SetUnhandledExceptionFilter(0);
1.25      paf       231: #endif
1.16      paf       232:                // successful finish
                    233:                return 0;
                    234:        } PCATCH(e) { // global problem 
1.19      paf       235:                const char *body=e.comment();
                    236:                int content_length=strlen(body);
1.5       paf       237: 
1.35      paf       238:                // prepare header
1.37      paf       239:                SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19      paf       240:                char content_length_cstr[MAX_NUMBER];
1.34      paf       241:                snprintf(content_length_cstr, MAX_NUMBER, "%lu", content_length);
1.37      paf       242:                SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35      paf       243: 
                    244:                // send header
1.37      paf       245:                SAPI::send_header(pool);
1.19      paf       246: 
                    247:                // body
1.35      paf       248:                if(!header_only)
1.37      paf       249:                        SAPI::send_body(pool, body, content_length);
1.2       paf       250: 
1.16      paf       251:                // unsuccessful finish
                    252:                return 1;
                    253:        }
                    254:        PEND_CATCH
1.1       paf       255: }

E-mail: