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

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

E-mail: