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

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

E-mail: