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

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

E-mail: