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

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.159   ! paf        11: static const char* IDENT_REQUEST_H="$Date: 2002/11/20 13:37:23 $";
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.155     paf        63:        friend class Temp_request_self;
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.140     paf        83:                bool mail_received;
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.147     paf       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:                }
1.147     paf       117:                execute(ops); // execute it
1.128     paf       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.154     paf       122:        VStateless_class *use_file(VStateless_class& aclass,
1.94      parser    123:                const String& file_name, 
1.149     paf       124:                bool ignore_class_path=false, 
1.154     paf       125:                bool fail_on_read_problem=true, bool fail_on_file_absence=true); // core.C
1.64      paf       126:        /// compiles a @a source buffer
1.154     paf       127:        VStateless_class *use_buf(VStateless_class& aclass,
                    128:                const char *source, 
                    129:                const String& filespec, const char *filespec_cstr); // core.C
1.28      paf       130: 
1.129     paf       131:        /// processes any code-junction there may be inside of @a value
1.131     paf       132:        StringOrValue process(Value& input_value, bool intercept_string=true); // execute.C
1.129     paf       133:        //@{ convinient helpers
                    134:        const String& process_to_string(Value& input_value) {
                    135:                return process(input_value, true/*intercept_string*/).as_string();
                    136:        }
                    137:        Value& process_to_value(Value& input_value, bool intercept_string=true) {
                    138:                return process(input_value, intercept_string).as_value();
1.126     paf       139:        }
                    140:        //@}
1.131     paf       141: 
1.126     paf       142:        
1.131     paf       143: #define DEFINE_DUAL(modification) \
                    144:        void write_##modification##_lang(StringOrValue dual) { \
                    145:                if(const String *string=dual.get_string()) \
                    146:                        write_##modification##_lang(*string); \
                    147:                else \
                    148:                        write_##modification##_lang(*dual.get_value()); \
                    149:        }
1.132     paf       150: #define DEFINE_DUAL_CHECKED(modification) \
                    151:        void write_##modification##_lang(StringOrValue dual, const String *origin) { \
                    152:                if(const String *string=dual.get_string()) \
                    153:                        write_##modification##_lang(*string); \
                    154:                else \
                    155:                        write_##modification##_lang(*dual.get_value(), origin); \
                    156:        }
1.131     paf       157: 
1.61      paf       158:        /// appending, sure of clean string inside
1.65      paf       159:        void write_no_lang(const String& astring) {
1.111     paf       160:                wcontext->write(astring, 
                    161:                        String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.49      paf       162:        }
1.131     paf       163:        /// appending sure value, that would be converted to clean string
                    164:        void write_no_lang(Value& avalue) {
                    165:                if(wcontext->get_in_expression())
                    166:                        wcontext->write(avalue);
                    167:                else
                    168:                        wcontext->write(avalue, 
                    169:                                String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
                    170:        }
                    171:        //DEFINE_DUAL(no)
                    172: 
1.61      paf       173:        /// appending string, passing language built into string being written
1.65      paf       174:        void write_pass_lang(const String& astring) {
                    175:                wcontext->write(astring, String::UL_PASS_APPENDED); 
1.59      paf       176:        }
1.131     paf       177:        /// appending possible string, passing language built into string being written
                    178:        void write_pass_lang(Value& avalue) {
                    179:                wcontext->write(avalue, String::UL_PASS_APPENDED); 
                    180:        }
                    181:        DEFINE_DUAL(pass)
                    182: 
1.61      paf       183:        /// appending possible string, assigning untaint language
1.40      paf       184:        void write_assign_lang(Value& avalue) {
1.39      paf       185:                wcontext->write(avalue, flang); 
1.106     parser    186:        }
1.132     paf       187:        /// appending possible string, assigning untaint language
                    188:        void write_assign_lang(Value& avalue, const String *origin) {
                    189:                wcontext->write(avalue, flang, origin); 
                    190:        }
1.106     parser    191:        /// appending string, assigning untaint language
                    192:        void write_assign_lang(const String& astring) {
                    193:                wcontext->write(astring, flang); 
1.22      paf       194:        }
1.131     paf       195:        DEFINE_DUAL(assign)
1.132     paf       196:        DEFINE_DUAL_CHECKED(assign)
1.22      paf       197: 
1.64      paf       198:        /// returns relative to @a path  path to @a file 
1.69      paf       199:        const String& relative(const char *apath, const String& relative_name);
1.61      paf       200: 
1.64      paf       201:        /// returns an absolute @a path to relative @a name
1.69      paf       202:        const String& absolute(const String& relative_name);
1.42      paf       203: 
1.80      paf       204:        /// returns the mime type of 'user_file_name_cstr'
                    205:        const String& mime_type_of(const char *user_file_name_cstr);
                    206: 
1.117     paf       207:        /// returns current SQL connection if any
                    208:        SQL_Connection *connection(const String *source) { 
                    209:                if(!fconnection && source)
1.125     paf       210:                        throw Exception("parser.runtime",
1.117     paf       211:                                source,
                    212:                                "outside of 'connect' operator");
                    213: 
                    214:                return fconnection; 
1.133     paf       215:        }
                    216: 
1.154     paf       217:        bool origins_mode();
1.117     paf       218: 
1.158     paf       219:        void interrupt() { finterrupted=true; }
                    220:        bool interrupted() { return finterrupted; }
                    221: 
1.17      paf       222: public:
1.22      paf       223:        
1.61      paf       224:        /// info from web server
1.53      paf       225:        Info& info;
1.81      paf       226:        /// user's post data
1.145     paf       227:        const char *post_data;  size_t post_size;
1.53      paf       228: 
1.134     paf       229:        /// name of 'main' method
                    230:        const String main_method_name;
                    231:        
1.154     paf       232:        /// 'MAIN' class conglomerat & operators are methods of this class
                    233:        VClass& main_class;
1.86      paf       234:        /// $env:fields
1.57      paf       235:        VEnv env;
1.108     paf       236:        /// $status:fields
                    237:        VStatus status;
1.86      paf       238:        /// $form:elements
1.57      paf       239:        VForm form;
1.140     paf       240:        /// $mail
                    241:        VMail mail;
1.90      parser    242:        /// $math:constants
                    243:        VMath math;
1.86      paf       244:        /// $request:elements
1.57      paf       245:        VRequest request;
1.86      paf       246:        /// $response:elements
1.57      paf       247:        VResponse response;
1.86      paf       248:        /// $cookie:elements
1.60      paf       249:        VCookie cookie;
1.70      paf       250: 
1.148     paf       251:        /// classes configured data
                    252:        Hash classes_conf;
                    253: 
                    254: private:
                    255: 
1.123     paf       256:        //@{ request processing status
1.148     paf       257:        /// exception stack trace
                    258:        Stack exception_trace;
1.123     paf       259:        /// execution stack
                    260:        Stack stack;
1.61      paf       261:        /// contexts
1.146     paf       262:        VMethodFrame *method_frame;
                    263:        Value *rcontext;
1.22      paf       264:        WContext *wcontext;
1.148     paf       265:        /// current language
                    266:        uchar flang; 
                    267:        /// current connection
                    268:        SQL_Connection *fconnection;
1.123     paf       269:        //@}
1.158     paf       270:        /// interrupted flag, raised on signals [SIGPIPE]
                    271:        bool finterrupted;
1.85      paf       272: 
1.148     paf       273: public: // status read methods
1.76      paf       274: 
1.148     paf       275:        VMethodFrame *get_method_frame() { return method_frame; }
1.157     paf       276:        Value *get_self();
1.87      paf       277: 
1.7       paf       278: private: // core data
1.6       paf       279: 
1.89      parser    280:        /// classes
1.6       paf       281:        Hash fclasses;
1.67      paf       282: 
1.89      parser    283:        /// already used files to avoid cyclic uses
1.67      paf       284:        Hash used_files;
1.89      parser    285: 
                    286:        /**     endless execute(execute(... preventing counter 
                    287:                @see ANTI_ENDLESS_EXECUTE_RECOURSION
                    288:        */
                    289:        uint anti_endless_execute_recoursion;
1.136     paf       290: 
                    291: private:
                    292: 
                    293:        /// already executed some @conf method
                    294:        bool configure_admin_done;
                    295: 
                    296:        void configure_admin(VStateless_class& conf_class, const String *source);
1.99      parser    297: 
1.7       paf       298: private: // compile.C
                    299: 
1.46      paf       300:        VStateless_class& real_compile(COMPILE_PARAMS);
1.7       paf       301: 
                    302: private: // execute.C
                    303: 
1.139     paf       304:        /// for @postprocess[body]
                    305:        const String& execute_method(VMethodFrame& amethodFrame, const Method& method);
                    306:        //{ for @conf[filespec] and @auto[filespec]
                    307:        void execute_method(Value& aself, 
                    308:                const Method& method, VString *optional_param,
1.137     paf       309:                const String **return_string);
                    310:        void execute_nonvirtual_method(VStateless_class& aclass, 
1.154     paf       311:                const String& method_name, VString *optional_param,
1.137     paf       312:                const String **return_string,
                    313:                const Method **return_method=0);
1.139     paf       314:        //}
                    315:        /// for @main[]
                    316:        const String *execute_virtual_method(Value& aself, const String& method_name);
1.9       paf       317: 
1.156     paf       318:        Value *get_element(const String *& remember_name, bool can_call_operator);
1.22      paf       319: 
1.58      paf       320: private: // defaults
                    321: 
1.111     paf       322:        const uchar fdefault_lang;
1.68      paf       323:        Value *default_content_type;
1.80      paf       324: 
                    325: private: // mime types
                    326: 
                    327:        /// $MAIN:MIME-TYPES
                    328:        Table *mime_types;
1.22      paf       329: 
1.39      paf       330: private: // lang manipulation
1.22      paf       331: 
1.111     paf       332:        uchar set_lang(uchar alang) {
                    333:                uchar result=flang;
1.39      paf       334:                flang=alang;
                    335:                return result;
                    336:        }
1.111     paf       337:        void restore_lang(uchar alang) {
1.39      paf       338:                flang=alang;
                    339:        }
                    340: 
1.117     paf       341: private: // connection manipulation
                    342: 
                    343:        SQL_Connection *set_connection(SQL_Connection *aconnection) {
                    344:                SQL_Connection *result=fconnection;
                    345:                fconnection=aconnection;
                    346:                return result;
                    347:        }
                    348:        void restore_connection(SQL_Connection *aconnection) {
                    349:                fconnection=aconnection;
                    350:        }
                    351: 
                    352: private:
                    353: 
1.159   ! paf       354:        void output_result(const VFile& body_file, bool header_only, bool as_attachment);
1.148     paf       355: };
                    356: 
                    357: /// Auto-object used to save request context across ^try body
                    358: class Request_context_saver {
                    359:        Request& fr;
                    360: 
                    361:        /// exception stack trace
                    362:        int exception_trace;
                    363:        /// execution stack
                    364:        int stack;
                    365:        /// contexts
                    366:        VMethodFrame *method_frame;
                    367:        Value *rcontext;
                    368:        WContext *wcontext;
                    369:        /// current language
                    370:        uchar flang; 
                    371:        /// current connection
1.117     paf       372:        SQL_Connection *fconnection;
1.126     paf       373: 
1.148     paf       374: public:
                    375:        Request_context_saver(Request& ar) : 
                    376:                exception_trace(ar.exception_trace.top_index()),        
                    377:                stack(ar.stack.top_index()),
                    378:                method_frame(ar.method_frame),
                    379:                rcontext(ar.rcontext),
                    380:                wcontext(ar.wcontext),
                    381:                flang(ar.flang),
                    382:                fconnection(ar.fconnection),
                    383:                fr(ar) {}
1.153     paf       384:        void restore() {
1.148     paf       385:                fr.exception_trace.top_index(exception_trace);
                    386:                fr.stack.top_index(stack);
1.157     paf       387:                fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148     paf       388:                fr.flang=flang;
                    389:                fr.fconnection=fconnection;
                    390:        }
1.39      paf       391: };
                    392: 
1.61      paf       393: ///    Auto-object used for temporary changing Request::flang.
1.39      paf       394: class Temp_lang {
                    395:        Request& frequest;
1.111     paf       396:        uchar saved_lang;
1.39      paf       397: public:
1.111     paf       398:        Temp_lang(Request& arequest, uchar alang) : 
1.39      paf       399:                frequest(arequest),
                    400:                saved_lang(arequest.set_lang(alang)) {
                    401:        }
                    402:        ~Temp_lang() { 
                    403:                frequest.restore_lang(saved_lang); 
1.117     paf       404:        }
                    405: };
                    406: 
                    407: ///    Auto-object used for temporary changing Request::fconnection.
                    408: class Temp_connection {
                    409:        Request& frequest;
                    410:        SQL_Connection *saved_connection;
                    411: public:
                    412:        Temp_connection(Request& arequest, SQL_Connection *aconnection) : 
                    413:                frequest(arequest),
                    414:                saved_connection(arequest.set_connection(aconnection)) {
                    415:        }
                    416:        ~Temp_connection() { 
                    417:                frequest.restore_connection(saved_connection); 
1.39      paf       418:        }
1.91      parser    419: };
                    420: 
                    421: /**
                    422:        @b method parameters passed in this array.
                    423:        contains handy typecast ad junction/not junction ensurers
                    424: 
                    425: */
                    426: class MethodParams : public Array {
                    427: public:
                    428:        MethodParams(Pool& pool, const String& amethod_name) : Array(pool),
                    429:                fmethod_name(amethod_name) {
                    430:        }
                    431: 
                    432:        /// handy typecast. I long for templates
                    433:        Value& get(int index) { 
                    434:                return *static_cast<Value *>(Array::get(index)); 
                    435:        }
                    436:        /// handy is-value-a-junction ensurer
                    437:        Value& as_junction(int index, const char *msg) { 
                    438:                return get_as(index, true, msg); 
                    439:        }
                    440:        /// handy value-is-not-a-junction ensurer
                    441:        Value& as_no_junction(int index, const char *msg) { 
                    442:                return get_as(index, false, msg); 
                    443:        }
                    444:        /// handy expression auto-processing to double
1.102     parser    445:        double as_double(int index, const char *msg, Request& r) { 
                    446:                return get_processed(index, msg, r).as_double(); 
1.91      parser    447:        }
                    448:        /// handy expression auto-processing to int
1.102     parser    449:        int as_int(int index, const char *msg, Request& r) { 
                    450:                return get_processed(index, msg, r).as_int(); 
1.103     parser    451:        }
                    452:        /// handy expression auto-processing to bool
                    453:        bool as_bool(int index, const char *msg, Request& r) { 
                    454:                return get_processed(index, msg, r).as_bool(); 
1.91      parser    455:        }
                    456:        /// handy string ensurer
                    457:        const String& as_string(int index, const char *msg) { 
                    458:                return as_no_junction(index, msg).as_string(); 
                    459:        }
                    460: 
                    461: private:
                    462: 
                    463:        /// handy value-is/not-a-junction ensurer
                    464:        Value& get_as(int index, bool as_junction, const char *msg) { 
                    465:                Value& result=get(index);
                    466:                if((result.get_junction()!=0) ^ as_junction)
1.125     paf       467:                        throw Exception("parser.runtime",
1.91      parser    468:                                &fmethod_name,
                    469:                                "%s (parameter #%d)", msg, 1+index);
1.104     parser    470: 
1.91      parser    471:                return result;
                    472:        }
                    473: 
1.102     parser    474:        Value& get_processed(int index, const char *msg, Request& r) {
1.126     paf       475:                return r.process_to_value(as_junction(index, msg), 0/*no name*/);
1.91      parser    476:        }
                    477: 
                    478: private:
                    479: 
                    480:        const String& fmethod_name;
                    481: 
1.4       paf       482: };
1.1       paf       483: 
                    484: #endif

E-mail: