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

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

E-mail: