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

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.33    ! paf         8:        $Id: parser3.C,v 1.32 2001/03/22 16:38:22 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.33    ! paf        98: /// @todo intelligent cache-control
1.30      paf        99: static void send_header(const char *buf, size_t size) {
1.33    ! paf       100:        if(cgi) {
        !           101:                puts("Cache-Control: no-cache");
        !           102: 
        !           103:                // header | body  delimiter
1.20      paf       104:                puts("");
1.33    ! paf       105:        }
1.30      paf       106: }
1.20      paf       107: 
1.30      paf       108: static void send_body(const char *buf, size_t size) {
1.19      paf       109:        stdout_write(buf, size);
1.17      paf       110: }
                    111: 
1.30      paf       112: /// Service funcs 
                    113:  Service_funcs service_funcs={
                    114:                get_env,
                    115:                read_post,
                    116:                add_header_attribute,
                    117:                send_header,
                    118:                send_body
                    119:  };
                    120: 
                    121: 
1.19      paf       122: // main
                    123: 
1.5       paf       124: int main(int argc, char *argv[]) {
1.32      paf       125:        umask(2);
                    126: 
                    127: #ifdef WIN32
1.27      paf       128:        setmode(fileno(stdin), _O_BINARY);
                    129:        setmode(fileno(stdout), _O_BINARY);
                    130:        setmode(fileno(stderr), _O_BINARY);
1.32      paf       131: #endif
1.12      paf       132: 
1.3       paf       133:        // were we started as CGI?
1.20      paf       134:        cgi=
1.2       paf       135:                getenv("SERVER_SOFTWARE") || 
                    136:                getenv("SERVER_NAME") || 
                    137:                getenv("GATEWAY_INTERFACE") || 
                    138:                getenv("REQUEST_METHOD");
1.5       paf       139:        
1.10      paf       140:        if(!cgi) {
                    141:                if(argc<2) {
                    142:                        char *binary=argv[0];
1.13      paf       143:                        printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10      paf       144:                        exit(1);
                    145:                }
                    146:        }
                    147: 
1.26      paf       148:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.27      paf       149: //\#ifdef WIN32
1.26      paf       150:        fix_slashes(filespec_to_process);
1.27      paf       151: //\#endif
1.10      paf       152: 
1.5       paf       153:        PTRY { // global try
                    154:                // must be first in PTRY{}PCATCH
                    155: #ifdef WIN32
1.14      paf       156: #      if _MSC_VER
1.5       paf       157:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    158:                //TODO: initSocks();
1.8       paf       159: #      endif
1.5       paf       160: #endif
1.2       paf       161: 
1.10      paf       162:                // init global variables
1.9       paf       163:                globals_init(pool);
1.10      paf       164: 
1.13      paf       165:                if(!filespec_to_process)
                    166:                        PTHROW(0, 0,
                    167:                                0,
                    168:                                "no file to process");
                    169: 
1.10      paf       170:                // Request info
                    171:                Request::Info request_info;
1.13      paf       172:                const char *document_root=getenv("DOCUMENT_ROOT");
                    173:                if(!document_root) {
                    174:                        static char fake_document_root[MAX_STRING];
                    175:                        strncpy(fake_document_root, filespec_to_process, MAX_STRING);
                    176:                        rsplit(fake_document_root, '/');  rsplit(fake_document_root, '\\');// strip filename
                    177:                        document_root=fake_document_root;
                    178:                }
                    179:                request_info.document_root=document_root;
1.10      paf       180:                request_info.path_translated=filespec_to_process;
1.15      paf       181:                request_info.method=getenv("REQUEST_METHOD");
1.10      paf       182:                request_info.query_string=getenv("QUERY_STRING");
1.15      paf       183:                request_info.uri=getenv("REQUEST_URI");
1.10      paf       184:                request_info.content_type=getenv("CONTENT_TYPE");
                    185:                const char *content_length=getenv("CONTENT_LENGTH");
                    186:                request_info.content_length=(content_length?atoi(content_length):0);
1.21      paf       187:                request_info.cookie=getenv("HTTP_COOKIE");
1.10      paf       188: 
1.5       paf       189:                // prepare to process request
1.27      paf       190:                Pool request_pool;
                    191:                Request request(request_pool,
1.10      paf       192:                        request_info,
1.27      paf       193:                        cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5       paf       194:                        );
                    195:                
                    196:                // some root-controlled location
1.30      paf       197:                char *root_auto_path;
1.3       paf       198: #ifdef WIN32
1.10      paf       199:                // c:\windows
1.30      paf       200:                root_auto_path=(char *)pool.malloc(MAX_STRING);
                    201:                GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3       paf       202: #else
1.10      paf       203:                // ~nobody
1.30      paf       204:                root_auto_path=getenv("HOME");
1.5       paf       205: #endif
                    206:                
                    207:                // beside by binary
1.30      paf       208:                char *site_auto_path=(char *)pool.malloc(MAX_STRING);
                    209:                strncpy(site_auto_path, argv[0], MAX_STRING);  // filespec of my binary
                    210:                rsplit(site_auto_path, '/');  rsplit(site_auto_path, '\\');// strip filename
1.5       paf       211:                
                    212:                // process the request
1.16      paf       213:                request.core(pool.exception(),
1.30      paf       214:                        root_auto_path, false,
1.31      paf       215:                        site_auto_path, false,
                    216:                        strcasecmp(request_info.method, "HEAD")==0);
1.16      paf       217: 
1.22      paf       218:                // must be last in PTRY{}PCATCH
1.5       paf       219: #ifdef WIN32
1.14      paf       220: #      if _MSC_VER
1.5       paf       221:                SetUnhandledExceptionFilter(0);
1.8       paf       222: #      endif
1.25      paf       223: #endif
1.16      paf       224:                // successful finish
                    225:                return 0;
                    226:        } PCATCH(e) { // global problem 
1.19      paf       227:                const char *body=e.comment();
                    228:                int content_length=strlen(body);
1.5       paf       229: 
1.19      paf       230:                // header
                    231:                (*service_funcs.output_header_attribute)("content-type", "text/plain");
                    232:                char content_length_cstr[MAX_NUMBER];
                    233:                snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
                    234:                (*service_funcs.output_header_attribute)("content-length", 
                    235:                        content_length_cstr);
                    236: 
                    237:                // body
                    238:                (*service_funcs.output_body)(body, content_length);
1.2       paf       239: 
1.16      paf       240:                // unsuccessful finish
                    241:                return 1;
                    242:        }
                    243:        PEND_CATCH
1.1       paf       244: }

E-mail: