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

1.61      paf         1: /** @file
1.62      paf         2:        Parser: request class decl.
                      3: 
1.120     paf         4:        Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.121     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.32      paf         6: 
1.128   ! paf         7:        $Id: pa_request.h,v 1.127 2002/04/11 13:33:39 paf Exp $
1.1       paf         8: */
                      9: 
                     10: #ifndef PA_REQUEST_H
                     11: #define PA_REQUEST_H
                     12: 
                     13: #include "pa_pool.h"
1.5       paf        14: #include "pa_hash.h"
1.7       paf        15: #include "pa_wcontext.h"
1.6       paf        16: #include "pa_value.h"
1.7       paf        17: #include "pa_stack.h"
1.12      paf        18: #include "pa_vclass.h"
1.45      paf        19: #include "pa_vobject.h"
1.47      paf        20: #include "pa_venv.h"
1.108     paf        21: #include "pa_vstatus.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.112     paf        29: #ifdef RESOURCES_DEBUG
                     30: #include <sys/resource.h>
                     31: #endif
                     32: 
1.128   ! paf        33: // consts
        !            34: 
        !            35: const uint ANTI_ENDLESS_EXECUTE_RECOURSION=500;
        !            36: 
        !            37: // defines
        !            38: 
1.7       paf        39: #ifndef NO_STRING_ORIGIN
1.40      paf        40: #      define COMPILE_PARAMS  \
                     41:                const char *source, \
1.46      paf        42:                VStateless_class *aclass, const String *name, \
                     43:                VStateless_class *base_class, \
1.40      paf        44:                const char *file
                     45: #      define COMPILE(source, aclass, name, base_class, file)  \
                     46:                real_compile(source, aclass, name, base_class, file)
1.7       paf        47: #else
1.40      paf        48: #      define COMPILE_PARAMS  \
                     49:                const char *source, \
1.46      paf        50:                VStateless_class *aclass, const String *name, \
                     51:                VStateless_class *base_class
1.40      paf        52: #      define COMPILE(source, aclass, name, base_class, file)  \
                     53:                real_compile(source, aclass, name, base_class)
1.7       paf        54: #endif
1.4       paf        55: 
1.39      paf        56: class Temp_lang;
1.84      paf        57: class Methoded;
1.116     paf        58: class VMethodFrame;
1.1       paf        59: 
1.61      paf        60: /// Main workhorse.
1.7       paf        61: class Request : public Pooled {
1.107     paf        62:        friend class Temp_lang;
1.117     paf        63:        friend class Temp_connection;
1.1       paf        64: public:
1.112     paf        65: 
                     66: #ifdef RESOURCES_DEBUG
                     67:        /// measures
                     68:        double sql_connect_time;
                     69:        double sql_request_time;
                     70: #endif 
1.61      paf        71: 
                     72:        /// some information from web server
1.65      paf        73:        class Info {
                     74:        public:
1.53      paf        75:                const char *document_root;
                     76:                const char *path_translated;
1.56      paf        77:                const char *method;
1.53      paf        78:                const char *query_string;
1.56      paf        79:                const char *uri;
1.53      paf        80:                const char *content_type;
                     81:                size_t content_length;
1.60      paf        82:                const char *cookie;
1.71      paf        83:                const char *user_agent;
1.53      paf        84:        };
                     85:        
1.50      paf        86:        Request(Pool& apool,
1.53      paf        87:                Info& ainfo,
1.111     paf        88:                uchar adefault_lang, ///< all tainted data default untainting lang
1.110     paf        89:                bool status_allowed ///<  status class allowed
1.50      paf        90:        );
1.118     paf        91:        ~Request();
1.1       paf        92: 
1.61      paf        93:        /// global classes
1.6       paf        94:        Hash& classes() { return fclasses; }
                     95: 
1.65      paf        96:        /**
                     97:                core request processing
                     98: 
                     99:                BEWARE: may throw exception to you: catch it!
                    100:        */
                    101:        void core(
1.95      parser    102:                const char *root_config_filespec, ///< system config filespec
                    103:                bool root_config_fail_on_read_problem, ///< fail if system config file not found
                    104:                const char *site_config_filespec, ///< site config filespec
                    105:                bool site_config_fail_on_read_problem, ///< fail if site config file not found
1.65      paf       106:                bool header_only);
1.17      paf       107: 
1.61      paf       108:        /// executes ops
1.89      parser    109:        void execute(const Array& ops); // execute.C
1.127     paf       110:        /// execute ops with anti-recoursion check
1.128   ! paf       111:        void recoursion_checked_execute(const String *name, const Array& ops) {
        !           112:                // anti_endless_execute_recoursion
        !           113:                if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
        !           114:                        anti_endless_execute_recoursion=0; // give @exception a chance
        !           115:                        throw Exception("parser.runtime",
        !           116:                                name,
        !           117:                                "call canceled - endless recursion detected");
        !           118:                }
        !           119:                execute(ops); // execute it
        !           120:                anti_endless_execute_recoursion--;
        !           121:        }
1.40      paf       122: 
1.64      paf       123:        /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.46      paf       124:        VStateless_class *use_file(
1.94      parser    125:                const String& file_name, 
                    126:                bool ignore_class_path=false, bool fail_on_read_problem=true,
1.45      paf       127:                const String *name=0, 
1.46      paf       128:                VStateless_class *base_class=0); // core.C
1.64      paf       129:        /// compiles a @a source buffer
1.46      paf       130:        VStateless_class *use_buf(
1.40      paf       131:                const char *source, const char *file,
1.46      paf       132:                VStateless_class *aclass=0, const String *name=0, 
                    133:                VStateless_class *base_class=0); // core.C
1.28      paf       134: 
1.126     paf       135:        //@{ convinient inline helpers @see Request::process
1.128   ! paf       136:        void process_to_nothing(Value& input_value, 
        !           137:                void (*postexecute)(void *info)=0, void *postexecute_info=0) {
        !           138:                const String *junk_string;
        !           139:                process_internal(input_value, 0/*result_name*/, true/*intercept_string*/, &junk_string, 0,
        !           140:                        postexecute, postexecute_info);
        !           141:        }
1.126     paf       142:        const String& process_to_string(Value& input_value, 
                    143:                const String *result_name=0) {
                    144:                const String *result;
                    145:                process_internal(input_value, result_name, true/*intercept_string*/, &result, 0);
                    146:                return *result;
                    147:        }
                    148:        Value& process_to_value(Value& input_value, 
                    149:                const String *result_name=0, 
                    150:                bool intercept_string=true) {
                    151:                Value *result;
                    152:                process_internal(input_value, result_name, intercept_string, 0, &result);
                    153:                return *result;
                    154:        }
                    155:        //@}
                    156:        
1.61      paf       157:        /// appending, sure of clean string inside
1.65      paf       158:        void write_no_lang(const String& astring) {
1.111     paf       159:                wcontext->write(astring, 
                    160:                        String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.49      paf       161:        }
1.61      paf       162:        /// appending string, passing language built into string being written
1.65      paf       163:        void write_pass_lang(const String& astring) {
                    164:                wcontext->write(astring, String::UL_PASS_APPENDED); 
1.59      paf       165:        }
1.61      paf       166:        /// appending possible string, assigning untaint language
1.40      paf       167:        void write_assign_lang(Value& avalue) {
1.39      paf       168:                wcontext->write(avalue, flang); 
1.106     parser    169:        }
                    170:        /// appending string, assigning untaint language
                    171:        void write_assign_lang(const String& astring) {
                    172:                wcontext->write(astring, flang); 
1.22      paf       173:        }
1.61      paf       174:        /// appending possible string, passing language built into string being written
1.40      paf       175:        void write_pass_lang(Value& avalue) {
1.65      paf       176:                wcontext->write(avalue, String::UL_PASS_APPENDED); 
1.49      paf       177:        }
1.61      paf       178:        /// appending sure value, that would be converted to clean string
1.49      paf       179:        void write_no_lang(Value& avalue) {
1.122     paf       180:                if(wcontext->get_in_expression())
                    181:                        wcontext->write(avalue);
                    182:                else
                    183:                        wcontext->write(avalue, 
                    184:                                String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.55      paf       185:        }
1.61      paf       186:        /// appending sure value, not VString
1.55      paf       187:        void write_expr_result(Value& avalue) {
                    188:                wcontext->write(avalue); 
1.40      paf       189:        }
1.22      paf       190: 
1.64      paf       191:        /// returns relative to @a path  path to @a file 
1.69      paf       192:        const String& relative(const char *apath, const String& relative_name);
1.61      paf       193: 
1.64      paf       194:        /// returns an absolute @a path to relative @a name
1.69      paf       195:        const String& absolute(const String& relative_name);
1.42      paf       196: 
1.80      paf       197:        /// returns the mime type of 'user_file_name_cstr'
                    198:        const String& mime_type_of(const char *user_file_name_cstr);
                    199: 
1.117     paf       200:        /// returns current SQL connection if any
                    201:        SQL_Connection *connection(const String *source) { 
                    202:                if(!fconnection && source)
1.125     paf       203:                        throw Exception("parser.runtime",
1.117     paf       204:                                source,
                    205:                                "outside of 'connect' operator");
                    206: 
                    207:                return fconnection; 
                    208:        }
                    209: 
1.17      paf       210: public:
1.22      paf       211:        
1.61      paf       212:        /// info from web server
1.53      paf       213:        Info& info;
1.81      paf       214:        /// user's post data
                    215:        char *post_data;  size_t post_size;
1.53      paf       216: 
1.78      paf       217:        /// operators are methods of this class
1.84      paf       218:        Methoded& OP;
1.86      paf       219:        /// $env:fields
1.57      paf       220:        VEnv env;
1.108     paf       221:        /// $status:fields
                    222:        VStatus status;
1.86      paf       223:        /// $form:elements
1.57      paf       224:        VForm form;
1.90      parser    225:        /// $math:constants
                    226:        VMath math;
1.86      paf       227:        /// $request:elements
1.57      paf       228:        VRequest request;
1.86      paf       229:        /// $response:elements
1.57      paf       230:        VResponse response;
1.86      paf       231:        /// $cookie:elements
1.60      paf       232:        VCookie cookie;
1.70      paf       233: 
1.123     paf       234:        //@{ request processing status
                    235:        /// execution stack
                    236:        Stack stack;
1.61      paf       237:        /// contexts
1.22      paf       238:        Value *self, *root, *rcontext;
1.61      paf       239:        /// contexts
1.22      paf       240:        WContext *wcontext;
1.124     paf       241:        /// exception stack trace
                    242:        Stack exception_trace;
1.123     paf       243:        //@}
1.85      paf       244: 
1.86      paf       245:        /// 'MAIN' class conglomerat
1.85      paf       246:        VStateless_class *main_class;
1.76      paf       247: 
1.87      paf       248:        /// classes configured data
                    249:        Hash classes_conf;
                    250: 
1.7       paf       251: private: // core data
1.6       paf       252: 
1.89      parser    253:        /// classes
1.6       paf       254:        Hash fclasses;
1.67      paf       255: 
1.89      parser    256:        /// already used files to avoid cyclic uses
1.67      paf       257:        Hash used_files;
1.89      parser    258: 
                    259:        /**     endless execute(execute(... preventing counter 
                    260:                @see ANTI_ENDLESS_EXECUTE_RECOURSION
                    261:        */
                    262:        uint anti_endless_execute_recoursion;
1.99      parser    263: 
1.7       paf       264: private: // compile.C
                    265: 
1.46      paf       266:        VStateless_class& real_compile(COMPILE_PARAMS);
1.7       paf       267: 
                    268: private: // execute.C
                    269: 
1.116     paf       270:        const String *execute_method(Value& aself, const Method& method,
                    271:                bool return_cstr);
                    272:        const String& execute_method(VMethodFrame& amethodFrame, const Method& method);
                    273:        const String *execute_virtual_method(Value& aself, const String& method_name);
1.93      parser    274:        const String *execute_nonvirtual_method(VStateless_class& aclass, 
1.116     paf       275:                const String& method_name,
                    276:                bool return_cstr);
1.9       paf       277: 
1.119     paf       278:        Value *get_element(bool can_call_operator);
1.22      paf       279: 
1.58      paf       280: private: // defaults
                    281: 
1.111     paf       282:        const uchar fdefault_lang;
1.68      paf       283:        Value *default_content_type;
1.80      paf       284: 
                    285: private: // mime types
                    286: 
                    287:        /// $MAIN:MIME-TYPES
                    288:        Table *mime_types;
1.22      paf       289: 
1.39      paf       290: private: // lang manipulation
1.22      paf       291: 
1.111     paf       292:        uchar set_lang(uchar alang) {
                    293:                uchar result=flang;
1.39      paf       294:                flang=alang;
                    295:                return result;
                    296:        }
1.111     paf       297:        void restore_lang(uchar alang) {
1.39      paf       298:                flang=alang;
                    299:        }
                    300: 
1.117     paf       301: private: // lang&raw 
                    302:        
                    303:        uchar flang;
                    304: 
                    305: 
                    306: private: // connection manipulation
                    307: 
                    308:        SQL_Connection *set_connection(SQL_Connection *aconnection) {
                    309:                SQL_Connection *result=fconnection;
                    310:                fconnection=aconnection;
                    311:                return result;
                    312:        }
                    313:        void restore_connection(SQL_Connection *aconnection) {
                    314:                fconnection=aconnection;
                    315:        }
                    316: 
                    317: private:
                    318: 
                    319:        /// connection
                    320:        SQL_Connection *fconnection;
                    321: 
                    322: 
1.59      paf       323: private:
                    324: 
1.126     paf       325:        /// processes any code-junction there may be inside of @a value
                    326:        void process_internal(
                    327:                Value& input_value, const String *result_name, 
                    328:                bool intercept_string,
1.128   ! paf       329:                const String **string_result, Value **value_result,
        !           330:                void (*postexecute)(void *info)=0, void *postexecute_info=0); // execute.C
1.126     paf       331: 
1.75      paf       332:        void output_result(const VFile& body_file, bool header_only);
1.39      paf       333: };
                    334: 
1.61      paf       335: ///    Auto-object used for temporary changing Request::flang.
1.39      paf       336: class Temp_lang {
                    337:        Request& frequest;
1.111     paf       338:        uchar saved_lang;
1.39      paf       339: public:
1.111     paf       340:        Temp_lang(Request& arequest, uchar alang) : 
1.39      paf       341:                frequest(arequest),
                    342:                saved_lang(arequest.set_lang(alang)) {
                    343:        }
                    344:        ~Temp_lang() { 
                    345:                frequest.restore_lang(saved_lang); 
1.117     paf       346:        }
                    347: };
                    348: 
                    349: ///    Auto-object used for temporary changing Request::fconnection.
                    350: class Temp_connection {
                    351:        Request& frequest;
                    352:        SQL_Connection *saved_connection;
                    353: public:
                    354:        Temp_connection(Request& arequest, SQL_Connection *aconnection) : 
                    355:                frequest(arequest),
                    356:                saved_connection(arequest.set_connection(aconnection)) {
                    357:        }
                    358:        ~Temp_connection() { 
                    359:                frequest.restore_connection(saved_connection); 
1.39      paf       360:        }
1.91      parser    361: };
                    362: 
                    363: /**
                    364:        @b method parameters passed in this array.
                    365:        contains handy typecast ad junction/not junction ensurers
                    366: 
                    367: */
                    368: class MethodParams : public Array {
                    369: public:
                    370:        MethodParams(Pool& pool, const String& amethod_name) : Array(pool),
                    371:                fmethod_name(amethod_name) {
                    372:        }
                    373: 
                    374:        /// handy typecast. I long for templates
                    375:        Value& get(int index) { 
                    376:                return *static_cast<Value *>(Array::get(index)); 
                    377:        }
                    378:        /// handy is-value-a-junction ensurer
                    379:        Value& as_junction(int index, const char *msg) { 
                    380:                return get_as(index, true, msg); 
                    381:        }
                    382:        /// handy value-is-not-a-junction ensurer
                    383:        Value& as_no_junction(int index, const char *msg) { 
                    384:                return get_as(index, false, msg); 
                    385:        }
                    386:        /// handy expression auto-processing to double
1.102     parser    387:        double as_double(int index, const char *msg, Request& r) { 
                    388:                return get_processed(index, msg, r).as_double(); 
1.91      parser    389:        }
                    390:        /// handy expression auto-processing to int
1.102     parser    391:        int as_int(int index, const char *msg, Request& r) { 
                    392:                return get_processed(index, msg, r).as_int(); 
1.103     parser    393:        }
                    394:        /// handy expression auto-processing to bool
                    395:        bool as_bool(int index, const char *msg, Request& r) { 
                    396:                return get_processed(index, msg, r).as_bool(); 
1.91      parser    397:        }
                    398:        /// handy string ensurer
                    399:        const String& as_string(int index, const char *msg) { 
                    400:                return as_no_junction(index, msg).as_string(); 
                    401:        }
                    402: 
                    403: private:
                    404: 
                    405:        /// handy value-is/not-a-junction ensurer
                    406:        Value& get_as(int index, bool as_junction, const char *msg) { 
                    407:                Value& result=get(index);
                    408:                if((result.get_junction()!=0) ^ as_junction)
1.125     paf       409:                        throw Exception("parser.runtime",
1.91      parser    410:                                &fmethod_name,
                    411:                                "%s (parameter #%d)", msg, 1+index);
1.104     parser    412: 
1.91      parser    413:                return result;
                    414:        }
                    415: 
1.102     parser    416:        Value& get_processed(int index, const char *msg, Request& r) {
1.126     paf       417:                return r.process_to_value(as_junction(index, msg), 0/*no name*/);
1.91      parser    418:        }
                    419: 
                    420: private:
                    421: 
                    422:        const String& fmethod_name;
                    423: 
1.4       paf       424: };
1.1       paf       425: 
                    426: #endif

E-mail: