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

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

E-mail: