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

1.61      paf         1: /** @file
1.62      paf         2:        Parser: request class decl.
                      3: 
1.160.2.9  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.34! paf        11: static const char* IDENT_REQUEST_H="$Date: 2003/03/08 16:41:30 $";
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.160.2.6  paf        19: #include "pa_request_info.h"
                     20: #include "pa_request_charsets.h"
1.160.2.8  paf        21: #include "pa_sapi.h"
1.44      paf        22: 
1.112     paf        23: #ifdef RESOURCES_DEBUG
                     24: #include <sys/resource.h>
                     25: #endif
                     26: 
1.128     paf        27: // consts
                     28: 
                     29: const uint ANTI_ENDLESS_EXECUTE_RECOURSION=500;
                     30: 
                     31: // defines
                     32: 
1.7       paf        33: #ifndef NO_STRING_ORIGIN
1.40      paf        34: #      define COMPILE_PARAMS  \
1.160.2.10  paf        35:                VStateless_class* aclass, const char* source \
1.160.2.9  paf        36:                , const char* file
1.154     paf        37: #      define COMPILE(aclass, source, file)  \
                     38:                real_compile(aclass, source, file)
1.7       paf        39: #else
1.40      paf        40: #      define COMPILE_PARAMS  \
1.160.2.10  paf        41:                VStateless_class* aclass, const char* source
1.154     paf        42: #      define COMPILE(aclass, source, file)  \
                     43:                real_compile(aclass, source)
1.7       paf        44: #endif
1.4       paf        45: 
1.160.2.8  paf        46: // forwards
                     47: 
1.39      paf        48: class Temp_lang;
1.84      paf        49: class Methoded;
1.116     paf        50: class VMethodFrame;
1.160.2.30  paf        51: class GdomeDOMString_auto_ptr;
1.160.2.32  paf        52: class VMail; DECLARE_OBJECT_PTR(VMail);
1.160.2.33  paf        53: class VForm; DECLARE_OBJECT_PTR(VForm);
                     54: class VResponse; DECLARE_OBJECT_PTR(VResponse);
                     55: class VCookie; DECLARE_OBJECT_PTR(VCookie);
1.160.2.34! paf        56: class VStateless_class; DECLARE_OBJECT_PTR(VStateless_class);
1.1       paf        57: 
1.61      paf        58: /// Main workhorse.
1.160.2.6  paf        59: class Request: public PA_Object {
1.107     paf        60:        friend class Temp_lang;
1.117     paf        61:        friend class Temp_connection;
1.148     paf        62:        friend class Request_context_saver;
1.155     paf        63:        friend class Temp_request_self;
1.160.2.6  paf        64: 
                     65:        /// all files and classes and other hard-to-place allocations go to this pool
1.160.2.21  paf        66:        Pool& fpool;
1.160.2.6  paf        67: 
1.160.2.7  paf        68:        ///@{ core data
                     69: 
                     70:        /// classes
1.160.2.8  paf        71:        HashStringValue fclasses;
1.160.2.7  paf        72: 
                     73:        /// already used files to avoid cyclic uses
1.160.2.8  paf        74:        Hash<StringPtr, bool> used_files;
1.160.2.7  paf        75: 
                     76:        /**     endless execute(execute(... preventing counter 
                     77:                @see ANTI_ENDLESS_EXECUTE_RECOURSION
                     78:        */
                     79:        uint anti_endless_execute_recoursion;
                     80: 
                     81:        ///@}
                     82: 
                     83: public:
1.160.2.12  paf        84: 
                     85:        class StackItem {
                     86:        public:
1.160.2.29  paf        87:                StringPtr string(Pool& pool) const { return value->get_string(&pool); }
1.160.2.12  paf        88:                ValuePtr value;
                     89:                ArrayOperationPtr ops;
1.160.2.24  paf        90:                void *ptr;
1.160.2.12  paf        91: 
                     92:                /// needed to fill unused Array entries
                     93:                StackItem() {}
                     94:                StackItem(ValuePtr avalue): value(avalue) {}            
                     95:                StackItem(ArrayOperationPtr aops): ops(aops) {}
1.160.2.24  paf        96:                StackItem(void *aptr): ptr(aptr) {}
1.160.2.12  paf        97:        };
                     98: 
1.160.2.6  paf        99:        //@{ request processing status
                    100:        /// exception stack trace
1.160.2.8  paf       101:        Stack<StringPtr> exception_trace;
1.160.2.6  paf       102:        /// execution stack
1.160.2.12  paf       103:        Stack<StackItem> stack;
1.160.2.6  paf       104:        /// contexts
                    105:        VMethodFrame *method_frame;
                    106:        Value *rcontext;
                    107:        WContext *wcontext;
                    108:        /// current language
1.160.2.28  paf       109:        String_UL flang; 
1.160.2.6  paf       110:        /// current connection
                    111:        SQL_Connection *fconnection;
                    112:        //@}
                    113:        /// interrupted flag, raised on signals [SIGPIPE]
                    114:        bool finterrupted;
                    115: 
1.1       paf       116: public:
1.112     paf       117: 
1.160.2.6  paf       118:        /// all files and classes and other hard-to-place allocations go to this pool
1.160.2.1  paf       119:        Pool& pool() { return fpool; }
                    120: 
1.112     paf       121: #ifdef RESOURCES_DEBUG
                    122:        /// measures
                    123:        double sql_connect_time;
                    124:        double sql_request_time;
                    125: #endif 
1.61      paf       126: 
1.160.2.22  paf       127:        Request(Pool& apool, SAPI_Info& asapi_info, Request_info& arequest_info,
1.160.2.28  paf       128:                String_UL adefault_lang, ///< all tainted data default untainting lang
1.110     paf       129:                bool status_allowed ///<  status class allowed
1.50      paf       130:        );
1.118     paf       131:        ~Request();
1.1       paf       132: 
1.61      paf       133:        /// global classes
1.160.2.8  paf       134:        HashStringValue& classes() { return fclasses; }
1.6       paf       135: 
1.65      paf       136:        /**
                    137:                core request processing
                    138: 
                    139:                BEWARE: may throw exception to you: catch it!
                    140:        */
                    141:        void core(
1.160.2.9  paf       142:                const char* config_filespec, ///< system config filespec
1.138     paf       143:                bool config_fail_on_read_problem, ///< fail if system config file not found
1.65      paf       144:                bool header_only);
1.17      paf       145: 
1.61      paf       146:        /// executes ops
1.160.2.12  paf       147:        void execute(ArrayOperation& ops); // execute.C
1.127     paf       148:        /// execute ops with anti-recoursion check
1.160.2.12  paf       149:        void recoursion_checked_execute(StringPtr name, ArrayOperation& ops) {
1.128     paf       150:                // anti_endless_execute_recoursion
                    151:                if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
                    152:                        anti_endless_execute_recoursion=0; // give @exception a chance
                    153:                        throw Exception("parser.runtime",
                    154:                                name,
                    155:                                "call canceled - endless recursion detected");
                    156:                }
1.147     paf       157:                execute(ops); // execute it
1.128     paf       158:                anti_endless_execute_recoursion--;
                    159:        }
1.40      paf       160: 
1.64      paf       161:        /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.160.2.10  paf       162:        VStateless_classPtr use_file(VStateless_class& aclass,
1.160.2.8  paf       163:                StringPtr file_name, 
1.149     paf       164:                bool ignore_class_path=false, 
1.160.2.8  paf       165:                bool fail_on_read_problem=true, bool fail_on_file_absence=true); // pa_request.C
1.64      paf       166:        /// compiles a @a source buffer
1.160.2.10  paf       167:        VStateless_classPtr use_buf(VStateless_class& aclass,
1.160.2.9  paf       168:                const char* source, 
                    169:                StringPtr filespec, const char* filespec_cstr); // pa_request.C
1.28      paf       170: 
1.129     paf       171:        /// processes any code-junction there may be inside of @a value
1.160.2.6  paf       172:        StringOrValue process(ValuePtr input_value, bool intercept_string=true); // execute.C
1.129     paf       173:        //@{ convinient helpers
1.160.2.8  paf       174:        StringPtr process_to_string(ValuePtr input_value) {
1.160.2.6  paf       175:                return process(input_value, true/*intercept_string*/).as_string(&pool());
1.129     paf       176:        }
1.160.2.6  paf       177:        ValuePtr process_to_value(ValuePtr input_value, bool intercept_string=true) {
1.129     paf       178:                return process(input_value, intercept_string).as_value();
1.126     paf       179:        }
                    180:        //@}
1.131     paf       181: 
1.126     paf       182:        
1.131     paf       183: #define DEFINE_DUAL(modification) \
                    184:        void write_##modification##_lang(StringOrValue dual) { \
1.160.2.8  paf       185:                if(StringPtr string=dual.get_string()) \
1.131     paf       186:                        write_##modification##_lang(*string); \
                    187:                else \
1.160.2.6  paf       188:                        write_##modification##_lang(dual.get_value()); \
1.131     paf       189:        }
1.132     paf       190: #define DEFINE_DUAL_CHECKED(modification) \
1.160.2.8  paf       191:        void write_##modification##_lang(StringOrValue dual, StringPtr origin) { \
                    192:                if(StringPtr string=dual.get_string()) \
1.132     paf       193:                        write_##modification##_lang(*string); \
                    194:                else \
1.160.2.6  paf       195:                        write_##modification##_lang(dual.get_value(), origin); \
1.132     paf       196:        }
1.131     paf       197: 
1.61      paf       198:        /// appending, sure of clean string inside
1.65      paf       199:        void write_no_lang(const String& astring) {
1.111     paf       200:                wcontext->write(astring, 
                    201:                        String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
1.49      paf       202:        }
1.131     paf       203:        /// appending sure value, that would be converted to clean string
1.160.2.6  paf       204:        void write_no_lang(ValuePtr avalue) {
1.131     paf       205:                if(wcontext->get_in_expression())
1.160.2.10  paf       206:                        wcontext->write(pool(), avalue);
1.131     paf       207:                else
1.160.2.6  paf       208:                        wcontext->write(pool(), avalue, 
1.131     paf       209:                                String::UL_CLEAN | flang&String::UL_OPTIMIZE_BIT);
                    210:        }
                    211: 
1.61      paf       212:        /// appending string, passing language built into string being written
1.65      paf       213:        void write_pass_lang(const String& astring) {
                    214:                wcontext->write(astring, String::UL_PASS_APPENDED); 
1.59      paf       215:        }
1.131     paf       216:        /// appending possible string, passing language built into string being written
1.160.2.6  paf       217:        void write_pass_lang(ValuePtr avalue) {
                    218:                wcontext->write(pool(), avalue, String::UL_PASS_APPENDED); 
1.131     paf       219:        }
                    220:        DEFINE_DUAL(pass)
                    221: 
1.61      paf       222:        /// appending possible string, assigning untaint language
1.160.2.6  paf       223:        void write_assign_lang(ValuePtr avalue) {
                    224:                wcontext->write(pool(), avalue, flang); 
1.106     parser    225:        }
1.132     paf       226:        /// appending possible string, assigning untaint language
1.160.2.8  paf       227:        void write_assign_lang(ValuePtr avalue, StringPtr origin) {
1.160.2.6  paf       228:                wcontext->write(pool(), avalue, flang, origin); 
1.132     paf       229:        }
1.106     parser    230:        /// appending string, assigning untaint language
                    231:        void write_assign_lang(const String& astring) {
                    232:                wcontext->write(astring, flang); 
1.22      paf       233:        }
1.131     paf       234:        DEFINE_DUAL(assign)
1.132     paf       235:        DEFINE_DUAL_CHECKED(assign)
1.22      paf       236: 
1.64      paf       237:        /// returns relative to @a path  path to @a file 
1.160.2.10  paf       238:        StringPtr relative(const char* apath, StringPtr relative_name);
1.61      paf       239: 
1.64      paf       240:        /// returns an absolute @a path to relative @a name
1.160.2.10  paf       241:        StringPtr absolute(StringPtr relative_name);
1.42      paf       242: 
1.80      paf       243:        /// returns the mime type of 'user_file_name_cstr'
1.160.2.10  paf       244:        StringPtr mime_type_of(const char* user_file_name_cstr);
1.80      paf       245: 
1.117     paf       246:        /// returns current SQL connection if any
1.160.2.8  paf       247:        SQL_Connection *connection(StringPtr source) { 
1.117     paf       248:                if(!fconnection && source)
1.125     paf       249:                        throw Exception("parser.runtime",
1.117     paf       250:                                source,
                    251:                                "outside of 'connect' operator");
                    252: 
                    253:                return fconnection; 
1.133     paf       254:        }
                    255: 
1.154     paf       256:        bool origins_mode();
1.117     paf       257: 
1.158     paf       258:        void interrupt() { finterrupted=true; }
                    259:        bool interrupted() { return finterrupted; }
                    260: 
1.17      paf       261: public:
1.22      paf       262:        
1.61      paf       263:        /// info from web server
1.160.2.8  paf       264:        Request_info& request_info;
                    265: 
                    266:        /// info about ServerAPI
                    267:        SAPI_Info& sapi_info;
1.160.2.2  paf       268: 
                    269:        /// source, client, mail charsets
1.160.2.6  paf       270:        Request_charsets charsets;
1.53      paf       271: 
1.154     paf       272:        /// 'MAIN' class conglomerat & operators are methods of this class
1.160.2.8  paf       273:        VStateless_classPtr main_class;
1.86      paf       274:        /// $form:elements
1.160.2.10  paf       275:        VFormPtr form;
1.140     paf       276:        /// $mail
1.160.2.10  paf       277:        VMailPtr mail;
1.86      paf       278:        /// $response:elements
1.160.2.10  paf       279:        VResponsePtr response;
1.86      paf       280:        /// $cookie:elements
1.160.2.10  paf       281:        VCookiePtr cookie;
1.70      paf       282: 
1.148     paf       283:        /// classes configured data
1.160.2.20  paf       284:        HashStringObject classes_conf;
1.85      paf       285: 
1.148     paf       286: public: // status read methods
1.76      paf       287: 
1.148     paf       288:        VMethodFrame *get_method_frame() { return method_frame; }
1.160.2.13  paf       289:        ValuePtr get_self();
1.160.2.18  paf       290: #define GET_SELF(request, type) (*static_cast<type *>(request.get_self().get()))
                    291:        /* for strange reason call to this: 
                    292:                r.get_self<VHash>() 
                    293:                refuses to compile
                    294: 
                    295:        template<typename T> T& get_self() {
                    296:                return *static_cast<T*>(get_self().get());
                    297:        }
                    298:        */
1.160.2.30  paf       299: 
                    300: #ifdef XML
                    301: public: // charset helpers
                    302: 
                    303:        /// @see Charset::transcode
                    304:        GdomeDOMString_auto_ptr transcode(StringPtr s);
                    305:        /// @see Charset::transcode
                    306:        StringPtr transcode(GdomeDOMString* s
                    307: #ifndef NO_STRING_ORIGIN
                    308:                , StringPtr origin
                    309: #endif
                    310:                );
1.160.2.31  paf       311:        /// @see Charset::transcode
                    312:        StringPtr transcode(xmlChar* s
                    313: #ifndef NO_STRING_ORIGIN
                    314:                , StringPtr origin
                    315: #endif
                    316:                );
                    317: 
1.160.2.30  paf       318: #endif
1.87      paf       319: 
1.136     paf       320: private:
                    321: 
                    322:        /// already executed some @conf method
                    323:        bool configure_admin_done;
                    324: 
1.160.2.10  paf       325:        void configure_admin(VStateless_class& conf_class, StringPtr source);
1.99      parser    326: 
1.7       paf       327: private: // compile.C
                    328: 
1.46      paf       329:        VStateless_class& real_compile(COMPILE_PARAMS);
1.7       paf       330: 
                    331: private: // execute.C
                    332: 
1.139     paf       333:        /// for @postprocess[body]
1.160.2.10  paf       334:        StringPtr execute_method(VMethodFrame& amethodFrame, const Method& method);
1.139     paf       335:        //{ for @conf[filespec] and @auto[filespec]
1.160.2.13  paf       336:        StringPtr execute_method(ValuePtr aself, 
1.160.2.10  paf       337:                const Method& method, VStringPtr optional_param,
                    338:                bool do_return_string);
1.160.2.25  paf       339:        struct Execute_nonvirtual_method_result {
                    340:                StringPtr string;
                    341:                MethodPtr method;
                    342:        };
                    343:        Execute_nonvirtual_method_result execute_nonvirtual_method(VStateless_class& aclass, 
1.160.2.10  paf       344:                StringPtr method_name, VStringPtr optional_param,
1.160.2.25  paf       345:                bool do_return_string);
1.139     paf       346:        //}
                    347:        /// for @main[]
1.160.2.13  paf       348:        StringPtr execute_virtual_method(ValuePtr aself, StringPtr method_name);
1.9       paf       349: 
1.160.2.13  paf       350:        ValuePtr get_element(StringPtr& remember_name, bool can_call_operator);
1.22      paf       351: 
1.58      paf       352: private: // defaults
                    353: 
1.160.2.28  paf       354:        const String_UL fdefault_lang;
1.80      paf       355: 
                    356: private: // mime types
                    357: 
                    358:        /// $MAIN:MIME-TYPES
                    359:        Table *mime_types;
1.22      paf       360: 
1.39      paf       361: private: // lang manipulation
1.22      paf       362: 
1.160.2.28  paf       363:        String_UL set_lang(String_UL alang) {
                    364:                String_UL result=flang;
1.39      paf       365:                flang=alang;
                    366:                return result;
                    367:        }
1.160.2.28  paf       368:        void restore_lang(String_UL alang) {
1.39      paf       369:                flang=alang;
                    370:        }
                    371: 
1.117     paf       372: private: // connection manipulation
                    373: 
                    374:        SQL_Connection *set_connection(SQL_Connection *aconnection) {
                    375:                SQL_Connection *result=fconnection;
                    376:                fconnection=aconnection;
                    377:                return result;
                    378:        }
                    379:        void restore_connection(SQL_Connection *aconnection) {
                    380:                fconnection=aconnection;
                    381:        }
                    382: 
                    383: private:
                    384: 
1.160.2.11  paf       385:        void output_result(VFilePtr body_file, bool header_only, bool as_attachment);
1.148     paf       386: };
                    387: 
                    388: /// Auto-object used to save request context across ^try body
                    389: class Request_context_saver {
                    390:        Request& fr;
                    391: 
                    392:        /// exception stack trace
                    393:        int exception_trace;
                    394:        /// execution stack
                    395:        int stack;
                    396:        /// contexts
                    397:        VMethodFrame *method_frame;
                    398:        Value *rcontext;
                    399:        WContext *wcontext;
                    400:        /// current language
1.160.2.28  paf       401:        String_UL flang; 
1.148     paf       402:        /// current connection
1.117     paf       403:        SQL_Connection *fconnection;
1.126     paf       404: 
1.148     paf       405: public:
                    406:        Request_context_saver(Request& ar) : 
                    407:                exception_trace(ar.exception_trace.top_index()),        
                    408:                stack(ar.stack.top_index()),
                    409:                method_frame(ar.method_frame),
                    410:                rcontext(ar.rcontext),
                    411:                wcontext(ar.wcontext),
                    412:                flang(ar.flang),
                    413:                fconnection(ar.fconnection),
                    414:                fr(ar) {}
1.153     paf       415:        void restore() {
1.148     paf       416:                fr.exception_trace.top_index(exception_trace);
                    417:                fr.stack.top_index(stack);
1.157     paf       418:                fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148     paf       419:                fr.flang=flang;
                    420:                fr.fconnection=fconnection;
                    421:        }
1.39      paf       422: };
                    423: 
1.61      paf       424: ///    Auto-object used for temporary changing Request::flang.
1.39      paf       425: class Temp_lang {
                    426:        Request& frequest;
1.160.2.15  paf       427:        String_UL saved_lang;
1.39      paf       428: public:
1.160.2.15  paf       429:        Temp_lang(Request& arequest, String_UL alang) : 
1.39      paf       430:                frequest(arequest),
                    431:                saved_lang(arequest.set_lang(alang)) {
                    432:        }
                    433:        ~Temp_lang() { 
                    434:                frequest.restore_lang(saved_lang); 
1.117     paf       435:        }
                    436: };
                    437: 
                    438: ///    Auto-object used for temporary changing Request::fconnection.
                    439: class Temp_connection {
                    440:        Request& frequest;
                    441:        SQL_Connection *saved_connection;
                    442: public:
                    443:        Temp_connection(Request& arequest, SQL_Connection *aconnection) : 
                    444:                frequest(arequest),
                    445:                saved_connection(arequest.set_connection(aconnection)) {
                    446:        }
                    447:        ~Temp_connection() { 
                    448:                frequest.restore_connection(saved_connection); 
1.39      paf       449:        }
1.4       paf       450: };
1.160.2.7  paf       451: 
1.160.2.15  paf       452: 
                    453: // defines for externs
                    454: 
                    455: #define CONTENT_DISPOSITION_NAME "content-disposition"
                    456: #define CONTENT_DISPOSITION_VALUE "attachment"
                    457: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
                    458: 
1.160.2.7  paf       459: // externs
                    460: 
                    461: extern StringPtr main_method_name;
1.160.2.20  paf       462: extern StringPtr auto_method_name;
1.160.2.16  paf       463: extern StringPtr body_name;
1.160.2.15  paf       464: extern StringPtr content_disposition_name;
                    465: extern StringPtr content_disposition_value;
                    466: extern StringPtr content_disposition_filename_name;
1.160.2.19  paf       467: 
                    468: // defines for statics
                    469: 
                    470: #define MAIN_CLASS_NAME "MAIN"
1.160.2.21  paf       471: #define AUTO_FILE_NAME "auto.p"
1.1       paf       472: 
                    473: #endif

E-mail: