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

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

E-mail: