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

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.62      paf         5: 
1.36      paf         6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.32      paf         7: 
1.91    ! parser      8:        $Id: pa_request.h,v 1.90 2001/07/06 11:13:35 parser Exp $
1.1       paf         9: */
                     10: 
                     11: #ifndef PA_REQUEST_H
                     12: #define PA_REQUEST_H
                     13: 
1.88      parser     14: #include "pa_config_includes.h"
1.1       paf        15: #include "pa_pool.h"
1.5       paf        16: #include "pa_hash.h"
1.7       paf        17: #include "pa_wcontext.h"
1.6       paf        18: #include "pa_value.h"
1.7       paf        19: #include "pa_stack.h"
1.12      paf        20: #include "pa_vclass.h"
1.45      paf        21: #include "pa_vobject.h"
1.47      paf        22: #include "pa_venv.h"
1.48      paf        23: #include "pa_vform.h"
1.90      parser     24: #include "pa_vmath.h"
1.56      paf        25: #include "pa_vrequest.h"
1.57      paf        26: #include "pa_vresponse.h"
1.60      paf        27: #include "pa_vcookie.h"
1.77      paf        28: #include "pa_sql_driver_manager.h"
1.44      paf        29: 
1.7       paf        30: #ifndef NO_STRING_ORIGIN
1.40      paf        31: #      define COMPILE_PARAMS  \
                     32:                const char *source, \
1.46      paf        33:                VStateless_class *aclass, const String *name, \
                     34:                VStateless_class *base_class, \
1.40      paf        35:                const char *file
                     36: #      define COMPILE(source, aclass, name, base_class, file)  \
                     37:                real_compile(source, aclass, name, base_class, file)
1.7       paf        38: #else
1.40      paf        39: #      define COMPILE_PARAMS  \
                     40:                const char *source, \
1.46      paf        41:                VStateless_class *aclass, const String *name, \
                     42:                VStateless_class *base_class
1.40      paf        43: #      define COMPILE(source, aclass, name, base_class, file)  \
                     44:                real_compile(source, aclass, name, base_class)
1.7       paf        45: #endif
1.4       paf        46: 
1.39      paf        47: class Temp_lang;
1.84      paf        48: class Methoded;
1.1       paf        49: 
1.61      paf        50: /// Main workhorse.
1.7       paf        51: class Request : public Pooled {
1.39      paf        52:        friend Temp_lang;
1.1       paf        53: public:
1.61      paf        54: 
                     55:        /// some information from web server
1.65      paf        56:        class Info {
                     57:        public:
1.53      paf        58:                const char *document_root;
                     59:                const char *path_translated;
1.56      paf        60:                const char *method;
1.53      paf        61:                const char *query_string;
1.56      paf        62:                const char *uri;
1.53      paf        63:                const char *content_type;
                     64:                size_t content_length;
1.60      paf        65:                const char *cookie;
1.71      paf        66:                const char *user_agent;
1.53      paf        67:        };
                     68:        
1.50      paf        69:        Request(Pool& apool,
1.53      paf        70:                Info& ainfo,
1.63      paf        71:                String::Untaint_lang adefault_lang ///< all tainted data default untainting lang
1.50      paf        72:        );
1.3       paf        73:        ~Request() {}
1.1       paf        74: 
1.61      paf        75:        /// global classes
1.6       paf        76:        Hash& classes() { return fclasses; }
                     77: 
1.65      paf        78:        /**
                     79:                core request processing
                     80: 
                     81:                BEWARE: may throw exception to you: catch it!
                     82:        */
                     83:        void core(
1.66      paf        84:                const char *root_auto_path, ///< path to system auto.p file
                     85:                bool root_auto_fail, ///< fail if system auto.p file not found
                     86:                const char *site_auto_path, ///< path to site auto.p file
                     87:                bool site_auto_fail, ///< fail if site auto.p file not found
1.65      paf        88:                bool header_only);
1.17      paf        89: 
1.61      paf        90:        /// executes ops
1.89      parser     91:        void execute(const Array& ops); // execute.C
1.40      paf        92: 
1.64      paf        93:        /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.46      paf        94:        VStateless_class *use_file(
1.69      paf        95:                const String& file_spec, 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.17      paf       143: public:
1.22      paf       144:        
1.61      paf       145:        /// info from web server
1.53      paf       146:        Info& info;
1.81      paf       147:        /// user's post data
                    148:        char *post_data;  size_t post_size;
1.53      paf       149: 
1.78      paf       150:        /// operators are methods of this class
1.84      paf       151:        Methoded& OP;
1.86      paf       152:        /// $env:fields
1.57      paf       153:        VEnv env;
1.86      paf       154:        /// $form:elements
1.57      paf       155:        VForm form;
1.90      parser    156:        /// $math:constants
                    157:        VMath math;
1.86      paf       158:        /// $request:elements
1.57      paf       159:        VRequest request;
1.86      paf       160:        /// $response:elements
1.57      paf       161:        VResponse response;
1.86      paf       162:        /// $cookie:elements
1.60      paf       163:        VCookie cookie;
1.70      paf       164: 
1.61      paf       165:        /// contexts
1.22      paf       166:        Value *self, *root, *rcontext;
1.61      paf       167:        /// contexts
1.22      paf       168:        WContext *wcontext;
1.85      paf       169: 
1.86      paf       170:        /// 'MAIN' class conglomerat
1.85      paf       171:        VStateless_class *main_class;
1.76      paf       172: 
                    173:        /// connection
1.77      paf       174:        SQL_Connection *connection;
1.83      paf       175:        /// PCRE character tables
                    176:        unsigned char *pcre_tables;
1.87      paf       177: 
                    178:        /// classes configured data
                    179:        Hash classes_conf;
                    180: 
1.6       paf       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: 
                    198: private: // compile.C
                    199: 
1.46      paf       200:        VStateless_class& real_compile(COMPILE_PARAMS);
1.7       paf       201: 
                    202: private: // execute.C
                    203: 
1.57      paf       204:        const String *execute_method(Value& aself, const Method& method, 
                    205:                bool return_cstr=true);
                    206:        const String *execute_method(Value& aself, const String& method_name, 
                    207:                bool return_cstr=true);
1.9       paf       208: 
                    209:        Value *get_element();
1.22      paf       210: 
                    211: private: // lang&raw 
                    212:        
1.39      paf       213:        String::Untaint_lang flang;
1.58      paf       214: 
                    215: private: // defaults
                    216: 
                    217:        const String::Untaint_lang fdefault_lang;
1.68      paf       218:        Value *default_content_type;
1.80      paf       219: 
                    220: private: // mime types
                    221: 
                    222:        /// $MAIN:MIME-TYPES
                    223:        Table *mime_types;
1.22      paf       224: 
1.39      paf       225: private: // lang manipulation
1.22      paf       226: 
1.39      paf       227:        String::Untaint_lang set_lang(String::Untaint_lang alang) {
                    228:                String::Untaint_lang result=flang;
                    229:                flang=alang;
                    230:                return result;
                    231:        }
                    232:        void restore_lang(String::Untaint_lang alang) {
                    233:                flang=alang;
                    234:        }
                    235: 
1.59      paf       236: private:
                    237: 
1.75      paf       238:        void output_result(const VFile& body_file, bool header_only);
1.39      paf       239: };
                    240: 
1.61      paf       241: ///    Auto-object used for temporary changing Request::flang.
1.39      paf       242: class Temp_lang {
                    243:        Request& frequest;
                    244:        String::Untaint_lang saved_lang;
                    245: public:
                    246:        Temp_lang(Request& arequest, String::Untaint_lang alang) : 
                    247:                frequest(arequest),
                    248:                saved_lang(arequest.set_lang(alang)) {
                    249:        }
                    250:        ~Temp_lang() { 
                    251:                frequest.restore_lang(saved_lang); 
                    252:        }
1.91    ! parser    253: };
        !           254: 
        !           255: /**
        !           256:        @b method parameters passed in this array.
        !           257:        contains handy typecast ad junction/not junction ensurers
        !           258: 
        !           259: */
        !           260: class MethodParams : public Array {
        !           261: public:
        !           262:        MethodParams(Pool& pool, const String& amethod_name) : Array(pool),
        !           263:                fmethod_name(amethod_name) {
        !           264:        }
        !           265: 
        !           266:        /// handy typecast. I long for templates
        !           267:        Value& get(int index) { 
        !           268:                return *static_cast<Value *>(Array::get(index)); 
        !           269:        }
        !           270:        /// handy is-value-a-junction ensurer
        !           271:        Value& as_junction(int index, const char *msg) { 
        !           272:                return get_as(index, true, msg); 
        !           273:        }
        !           274:        /// handy value-is-not-a-junction ensurer
        !           275:        Value& as_no_junction(int index, const char *msg) { 
        !           276:                return get_as(index, false, msg); 
        !           277:        }
        !           278:        /// handy expression auto-processing to double
        !           279:        double as_double(int index, Request& r) { 
        !           280:                return get_processed(index, r).as_double(); 
        !           281:        }
        !           282:        /// handy expression auto-processing to int
        !           283:        int as_int(int index, Request& r) { 
        !           284:                return get_processed(index, r).as_int(); 
        !           285:        }
        !           286:        /// handy string ensurer
        !           287:        const String& as_string(int index, const char *msg) { 
        !           288:                return as_no_junction(index, msg).as_string(); 
        !           289:        }
        !           290: 
        !           291: private:
        !           292: 
        !           293:        /// handy value-is/not-a-junction ensurer
        !           294:        Value& get_as(int index, bool as_junction, const char *msg) { 
        !           295:                Value& result=get(index);
        !           296:                if((result.get_junction()!=0) ^ as_junction)
        !           297:                        THROW(0, 0,
        !           298:                                &fmethod_name,
        !           299:                                "%s (parameter #%d)", msg, 1+index);
        !           300:                return result;
        !           301:        }
        !           302: 
        !           303:        Value& get_processed(int index, Request& r) {
        !           304:                return r.process(get(index),
        !           305:                        0/*no name*/,
        !           306:                        false/*don't intercept string*/);
        !           307:        }
        !           308: 
        !           309: private:
        !           310: 
        !           311:        const String& fmethod_name;
        !           312: 
1.4       paf       313: };
1.1       paf       314: 
                    315: #endif

E-mail: