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

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

E-mail: