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

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.26.2.2! paf         6:        $Id: parser3.C,v 1.26.2.1 2001/03/20 17:21:56 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>
1.26.2.2! paf        17: #else
        !            18: #      include <unistd.h>
1.3       paf        19: #endif
                     20: #include <stdlib.h>
1.4       paf        21: #include <stdio.h>
                     22: #include <string.h>
                     23: #include <fcntl.h>
1.3       paf        24: 
1.24      paf        25: #include "pa_common.h"
1.5       paf        26: #include "pa_globals.h"
1.2       paf        27: #include "pa_request.h"
1.1       paf        28: 
1.5       paf        29: Pool pool; // global pool
1.20      paf        30: bool cgi;
1.5       paf        31: 
1.3       paf        32: #ifdef WIN32
1.14      paf        33: #      if _MSC_VER
1.5       paf        34: // intercept global system errors
                     35: LONG WINAPI TopLevelExceptionFilter (
                     36:                                                                         struct _EXCEPTION_POINTERS *ExceptionInfo
                     37:                                                                         ) {
                     38:        char buf[MAX_STRING];
                     39:        if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
                     40:                struct _EXCEPTION_RECORD *r=ExceptionInfo->ExceptionRecord;
                     41:                
                     42:                int printed=0;
                     43:                printed+=snprintf(buf+printed, MAX_STRING-printed, "Exception 0x%X at 0x%p", 
                     44:                        r->ExceptionCode, 
                     45:                        r->ExceptionAddress);
                     46:                for(unsigned int i=0; i<r->NumberParameters; i++)
                     47:                        printed+=snprintf(buf+printed, MAX_STRING-printed, ", 0x%X", 
                     48:                                r->ExceptionInformation[i]);
                     49:        } else 
                     50:                strcpy(buf, "Exception <unknown>");
                     51:        
                     52:        PTHROW(0, 0,
                     53:                0,
                     54:                buf);
                     55: 
                     56:        return EXCEPTION_EXECUTE_HANDLER; // never reached
                     57: }
1.8       paf        58: #      endif
1.26      paf        59: 
                     60: void fix_slashes(char *s) {
                     61:        for(; *s; s++)
                     62:                if(*s=='\\')
                     63:                        *s='/';
                     64: }
1.3       paf        65: #endif
                     66: 
1.19      paf        67: // service funcs
                     68: 
1.12      paf        69: int read_post(char *buf, int max_bytes) {
                     70:        int read_size=0;
                     71:        do {
                     72:                int chunk_size=read
                     73:                        (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size));
                     74:                if(chunk_size<0)
                     75:                        break;
                     76:                read_size+=chunk_size;
                     77:        } while(read_size<max_bytes);
                     78: 
                     79:        return read_size;
1.10      paf        80: }
                     81: 
1.19      paf        82: void output_header_attribute(const char *key, const char *value) {
1.20      paf        83:        if(cgi)
                     84:                printf("%s: %s\n", key, value);
1.19      paf        85: }
                     86: 
                     87: void output_body(const char *buf, size_t size) {
1.20      paf        88:        if(cgi) // header | body  delimiter
                     89:                puts("");
                     90: 
1.19      paf        91:        stdout_write(buf, size);
1.17      paf        92: }
                     93: 
1.19      paf        94: // main
                     95: 
1.5       paf        96: int main(int argc, char *argv[]) {
1.23      paf        97:        // TODO:umask(2);
1.12      paf        98: #ifdef WIN32
                     99:        _setmode(fileno(stdin), _O_BINARY);
                    100:        _setmode(fileno(stdout), _O_BINARY);
                    101:        _setmode(fileno(stderr), _O_BINARY);
                    102: #endif
                    103: 
1.10      paf       104:        // Service funcs 
                    105:        service_funcs.read_post=read_post;
1.19      paf       106:        service_funcs.output_header_attribute=output_header_attribute;
                    107:        service_funcs.output_body=output_body;
1.10      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];
                    125: #ifdef WIN32
                    126:        fix_slashes(filespec_to_process);
                    127: #endif
1.10      paf       128: 
1.5       paf       129:        PTRY { // global try
                    130:                // must be first in PTRY{}PCATCH
                    131: #ifdef WIN32
1.14      paf       132: #      if _MSC_VER
1.5       paf       133:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    134:                //TODO: initSocks();
1.8       paf       135: #      endif
1.5       paf       136: #endif
1.2       paf       137: 
1.10      paf       138:                // init global variables
1.9       paf       139:                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.13      paf       148:                const char *document_root=getenv("DOCUMENT_ROOT");
                    149:                if(!document_root) {
                    150:                        static char fake_document_root[MAX_STRING];
                    151:                        strncpy(fake_document_root, filespec_to_process, MAX_STRING);
                    152:                        rsplit(fake_document_root, '/');  rsplit(fake_document_root, '\\');// strip filename
                    153:                        document_root=fake_document_root;
                    154:                }
                    155:                request_info.document_root=document_root;
1.10      paf       156:                request_info.path_translated=filespec_to_process;
1.15      paf       157:                request_info.method=getenv("REQUEST_METHOD");
1.10      paf       158:                request_info.query_string=getenv("QUERY_STRING");
1.15      paf       159:                request_info.uri=getenv("REQUEST_URI");
1.10      paf       160:                request_info.content_type=getenv("CONTENT_TYPE");
                    161:                const char *content_length=getenv("CONTENT_LENGTH");
                    162:                request_info.content_length=(content_length?atoi(content_length):0);
1.21      paf       163:                request_info.cookie=getenv("HTTP_COOKIE");
1.10      paf       164: 
1.5       paf       165:                // prepare to process request
1.26.2.2! paf       166:                Pool request_pool;
        !           167:                Request request(request_pool,
1.10      paf       168:                        request_info,
1.26.2.1  paf       169:                        cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5       paf       170:                        );
                    171:                
                    172:                // some root-controlled location
                    173:                char *sys_auto_path1;
1.3       paf       174: #ifdef WIN32
1.10      paf       175:                // c:\windows
1.5       paf       176:                sys_auto_path1=(char *)pool.malloc(MAX_STRING);
1.7       paf       177:                GetWindowsDirectory(sys_auto_path1, MAX_STRING);
1.10      paf       178:                strcat(sys_auto_path1, PATH_DELIMITER_STRING);
1.3       paf       179: #else
1.10      paf       180:                // ~nobody
1.5       paf       181:                sys_auto_path1=getenv("HOME");
                    182: #endif
                    183:                
                    184:                // beside by binary
                    185:                char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
1.6       paf       186:                strncpy(sys_auto_path2, argv[0], MAX_STRING);  // filespec of my binary
1.13      paf       187:                rsplit(sys_auto_path2, '/');  rsplit(sys_auto_path2, '\\');// strip filename
                    188:                strcat(sys_auto_path2, PATH_DELIMITER_STRING);
1.5       paf       189:                
                    190:                // process the request
1.16      paf       191:                request.core(pool.exception(),
1.5       paf       192:                        sys_auto_path1, 
                    193:                        sys_auto_path2);
1.19      paf       194:                // no actions with request' data past this point
                    195:                // request.exception not not handled here, but all
                    196:                // request' data are associated with it's pool=exception
1.16      paf       197: 
1.22      paf       198:                // must be last in PTRY{}PCATCH
1.5       paf       199: #ifdef WIN32
1.14      paf       200: #      if _MSC_VER
1.5       paf       201:                SetUnhandledExceptionFilter(0);
1.8       paf       202: #      endif
1.25      paf       203: #endif
1.16      paf       204:                // successful finish
                    205:                return 0;
                    206:        } PCATCH(e) { // global problem 
1.19      paf       207:                const char *body=e.comment();
                    208:                int content_length=strlen(body);
1.5       paf       209: 
1.19      paf       210:                // header
                    211:                (*service_funcs.output_header_attribute)("content-type", "text/plain");
                    212:                char content_length_cstr[MAX_NUMBER];
                    213:                snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
                    214:                (*service_funcs.output_header_attribute)("content-length", 
                    215:                        content_length_cstr);
                    216: 
                    217:                // body
                    218:                (*service_funcs.output_body)(body, content_length);
1.2       paf       219: 
1.16      paf       220:                // unsuccessful finish
                    221:                return 1;
                    222:        }
                    223:        PEND_CATCH
1.1       paf       224: }

E-mail: