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

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.5     ! paf         6:        $Id: parser3.C,v 1.4 2001/03/13 19:35:07 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.5     ! paf        30: // intercept global system errors
        !            31: LONG WINAPI TopLevelExceptionFilter (
        !            32:                                                                         struct _EXCEPTION_POINTERS *ExceptionInfo
        !            33:                                                                         ) {
        !            34:        char buf[MAX_STRING];
        !            35:        if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
        !            36:                struct _EXCEPTION_RECORD *r=ExceptionInfo->ExceptionRecord;
        !            37:                
        !            38:                int printed=0;
        !            39:                printed+=snprintf(buf+printed, MAX_STRING-printed, "Exception 0x%X at 0x%p", 
        !            40:                        r->ExceptionCode, 
        !            41:                        r->ExceptionAddress);
        !            42:                for(unsigned int i=0; i<r->NumberParameters; i++)
        !            43:                        printed+=snprintf(buf+printed, MAX_STRING-printed, ", 0x%X", 
        !            44:                                r->ExceptionInformation[i]);
        !            45:        } else 
        !            46:                strcpy(buf, "Exception <unknown>");
        !            47:        
        !            48:        PTHROW(0, 0,
        !            49:                0,
        !            50:                buf);
        !            51: 
        !            52:        return EXCEPTION_EXECUTE_HANDLER; // never reached
        !            53: }
1.3       paf        54: #endif
                     55: 
1.5     ! paf        56: void fill_vform_fields(Pool& pool, bool cgi, Hash& fields) {
        !            57:        String& ename=*new(pool) String(pool);
        !            58:        ename.APPEND_CONST("test");
1.3       paf        59: 
1.5     ! paf        60:        String& evalue=*new(pool) String(pool);
        !            61:        evalue.APPEND_TAINTED("<value>", 0, "form", 0);
1.3       paf        62: 
1.5     ! paf        63:        fields.put(ename, new(pool) VString(evalue));
        !            64: }
1.2       paf        65: 
1.5     ! paf        66: int main(int argc, char *argv[]) {
1.3       paf        67:        // were we started as CGI?
1.2       paf        68:        bool cgi=
                     69:                getenv("SERVER_SOFTWARE") || 
                     70:                getenv("SERVER_NAME") || 
                     71:                getenv("GATEWAY_INTERFACE") || 
                     72:                getenv("REQUEST_METHOD");
1.5     ! paf        73:        
        !            74:        char *result;  char error[MAX_STRING];  error[0]=0;
        !            75:        PTRY { // global try
        !            76:                // must be first in PTRY{}PCATCH
        !            77: #ifdef WIN32
        !            78:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
        !            79:                //TODO: initSocks();
        !            80: #endif
1.2       paf        81: 
1.5     ! paf        82:                fill_globals(pool);
        !            83:                
        !            84:                Pool request_pool; // request pool
        !            85:                // TODO: ifdef WIN32 flip \\ to /
        !            86:                const char *document_root="Y:/parser3/src/";
        !            87:                const char *page_filespec="Y:/parser3/src/test.p";
        !            88:                
        !            89:                // prepare to process request
        !            90:                Request request(request_pool,
        !            91:                        cgi ? String::Untaint_lang::HTML_TYPO : String::Untaint_lang::NO,
        !            92:                        document_root,
        !            93:                        page_filespec
        !            94:                        );
        !            95:                
        !            96:                // fill user passed forms
        !            97:                fill_vform_fields(pool, cgi, request.form_class.fields());
        !            98:                
        !            99:                // some root-controlled location
        !           100:                char *sys_auto_path1;
1.3       paf       101: #ifdef WIN32
1.5     ! paf       102:                sys_auto_path1=(char *)pool.malloc(MAX_STRING);
        !           103:                GetWindowsDirectory(sys_auto_path1, MAX_STRING-1/*for \*/);
        !           104:                strcat(sys_auto_path1, "\\");
1.3       paf       105: #else
1.5     ! paf       106:                sys_auto_path1=getenv("HOME");
        !           107: #endif
        !           108:                
        !           109:                // beside by binary
        !           110:                char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
        !           111:                strncpy(sys_auto_path2, argv[0], MAX_STRING-20);  // filespec of my binary
        !           112:                rsplit(sys_auto_path2, '\\');  rsplit(sys_auto_path2, '/'); // strip filename
        !           113:                
        !           114:                // process the request
        !           115:                result=request.core(
        !           116:                        sys_auto_path1, 
        !           117:                        sys_auto_path2);
        !           118:                // set error, will be reported in case result==0
        !           119:                strcpy(error, "exception occured in request exception handler");
        !           120: 
        !           121:                // must be last in PTRY{}PCATCH
        !           122: #ifdef WIN32
        !           123:                SetUnhandledExceptionFilter(0);
1.3       paf       124: #endif
1.5     ! paf       125:        } PCATCH(e) { // global problem, such as out of memory when creating Request
        !           126:                result=0;
        !           127:                strcpy(error, e.comment());
        !           128:        }
        !           129:        PEND_CATCH
        !           130: 
        !           131:        // write out the result 
1.3       paf       132:        if(cgi) {
1.4       paf       133:                if(result) {
                    134:                        const char *content_type="text/html";
                    135:                        printf(
                    136:                                "Content-type: %s\n"
                    137:                                "Content-length: %d\n"
                    138:                                "\n", 
1.3       paf       139:                                content_type,
                    140:                                strlen(result));
1.4       paf       141:                        stdout_write(result);
                    142:                } else {
                    143:                        printf(
                    144:                                "Content-type: text/plain\n"
                    145:                                "Content-length: %d\n"
                    146:                                "\n", 
                    147:                                strlen(error));
                    148:                        stdout_write(error);
                    149:                }
1.3       paf       150:        } else
1.4       paf       151:                if(result)
                    152:                        printf("%s", result);
                    153:                else
                    154:                        fputs(error, stderr);
1.2       paf       155: 
1.1       paf       156:        return 0;
                    157: }

E-mail: