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

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.30    ! paf         8:        $Id: parser3.C,v 1.29 2001/03/21 16:59:07 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) {
                     48:                struct _EXCEPTION_RECORD *r=ExceptionInfo->ExceptionRecord;
                     49:                
                     50:                int printed=0;
                     51:                printed+=snprintf(buf+printed, MAX_STRING-printed, "Exception 0x%X at 0x%p", 
                     52:                        r->ExceptionCode, 
                     53:                        r->ExceptionAddress);
                     54:                for(unsigned int i=0; i<r->NumberParameters; i++)
                     55:                        printed+=snprintf(buf+printed, MAX_STRING-printed, ", 0x%X", 
                     56:                                r->ExceptionInformation[i]);
                     57:        } else 
                     58:                strcpy(buf, "Exception <unknown>");
                     59:        
                     60:        PTHROW(0, 0,
                     61:                0,
                     62:                buf);
                     63: 
                     64:        return EXCEPTION_EXECUTE_HANDLER; // never reached
                     65: }
1.8       paf        66: #      endif
1.26      paf        67: 
1.27      paf        68: #endif
                     69: 
                     70: //\if
1.30    ! paf        71: static void fix_slashes(char *s) {
1.27      paf        72:        if(s)
                     73:                for(; *s; s++)
                     74:                        if(*s=='\\')
                     75:                                *s='/';
1.26      paf        76: }
1.27      paf        77: //\endif
1.3       paf        78: 
1.19      paf        79: // service funcs
                     80: 
1.30    ! paf        81: static const char *get_env(Pool& pool, const char *name) {
1.28      paf        82:        return getenv(name);
                     83: }
                     84: 
1.30    ! paf        85: static uint read_post(char *buf, uint max_bytes) {
1.12      paf        86:        int read_size=0;
                     87:        do {
                     88:                int chunk_size=read
                     89:                        (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size));
                     90:                if(chunk_size<0)
                     91:                        break;
                     92:                read_size+=chunk_size;
                     93:        } while(read_size<max_bytes);
                     94: 
                     95:        return read_size;
1.10      paf        96: }
                     97: 
1.30    ! paf        98: static void add_header_attribute(const char *key, const char *value) {
1.20      paf        99:        if(cgi)
                    100:                printf("%s: %s\n", key, value);
1.19      paf       101: }
                    102: 
1.30    ! paf       103: static void send_header(const char *buf, size_t size) {
1.20      paf       104:        if(cgi) // header | body  delimiter
                    105:                puts("");
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.23      paf       125:        // TODO:umask(2);
1.27      paf       126: //\#ifdef WIN32
                    127:        setmode(fileno(stdin), _O_BINARY);
                    128:        setmode(fileno(stdout), _O_BINARY);
                    129:        setmode(fileno(stderr), _O_BINARY);
                    130: //\#endif
1.12      paf       131: 
1.3       paf       132:        // were we started as CGI?
1.20      paf       133:        cgi=
1.2       paf       134:                getenv("SERVER_SOFTWARE") || 
                    135:                getenv("SERVER_NAME") || 
                    136:                getenv("GATEWAY_INTERFACE") || 
                    137:                getenv("REQUEST_METHOD");
1.5       paf       138:        
1.10      paf       139:        if(!cgi) {
                    140:                if(argc<2) {
                    141:                        char *binary=argv[0];
1.13      paf       142:                        printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10      paf       143:                        exit(1);
                    144:                }
                    145:        }
                    146: 
1.26      paf       147:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.27      paf       148: //\#ifdef WIN32
1.26      paf       149:        fix_slashes(filespec_to_process);
1.27      paf       150: //\#endif
1.10      paf       151: 
1.5       paf       152:        PTRY { // global try
                    153:                // must be first in PTRY{}PCATCH
                    154: #ifdef WIN32
1.14      paf       155: #      if _MSC_VER
1.5       paf       156:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    157:                //TODO: initSocks();
1.8       paf       158: #      endif
1.5       paf       159: #endif
1.2       paf       160: 
1.10      paf       161:                // init global variables
1.9       paf       162:                globals_init(pool);
1.10      paf       163: 
1.13      paf       164:                if(!filespec_to_process)
                    165:                        PTHROW(0, 0,
                    166:                                0,
                    167:                                "no file to process");
                    168: 
1.10      paf       169:                // Request info
                    170:                Request::Info request_info;
1.13      paf       171:                const char *document_root=getenv("DOCUMENT_ROOT");
                    172:                if(!document_root) {
                    173:                        static char fake_document_root[MAX_STRING];
                    174:                        strncpy(fake_document_root, filespec_to_process, MAX_STRING);
                    175:                        rsplit(fake_document_root, '/');  rsplit(fake_document_root, '\\');// strip filename
                    176:                        document_root=fake_document_root;
                    177:                }
                    178:                request_info.document_root=document_root;
1.10      paf       179:                request_info.path_translated=filespec_to_process;
1.15      paf       180:                request_info.method=getenv("REQUEST_METHOD");
1.10      paf       181:                request_info.query_string=getenv("QUERY_STRING");
1.15      paf       182:                request_info.uri=getenv("REQUEST_URI");
1.10      paf       183:                request_info.content_type=getenv("CONTENT_TYPE");
                    184:                const char *content_length=getenv("CONTENT_LENGTH");
                    185:                request_info.content_length=(content_length?atoi(content_length):0);
1.21      paf       186:                request_info.cookie=getenv("HTTP_COOKIE");
1.10      paf       187: 
1.5       paf       188:                // prepare to process request
1.27      paf       189:                Pool request_pool;
                    190:                Request request(request_pool,
1.10      paf       191:                        request_info,
1.27      paf       192:                        cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5       paf       193:                        );
                    194:                
                    195:                // some root-controlled location
1.30    ! paf       196:                char *root_auto_path;
1.3       paf       197: #ifdef WIN32
1.10      paf       198:                // c:\windows
1.30    ! paf       199:                root_auto_path=(char *)pool.malloc(MAX_STRING);
        !           200:                GetWindowsDirectory(root_auto_path, MAX_STRING);
        !           201:                strcat(root_auto_path, "/");
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
        !           211:                strcat(site_auto_path, "/");
1.5       paf       212:                
                    213:                // process the request
1.16      paf       214:                request.core(pool.exception(),
1.30    ! paf       215:                        root_auto_path, false,
        !           216:                        site_auto_path, false);
1.19      paf       217:                // no actions with request' data past this point
                    218:                // request.exception not not handled here, but all
                    219:                // request' data are associated with it's pool=exception
1.16      paf       220: 
1.22      paf       221:                // must be last in PTRY{}PCATCH
1.5       paf       222: #ifdef WIN32
1.14      paf       223: #      if _MSC_VER
1.5       paf       224:                SetUnhandledExceptionFilter(0);
1.8       paf       225: #      endif
1.25      paf       226: #endif
1.16      paf       227:                // successful finish
                    228:                return 0;
                    229:        } PCATCH(e) { // global problem 
1.19      paf       230:                const char *body=e.comment();
                    231:                int content_length=strlen(body);
1.5       paf       232: 
1.19      paf       233:                // header
                    234:                (*service_funcs.output_header_attribute)("content-type", "text/plain");
                    235:                char content_length_cstr[MAX_NUMBER];
                    236:                snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
                    237:                (*service_funcs.output_header_attribute)("content-length", 
                    238:                        content_length_cstr);
                    239: 
                    240:                // body
                    241:                (*service_funcs.output_body)(body, content_length);
1.2       paf       242: 
1.16      paf       243:                // unsuccessful finish
                    244:                return 1;
                    245:        }
                    246:        PEND_CATCH
1.1       paf       247: }

E-mail: