Annotation of parser3/src/include/pa_request.h, revision 1.105

1.61      paf         1: /** @file
1.62      paf         2:        Parser: request class decl.
                      3: 
1.32      paf         4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.36      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.32      paf         6: 
1.105   ! parser      7:        $Id: pa_request.h,v 1.104 2001/10/19 12:43:30 parser Exp $
1.1       paf         8: */
                      9: 
                     10: #ifndef PA_REQUEST_H
                     11: #define PA_REQUEST_H
                     12: 
1.88      parser     13: #include "pa_config_includes.h"
1.1       paf        14: #include "pa_pool.h"
1.5       paf        15: #include "pa_hash.h"
1.7       paf        16: #include "pa_wcontext.h"
1.6       paf        17: #include "pa_value.h"
1.7       paf        18: #include "pa_stack.h"
1.12      paf        19: #include "pa_vclass.h"
1.45      paf        20: #include "pa_vobject.h"
1.47      paf        21: #include "pa_venv.h"
1.48      paf        22: #include "pa_vform.h"
1.90      parser     23: #include "pa_vmath.h"
1.56      paf        24: #include "pa_vrequest.h"
1.57      paf        25: #include "pa_vresponse.h"
1.60      paf        26: #include "pa_vcookie.h"
1.77      paf        27: #include "pa_sql_driver_manager.h"
1.44      paf        28: 
1.7       paf        29: #ifndef NO_STRING_ORIGIN
1.40      paf        30: #      define COMPILE_PARAMS  \
                     31:                const char *source, \
1.46      paf        32:                VStateless_class *aclass, const String *name, \
                     33:                VStateless_class *base_class, \
1.40      paf        34:                const char *file
                     35: #      define COMPILE(source, aclass, name, base_class, file)  \
                     36:                real_compile(source, aclass, name, base_class, file)
1.7       paf        37: #else
1.40      paf        38: #      define COMPILE_PARAMS  \
                     39:                const char *source, \
1.46      paf        40:                VStateless_class *aclass, const String *name, \
                     41:                VStateless_class *base_class
1.40      paf        42: #      define COMPILE(source, aclass, name, base_class, file)  \
                     43:                real_compile(source, aclass, name, base_class)
1.7       paf        44: #endif
1.4       paf        45: 
1.39      paf        46: class Temp_lang;
1.84      paf        47: class Methoded;
1.1       paf        48: 
1.61      paf        49: /// Main workhorse.
1.7       paf        50: class Request : public Pooled {
1.39      paf        51:        friend Temp_lang;
1.1       paf        52: public:
1.61      paf        53: 
                     54:        /// some information from web server
1.65      paf        55:        class Info {
                     56:        public:
1.53      paf        57:                const char *document_root;
                     58:                const char *path_translated;
1.56      paf        59:                const char *method;
1.53      paf        60:                const char *query_string;
1.56      paf        61:                const char *uri;
1.53      paf        62:                const char *content_type;
                     63:                size_t content_length;
1.60      paf        64:                const char *cookie;
1.71      paf        65:                const char *user_agent;
1.53      paf        66:        };
                     67:        
1.50      paf        68:        Request(Pool& apool,
1.53      paf        69:                Info& ainfo,
1.63      paf        70:                String::Untaint_lang adefault_lang ///< all tainted data default untainting lang
1.50      paf        71:        );
1.3       paf        72:        ~Request() {}
1.1       paf        73: 
1.61      paf        74:        /// global classes
1.6       paf        75:        Hash& classes() { return fclasses; }
                     76: 
1.65      paf        77:        /**
                     78:                core request processing
                     79: 
                     80:                BEWARE: may throw exception to you: catch it!
                     81:        */
                     82:        void core(
1.95      parser     83:                const char *root_config_filespec, ///< system config filespec
                     84:                bool root_config_fail_on_read_problem, ///< fail if system config file not found
                     85:                const char *site_config_filespec, ///< site config filespec
                     86:                bool site_config_fail_on_read_problem, ///< fail if site config file not found
1.65      paf        87:                bool header_only);
1.17      paf        88: 
1.61      paf        89:        /// executes ops
1.89      parser     90:        void execute(const Array& ops); // execute.C
1.40      paf        91: 
1.64      paf        92:        /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.46      paf        93:        VStateless_class *use_file(
1.94      parser     94:                const String& file_name, 
                     95:                bool ignore_class_path=false, bool fail_on_read_problem=true,
1.45      paf        96:                const String *name=0, 
1.46      paf        97:                VStateless_class *base_class=0); // core.C
1.64      paf        98:        /// compiles a @a source buffer
1.46      paf        99:        VStateless_class *use_buf(
1.40      paf       100:                const char *source, const char *file,
1.46      paf       101:                VStateless_class *aclass=0, const String *name=0, 
                    102:                VStateless_class *base_class=0); // core.C
1.64      paf       103:        /// processes any code-junction there may be inside of @a value
1.40      paf       104:        Value& process(
1.33      paf       105:                Value& value, 
1.37      paf       106:                const String *name=0,
1.38      paf       107:                bool intercept_string=true); // execute.C
1.28      paf       108: 
1.61      paf       109:        /// appending, sure of clean string inside
1.65      paf       110:        void write_no_lang(const String& astring) {
1.74      paf       111:                wcontext->write(astring, String::UL_CLEAN);
1.49      paf       112:        }
1.61      paf       113:        /// appending string, passing language built into string being written
1.65      paf       114:        void write_pass_lang(const String& astring) {
                    115:                wcontext->write(astring, String::UL_PASS_APPENDED); 
1.59      paf       116:        }
1.61      paf       117:        /// appending possible string, assigning untaint language
1.40      paf       118:        void write_assign_lang(Value& avalue) {
1.39      paf       119:                wcontext->write(avalue, flang); 
1.22      paf       120:        }
1.61      paf       121:        /// appending possible string, passing language built into string being written
1.40      paf       122:        void write_pass_lang(Value& avalue) {
1.65      paf       123:                wcontext->write(avalue, String::UL_PASS_APPENDED); 
1.49      paf       124:        }
1.61      paf       125:        /// appending sure value, that would be converted to clean string
1.49      paf       126:        void write_no_lang(Value& avalue) {
1.74      paf       127:                wcontext->write(avalue, String::UL_CLEAN);
1.55      paf       128:        }
1.61      paf       129:        /// appending sure value, not VString
1.55      paf       130:        void write_expr_result(Value& avalue) {
                    131:                wcontext->write(avalue); 
1.40      paf       132:        }
1.22      paf       133: 
1.64      paf       134:        /// returns relative to @a path  path to @a file 
1.69      paf       135:        const String& relative(const char *apath, const String& relative_name);
1.61      paf       136: 
1.64      paf       137:        /// returns an absolute @a path to relative @a name
1.69      paf       138:        const String& absolute(const String& relative_name);
1.42      paf       139: 
1.80      paf       140:        /// returns the mime type of 'user_file_name_cstr'
                    141:        const String& mime_type_of(const char *user_file_name_cstr);
                    142: 
1.99      parser    143:        /// PCRE character tables
1.101     parser    144:        const unsigned char *pcre_tables();
1.99      parser    145: 
1.17      paf       146: public:
1.22      paf       147:        
1.61      paf       148:        /// info from web server
1.53      paf       149:        Info& info;
1.81      paf       150:        /// user's post data
                    151:        char *post_data;  size_t post_size;
1.53      paf       152: 
1.78      paf       153:        /// operators are methods of this class
1.84      paf       154:        Methoded& OP;
1.86      paf       155:        /// $env:fields
1.57      paf       156:        VEnv env;
1.86      paf       157:        /// $form:elements
1.57      paf       158:        VForm form;
1.90      parser    159:        /// $math:constants
                    160:        VMath math;
1.86      paf       161:        /// $request:elements
1.57      paf       162:        VRequest request;
1.86      paf       163:        /// $response:elements
1.57      paf       164:        VResponse response;
1.86      paf       165:        /// $cookie:elements
1.60      paf       166:        VCookie cookie;
1.70      paf       167: 
1.61      paf       168:        /// contexts
1.22      paf       169:        Value *self, *root, *rcontext;
1.61      paf       170:        /// contexts
1.22      paf       171:        WContext *wcontext;
1.85      paf       172: 
1.86      paf       173:        /// 'MAIN' class conglomerat
1.85      paf       174:        VStateless_class *main_class;
1.76      paf       175: 
                    176:        /// connection
1.77      paf       177:        SQL_Connection *connection;
1.87      paf       178: 
                    179:        /// classes configured data
                    180:        Hash classes_conf;
                    181: 
1.7       paf       182: private: // core data
1.6       paf       183: 
1.89      parser    184:        /// classes
1.6       paf       185:        Hash fclasses;
1.67      paf       186: 
1.89      parser    187:        /// already used files to avoid cyclic uses
1.67      paf       188:        Hash used_files;
1.6       paf       189: 
1.89      parser    190:        /// execution stack
1.7       paf       191:        Stack stack;
1.89      parser    192: 
                    193:        /**     endless execute(execute(... preventing counter 
                    194:                @see ANTI_ENDLESS_EXECUTE_RECOURSION
                    195:        */
                    196:        uint anti_endless_execute_recoursion;
1.7       paf       197: 
1.100     parser    198:        /// charset->pcre_tables
1.99      parser    199:        Hash CTYPE;     
1.105   ! parser    200: 
        !           201:        /// stack trace
        !           202:        Stack trace;
1.99      parser    203: 
1.7       paf       204: private: // compile.C
                    205: 
1.46      paf       206:        VStateless_class& real_compile(COMPILE_PARAMS);
1.7       paf       207: 
                    208: private: // execute.C
                    209: 
1.92      parser    210:        const String *execute_method(Value& aself, 
                    211:                const Method& method, bool return_cstr=true);
                    212:        const String *execute_virtual_method(Value& aself, 
                    213:                const String& method_name, bool return_cstr=true);
1.93      parser    214:        const String *execute_nonvirtual_method(VStateless_class& aclass, 
1.92      parser    215:                const String& method_name, bool return_cstr=true);
1.9       paf       216: 
                    217:        Value *get_element();
1.22      paf       218: 
                    219: private: // lang&raw 
                    220:        
1.39      paf       221:        String::Untaint_lang flang;
1.58      paf       222: 
                    223: private: // defaults
                    224: 
                    225:        const String::Untaint_lang fdefault_lang;
1.68      paf       226:        Value *default_content_type;
1.80      paf       227: 
                    228: private: // mime types
                    229: 
                    230:        /// $MAIN:MIME-TYPES
                    231:        Table *mime_types;
1.22      paf       232: 
1.39      paf       233: private: // lang manipulation
1.22      paf       234: 
1.39      paf       235:        String::Untaint_lang set_lang(String::Untaint_lang alang) {
                    236:                String::Untaint_lang result=flang;
                    237:                flang=alang;
                    238:                return result;
                    239:        }
                    240:        void restore_lang(String::Untaint_lang alang) {
                    241:                flang=alang;
                    242:        }
                    243: 
1.59      paf       244: private:
                    245: 
1.75      paf       246:        void output_result(const VFile& body_file, bool header_only);
1.39      paf       247: };
                    248: 
1.61      paf       249: ///    Auto-object used for temporary changing Request::flang.
1.39      paf       250: class Temp_lang {
                    251:        Request& frequest;
                    252:        String::Untaint_lang saved_lang;
                    253: public:
                    254:        Temp_lang(Request& arequest, String::Untaint_lang alang) : 
                    255:                frequest(arequest),
                    256:                saved_lang(arequest.set_lang(alang)) {
                    257:        }
                    258:        ~Temp_lang() { 
                    259:                frequest.restore_lang(saved_lang); 
                    260:        }
1.91      parser    261: };
                    262: 
                    263: /**
                    264:        @b method parameters passed in this array.
                    265:        contains handy typecast ad junction/not junction ensurers
                    266: 
                    267: */
                    268: class MethodParams : public Array {
                    269: public:
                    270:        MethodParams(Pool& pool, const String& amethod_name) : Array(pool),
                    271:                fmethod_name(amethod_name) {
                    272:        }
                    273: 
                    274:        /// handy typecast. I long for templates
                    275:        Value& get(int index) { 
                    276:                return *static_cast<Value *>(Array::get(index)); 
                    277:        }
                    278:        /// handy is-value-a-junction ensurer
                    279:        Value& as_junction(int index, const char *msg) { 
                    280:                return get_as(index, true, msg); 
                    281:        }
                    282:        /// handy value-is-not-a-junction ensurer
                    283:        Value& as_no_junction(int index, const char *msg) { 
                    284:                return get_as(index, false, msg); 
                    285:        }
                    286:        /// handy expression auto-processing to double
1.102     parser    287:        double as_double(int index, const char *msg, Request& r) { 
                    288:                return get_processed(index, msg, r).as_double(); 
1.91      parser    289:        }
                    290:        /// handy expression auto-processing to int
1.102     parser    291:        int as_int(int index, const char *msg, Request& r) { 
                    292:                return get_processed(index, msg, r).as_int(); 
1.103     parser    293:        }
                    294:        /// handy expression auto-processing to bool
                    295:        bool as_bool(int index, const char *msg, Request& r) { 
                    296:                return get_processed(index, msg, r).as_bool(); 
1.91      parser    297:        }
                    298:        /// handy string ensurer
                    299:        const String& as_string(int index, const char *msg) { 
                    300:                return as_no_junction(index, msg).as_string(); 
                    301:        }
                    302: 
                    303: private:
                    304: 
                    305:        /// handy value-is/not-a-junction ensurer
                    306:        Value& get_as(int index, bool as_junction, const char *msg) { 
                    307:                Value& result=get(index);
                    308:                if((result.get_junction()!=0) ^ as_junction)
1.104     parser    309:                        throw Exception(0, 0,
1.91      parser    310:                                &fmethod_name,
                    311:                                "%s (parameter #%d)", msg, 1+index);
1.104     parser    312: 
1.91      parser    313:                return result;
                    314:        }
                    315: 
1.102     parser    316:        Value& get_processed(int index, const char *msg, Request& r) {
                    317:                return r.process(as_junction(index, msg),
1.91      parser    318:                        0/*no name*/,
                    319:                        false/*don't intercept string*/);
                    320:        }
                    321: 
                    322: private:
                    323: 
                    324:        const String& fmethod_name;
                    325: 
1.4       paf       326: };
1.1       paf       327: 
                    328: #endif

E-mail: