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

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.7     ! paf         6:        $Id: parser3.C,v 1.6 2001/03/14 08:53:58 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);
1.7     ! paf       102:                GetWindowsDirectory(sys_auto_path1, MAX_STRING);
1.5       paf       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
1.7     ! paf       111:                rsplit(sys_auto_path2, PATH_DELIMITER_CHAR);  // strip filename
1.5       paf       112:                
                    113:                // process the request
                    114:                result=request.core(
                    115:                        sys_auto_path1, 
                    116:                        sys_auto_path2);
                    117:                // set error, will be reported in case result==0
                    118:                strcpy(error, "exception occured in request exception handler");
                    119: 
                    120:                // must be last in PTRY{}PCATCH
                    121: #ifdef WIN32
                    122:                SetUnhandledExceptionFilter(0);
1.3       paf       123: #endif
1.7     ! paf       124:        } PCATCH(e) { // global problem @globals fill @Request create @prepare to .core()
1.5       paf       125:                result=0;
                    126:                strcpy(error, e.comment());
                    127:        }
                    128:        PEND_CATCH
                    129: 
                    130:        // write out the result 
1.3       paf       131:        if(cgi) {
1.4       paf       132:                if(result) {
                    133:                        const char *content_type="text/html";
                    134:                        printf(
                    135:                                "Content-type: %s\n"
                    136:                                "Content-length: %d\n"
                    137:                                "\n", 
1.3       paf       138:                                content_type,
                    139:                                strlen(result));
1.4       paf       140:                        stdout_write(result);
                    141:                } else {
                    142:                        printf(
                    143:                                "Content-type: text/plain\n"
                    144:                                "Content-length: %d\n"
                    145:                                "\n", 
                    146:                                strlen(error));
                    147:                        stdout_write(error);
                    148:                }
1.3       paf       149:        } else
1.4       paf       150:                if(result)
                    151:                        printf("%s", result);
                    152:                else
                    153:                        fputs(error, stderr);
1.2       paf       154: 
1.1       paf       155:        return 0;
                    156: }

E-mail: