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

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.1       paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.27      paf         5: 
1.1       paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: 
1.32    ! paf         8:        $Id: parser3.C,v 1.31 2001/03/22 15:30:47 paf Exp $
1.1       paf         9: */
                     10: 
1.5       paf        11: #ifdef HAVE_CONFIG_H
                     12: #      include "pa_config.h"
                     13: #endif
                     14: 
1.3       paf        15: 
                     16: #ifdef WIN32
                     17: #      include <windows.h>
                     18: #      include <io.h>
1.27      paf        19: #else
                     20: #      include <unistd.h>
1.3       paf        21: #endif
1.27      paf        22: 
                     23: //\ifwin32
                     24: #include <io.h>
                     25: //#include <fcntl.h>
                     26: //\endifwin32
                     27: 
1.3       paf        28: #include <stdlib.h>
1.4       paf        29: #include <stdio.h>
                     30: #include <string.h>
                     31: #include <fcntl.h>
1.3       paf        32: 
1.24      paf        33: #include "pa_common.h"
1.5       paf        34: #include "pa_globals.h"
1.2       paf        35: #include "pa_request.h"
1.1       paf        36: 
1.29      paf        37: Pool pool; // global pool
1.27      paf        38: bool cgi; ///< we were started as CGI?
1.5       paf        39: 
1.3       paf        40: #ifdef WIN32
1.14      paf        41: #      if _MSC_VER
1.5       paf        42: // intercept global system errors
1.30      paf        43: static LONG WINAPI TopLevelExceptionFilter (
1.5       paf        44:                                                                         struct _EXCEPTION_POINTERS *ExceptionInfo
                     45:                                                                         ) {
                     46:        char buf[MAX_STRING];
                     47:        if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
1.31      paf        48:                struct _EXCEPTION_RECORD *rr=ExceptionInfo->ExceptionRecord;
                     49:                snprintf(buf, MAX_STRING, "Exception %#X at %p", 
                     50:                        er->ExceptionCode, 
                     51:                        er->ExceptionAddress);
1.5       paf        52:        } else 
                     53:                strcpy(buf, "Exception <unknown>");
                     54:        
                     55:        PTHROW(0, 0,
                     56:                0,
                     57:                buf);
                     58: 
                     59:        return EXCEPTION_EXECUTE_HANDLER; // never reached
                     60: }
1.8       paf        61: #      endif
1.26      paf        62: 
1.27      paf        63: #endif
                     64: 
                     65: //\if
1.30      paf        66: static void fix_slashes(char *s) {
1.27      paf        67:        if(s)
                     68:                for(; *s; s++)
                     69:                        if(*s=='\\')
                     70:                                *s='/';
1.26      paf        71: }
1.27      paf        72: //\endif
1.3       paf        73: 
1.19      paf        74: // service funcs
                     75: 
1.30      paf        76: static const char *get_env(Pool& pool, const char *name) {
1.28      paf        77:        return getenv(name);
                     78: }
                     79: 
1.30      paf        80: static uint read_post(char *buf, uint max_bytes) {
1.12      paf        81:        int read_size=0;
                     82:        do {
                     83:                int chunk_size=read
                     84:                        (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size));
                     85:                if(chunk_size<0)
                     86:                        break;
                     87:                read_size+=chunk_size;
                     88:        } while(read_size<max_bytes);
                     89: 
                     90:        return read_size;
1.10      paf        91: }
                     92: 
1.30      paf        93: static void add_header_attribute(const char *key, const char *value) {
1.20      paf        94:        if(cgi)
                     95:                printf("%s: %s\n", key, value);
1.19      paf        96: }
                     97: 
1.30      paf        98: static void send_header(const char *buf, size_t size) {
1.20      paf        99:        if(cgi) // header | body  delimiter
                    100:                puts("");
1.30      paf       101: }
1.20      paf       102: 
1.30      paf       103: static void send_body(const char *buf, size_t size) {
1.19      paf       104:        stdout_write(buf, size);
1.17      paf       105: }
                    106: 
1.30      paf       107: /// Service funcs 
                    108:  Service_funcs service_funcs={
                    109:                get_env,
                    110:                read_post,
                    111:                add_header_attribute,
                    112:                send_header,
                    113:                send_body
                    114:  };
                    115: 
                    116: 
1.19      paf       117: // main
                    118: 
1.5       paf       119: int main(int argc, char *argv[]) {
1.32    ! paf       120:        umask(2);
        !           121: 
        !           122: #ifdef WIN32
1.27      paf       123:        setmode(fileno(stdin), _O_BINARY);
                    124:        setmode(fileno(stdout), _O_BINARY);
                    125:        setmode(fileno(stderr), _O_BINARY);
1.32    ! paf       126: #endif
1.12      paf       127: 
1.3       paf       128:        // were we started as CGI?
1.20      paf       129:        cgi=
1.2       paf       130:                getenv("SERVER_SOFTWARE") || 
                    131:                getenv("SERVER_NAME") || 
                    132:                getenv("GATEWAY_INTERFACE") || 
                    133:                getenv("REQUEST_METHOD");
1.5       paf       134:        
1.10      paf       135:        if(!cgi) {
                    136:                if(argc<2) {
                    137:                        char *binary=argv[0];
1.13      paf       138:                        printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10      paf       139:                        exit(1);
                    140:                }
                    141:        }
                    142: 
1.26      paf       143:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.27      paf       144: //\#ifdef WIN32
1.26      paf       145:        fix_slashes(filespec_to_process);
1.27      paf       146: //\#endif
1.10      paf       147: 
1.5       paf       148:        PTRY { // global try
                    149:                // must be first in PTRY{}PCATCH
                    150: #ifdef WIN32
1.14      paf       151: #      if _MSC_VER
1.5       paf       152:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    153:                //TODO: initSocks();
1.8       paf       154: #      endif
1.5       paf       155: #endif
1.2       paf       156: 
1.10      paf       157:                // init global variables
1.9       paf       158:                globals_init(pool);
1.10      paf       159: 
1.13      paf       160:                if(!filespec_to_process)
                    161:                        PTHROW(0, 0,
                    162:                                0,
                    163:                                "no file to process");
                    164: 
1.10      paf       165:                // Request info
                    166:                Request::Info request_info;
1.13      paf       167:                const char *document_root=getenv("DOCUMENT_ROOT");
                    168:                if(!document_root) {
                    169:                        static char fake_document_root[MAX_STRING];
                    170:                        strncpy(fake_document_root, filespec_to_process, MAX_STRING);
                    171:                        rsplit(fake_document_root, '/');  rsplit(fake_document_root, '\\');// strip filename
                    172:                        document_root=fake_document_root;
                    173:                }
                    174:                request_info.document_root=document_root;
1.10      paf       175:                request_info.path_translated=filespec_to_process;
1.15      paf       176:                request_info.method=getenv("REQUEST_METHOD");
1.10      paf       177:                request_info.query_string=getenv("QUERY_STRING");
1.15      paf       178:                request_info.uri=getenv("REQUEST_URI");
1.10      paf       179:                request_info.content_type=getenv("CONTENT_TYPE");
                    180:                const char *content_length=getenv("CONTENT_LENGTH");
                    181:                request_info.content_length=(content_length?atoi(content_length):0);
1.21      paf       182:                request_info.cookie=getenv("HTTP_COOKIE");
1.10      paf       183: 
1.5       paf       184:                // prepare to process request
1.27      paf       185:                Pool request_pool;
                    186:                Request request(request_pool,
1.10      paf       187:                        request_info,
1.27      paf       188:                        cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5       paf       189:                        );
                    190:                
                    191:                // some root-controlled location
1.30      paf       192:                char *root_auto_path;
1.3       paf       193: #ifdef WIN32
1.10      paf       194:                // c:\windows
1.30      paf       195:                root_auto_path=(char *)pool.malloc(MAX_STRING);
                    196:                GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3       paf       197: #else
1.10      paf       198:                // ~nobody
1.30      paf       199:                root_auto_path=getenv("HOME");
1.5       paf       200: #endif
                    201:                
                    202:                // beside by binary
1.30      paf       203:                char *site_auto_path=(char *)pool.malloc(MAX_STRING);
                    204:                strncpy(site_auto_path, argv[0], MAX_STRING);  // filespec of my binary
                    205:                rsplit(site_auto_path, '/');  rsplit(site_auto_path, '\\');// strip filename
1.5       paf       206:                
                    207:                // process the request
1.16      paf       208:                request.core(pool.exception(),
1.30      paf       209:                        root_auto_path, false,
1.31      paf       210:                        site_auto_path, false,
                    211:                        strcasecmp(request_info.method, "HEAD")==0);
1.16      paf       212: 
1.22      paf       213:                // must be last in PTRY{}PCATCH
1.5       paf       214: #ifdef WIN32
1.14      paf       215: #      if _MSC_VER
1.5       paf       216:                SetUnhandledExceptionFilter(0);
1.8       paf       217: #      endif
1.25      paf       218: #endif
1.16      paf       219:                // successful finish
                    220:                return 0;
                    221:        } PCATCH(e) { // global problem 
1.19      paf       222:                const char *body=e.comment();
                    223:                int content_length=strlen(body);
1.5       paf       224: 
1.19      paf       225:                // header
                    226:                (*service_funcs.output_header_attribute)("content-type", "text/plain");
                    227:                char content_length_cstr[MAX_NUMBER];
                    228:                snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
                    229:                (*service_funcs.output_header_attribute)("content-length", 
                    230:                        content_length_cstr);
                    231: 
                    232:                // body
                    233:                (*service_funcs.output_body)(body, content_length);
1.2       paf       234: 
1.16      paf       235:                // unsuccessful finish
                    236:                return 1;
                    237:        }
                    238:        PEND_CATCH
1.1       paf       239: }

E-mail: