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

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

E-mail: