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

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

E-mail: