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

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.6     ! paf         6:        $Id: parser3.C,v 1.5 2001/03/14 08:50:05 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:                // TODO: ifdef WIN32 flip \\ to /
                     85:                const char *document_root="Y:/parser3/src/";
                     86:                const char *page_filespec="Y:/parser3/src/test.p";
                     87:                
                     88:                // prepare to process request
1.6     ! paf        89:                Request request(Pool(),
1.5       paf        90:                        cgi ? String::Untaint_lang::HTML_TYPO : String::Untaint_lang::NO,
                     91:                        document_root,
                     92:                        page_filespec
                     93:                        );
                     94:                
                     95:                // fill user passed forms
                     96:                fill_vform_fields(pool, cgi, request.form_class.fields());
                     97:                
                     98:                // some root-controlled location
                     99:                char *sys_auto_path1;
1.3       paf       100: #ifdef WIN32
1.5       paf       101:                sys_auto_path1=(char *)pool.malloc(MAX_STRING);
                    102:                GetWindowsDirectory(sys_auto_path1, MAX_STRING-1/*for \*/);
                    103:                strcat(sys_auto_path1, "\\");
1.3       paf       104: #else
1.5       paf       105:                sys_auto_path1=getenv("HOME");
                    106: #endif
                    107:                
                    108:                // beside by binary
                    109:                char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
1.6     ! paf       110:                strncpy(sys_auto_path2, argv[0], MAX_STRING);  // filespec of my binary
        !           111:                // strip filename
        !           112: #ifdef WIN32
        !           113:                rsplit(sys_auto_path2, '\\');  
        !           114: #else
        !           115:                rsplit(sys_auto_path2, '/'); 
        !           116: #endif
1.5       paf       117:                
                    118:                // process the request
                    119:                result=request.core(
                    120:                        sys_auto_path1, 
                    121:                        sys_auto_path2);
                    122:                // set error, will be reported in case result==0
                    123:                strcpy(error, "exception occured in request exception handler");
                    124: 
                    125:                // must be last in PTRY{}PCATCH
                    126: #ifdef WIN32
                    127:                SetUnhandledExceptionFilter(0);
1.3       paf       128: #endif
1.6     ! paf       129:        } PCATCH(e) { // global problem [creating Request, preparing to .core()]
1.5       paf       130:                result=0;
                    131:                strcpy(error, e.comment());
                    132:        }
                    133:        PEND_CATCH
                    134: 
                    135:        // write out the result 
1.3       paf       136:        if(cgi) {
1.4       paf       137:                if(result) {
                    138:                        const char *content_type="text/html";
                    139:                        printf(
                    140:                                "Content-type: %s\n"
                    141:                                "Content-length: %d\n"
                    142:                                "\n", 
1.3       paf       143:                                content_type,
                    144:                                strlen(result));
1.4       paf       145:                        stdout_write(result);
                    146:                } else {
                    147:                        printf(
                    148:                                "Content-type: text/plain\n"
                    149:                                "Content-length: %d\n"
                    150:                                "\n", 
                    151:                                strlen(error));
                    152:                        stdout_write(error);
                    153:                }
1.3       paf       154:        } else
1.4       paf       155:                if(result)
                    156:                        printf("%s", result);
                    157:                else
                    158:                        fputs(error, stderr);
1.2       paf       159: 
1.1       paf       160:        return 0;
                    161: }

E-mail: