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

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

E-mail: