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

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

E-mail: