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

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

E-mail: