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

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

E-mail: