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

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

E-mail: