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

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.16    ! paf         6:        $Id: parser3.C,v 1.15 2001/03/16 12:30:25 paf Exp $
1.1       paf         7: */
                      8: 
1.5       paf         9: #ifdef HAVE_CONFIG_H
                     10: #      include "pa_config.h"
                     11: #endif
                     12: 
1.3       paf        13: 
                     14: #ifdef WIN32
                     15: #      include <windows.h>
                     16: #      include <io.h>
                     17: #endif
                     18: #include <stdlib.h>
1.4       paf        19: #include <stdio.h>
                     20: #include <string.h>
                     21: #include <fcntl.h>
1.3       paf        22: 
1.5       paf        23: #include "pa_globals.h"
1.2       paf        24: #include "pa_request.h"
                     25: #include "pa_common.h"
1.1       paf        26: 
1.5       paf        27: Pool pool; // global pool
                     28: 
1.3       paf        29: #ifdef WIN32
1.14      paf        30: #      if _MSC_VER
1.5       paf        31: // intercept global system errors
                     32: LONG WINAPI TopLevelExceptionFilter (
                     33:                                                                         struct _EXCEPTION_POINTERS *ExceptionInfo
                     34:                                                                         ) {
                     35:        char buf[MAX_STRING];
                     36:        if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
                     37:                struct _EXCEPTION_RECORD *r=ExceptionInfo->ExceptionRecord;
                     38:                
                     39:                int printed=0;
                     40:                printed+=snprintf(buf+printed, MAX_STRING-printed, "Exception 0x%X at 0x%p", 
                     41:                        r->ExceptionCode, 
                     42:                        r->ExceptionAddress);
                     43:                for(unsigned int i=0; i<r->NumberParameters; i++)
                     44:                        printed+=snprintf(buf+printed, MAX_STRING-printed, ", 0x%X", 
                     45:                                r->ExceptionInformation[i]);
                     46:        } else 
                     47:                strcpy(buf, "Exception <unknown>");
                     48:        
                     49:        PTHROW(0, 0,
                     50:                0,
                     51:                buf);
                     52: 
                     53:        return EXCEPTION_EXECUTE_HANDLER; // never reached
                     54: }
1.8       paf        55: #      endif
1.3       paf        56: #endif
                     57: 
1.12      paf        58: int read_post(char *buf, int max_bytes) {
                     59:        int read_size=0;
                     60:        do {
                     61:                int chunk_size=read
                     62:                        (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size));
                     63:                if(chunk_size<0)
                     64:                        break;
                     65:                read_size+=chunk_size;
                     66:        } while(read_size<max_bytes);
                     67: 
                     68:        return read_size;
1.10      paf        69: }
                     70: 
1.5       paf        71: int main(int argc, char *argv[]) {
1.12      paf        72:        //TODO: umask(2);
                     73: #ifdef WIN32
                     74:        _setmode(fileno(stdin), _O_BINARY);
                     75:        _setmode(fileno(stdout), _O_BINARY);
                     76:        _setmode(fileno(stderr), _O_BINARY);
                     77: #endif
                     78: 
1.10      paf        79:        // Service funcs 
                     80:        service_funcs.read_post=read_post;
                     81:        
1.3       paf        82:        // were we started as CGI?
1.2       paf        83:        bool cgi=
                     84:                getenv("SERVER_SOFTWARE") || 
                     85:                getenv("SERVER_NAME") || 
                     86:                getenv("GATEWAY_INTERFACE") || 
                     87:                getenv("REQUEST_METHOD");
1.5       paf        88:        
1.10      paf        89:        if(!cgi) {
                     90:                if(argc<2) {
                     91:                        char *binary=argv[0];
1.13      paf        92:                        printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10      paf        93:                        exit(1);
                     94:                }
                     95:        }
                     96: 
                     97:        const char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
                     98: 
1.5       paf        99:        PTRY { // global try
                    100:                // must be first in PTRY{}PCATCH
                    101: #ifdef WIN32
1.14      paf       102: #      if _MSC_VER
1.5       paf       103:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    104:                //TODO: initSocks();
1.8       paf       105: #      endif
1.5       paf       106: #endif
1.2       paf       107: 
1.10      paf       108:                // init global variables
1.9       paf       109:                globals_init(pool);
1.10      paf       110: 
1.13      paf       111:                if(!filespec_to_process)
                    112:                        PTHROW(0, 0,
                    113:                                0,
                    114:                                "no file to process");
                    115: 
1.10      paf       116:                // Request info
1.5       paf       117:                // TODO: ifdef WIN32 flip \\ to /
1.10      paf       118:                Request::Info request_info;
1.13      paf       119:                const char *document_root=getenv("DOCUMENT_ROOT");
                    120:                if(!document_root) {
                    121:                        static char fake_document_root[MAX_STRING];
                    122:                        strncpy(fake_document_root, filespec_to_process, MAX_STRING);
                    123:                        rsplit(fake_document_root, '/');  rsplit(fake_document_root, '\\');// strip filename
                    124:                        document_root=fake_document_root;
                    125:                }
                    126:                request_info.document_root=document_root;
1.10      paf       127:                request_info.path_translated=filespec_to_process;
1.15      paf       128:                request_info.method=getenv("REQUEST_METHOD");
1.10      paf       129:                request_info.query_string=getenv("QUERY_STRING");
1.15      paf       130:                request_info.uri=getenv("REQUEST_URI");
1.10      paf       131:                request_info.content_type=getenv("CONTENT_TYPE");
                    132:                const char *content_length=getenv("CONTENT_LENGTH");
                    133:                request_info.content_length=(content_length?atoi(content_length):0);
                    134: 
1.5       paf       135:                // prepare to process request
1.6       paf       136:                Request request(Pool(),
1.10      paf       137:                        request_info,
                    138:                        cgi ? String::Untaint_lang::HTML_TYPO : String::Untaint_lang::NO
1.5       paf       139:                        );
                    140:                
                    141:                // some root-controlled location
                    142:                char *sys_auto_path1;
1.3       paf       143: #ifdef WIN32
1.10      paf       144:                // c:\windows
1.5       paf       145:                sys_auto_path1=(char *)pool.malloc(MAX_STRING);
1.7       paf       146:                GetWindowsDirectory(sys_auto_path1, MAX_STRING);
1.10      paf       147:                strcat(sys_auto_path1, PATH_DELIMITER_STRING);
1.3       paf       148: #else
1.10      paf       149:                // ~nobody
1.5       paf       150:                sys_auto_path1=getenv("HOME");
                    151: #endif
                    152:                
                    153:                // beside by binary
                    154:                char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
1.6       paf       155:                strncpy(sys_auto_path2, argv[0], MAX_STRING);  // filespec of my binary
1.13      paf       156:                rsplit(sys_auto_path2, '/');  rsplit(sys_auto_path2, '\\');// strip filename
                    157:                strcat(sys_auto_path2, PATH_DELIMITER_STRING);
1.5       paf       158:                
                    159:                // process the request
1.16    ! paf       160:                request.core(pool.exception(),
1.5       paf       161:                        sys_auto_path1, 
                    162:                        sys_auto_path2);
                    163: 
1.16    ! paf       164:                // extract request.response body
        !           165:                Value *body_value=static_cast<Value *>(
        !           166:                        request.response.fields().get(*body_name));
        !           167:                const char *body=body_value?
        !           168:                        body_value->as_string().cstr():"no body";// TODO: IMAGE&FILE
        !           169: 
        !           170:                // OK. write out the result
        !           171:                if(cgi) {
        !           172:                        // content-type:
        !           173:                        Value *content_type_value=static_cast<Value *>(
        !           174:                                request.response.fields().get(*content_type_name));
        !           175:                        const char *content_type=content_type_value?
        !           176:                                content_type_value->as_string().cstr():"text/html";
        !           177: 
        !           178:                        // header
        !           179:                        printf(
        !           180:                                "Content-type: %s\n"
        !           181:                                "Content-length: %d\n"
        !           182:                                "\n", 
        !           183:                                content_type,
        !           184:                                strlen(body));
        !           185:                }
        !           186:                // body
        !           187:                stdout_write(body);
        !           188: 
        !           189:                        // must be last in PTRY{}PCATCH
1.5       paf       190: #ifdef WIN32
1.14      paf       191: #      if _MSC_VER
1.5       paf       192:                SetUnhandledExceptionFilter(0);
1.8       paf       193: #      endif
1.16    ! paf       194:                // successful finish
        !           195:                return 0;
1.3       paf       196: #endif
1.16    ! paf       197:        } PCATCH(e) { // global problem 
        !           198:                // @globals fill 
        !           199:                // @Request create 
        !           200:                // @prepare to .core()
        !           201:                // @request.core when reporting request exception
        !           202:                // @write result
        !           203:                const char *error=e.comment();
1.5       paf       204: 
1.16    ! paf       205:                if(cgi) {
1.4       paf       206:                        printf(
                    207:                                "Content-type: text/plain\n"
                    208:                                "Content-length: %d\n"
                    209:                                "\n", 
                    210:                                strlen(error));
                    211:                        stdout_write(error);
1.16    ! paf       212:                } else
1.4       paf       213:                        fputs(error, stderr);
1.2       paf       214: 
1.16    ! paf       215:                // unsuccessful finish
        !           216:                return 1;
        !           217:        }
        !           218:        PEND_CATCH
1.1       paf       219: }

E-mail: