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

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.8.1! paf        11: static const char * const IDENT_REQUEST_H="$Date: 2005/12/16 10:15:12 $";
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;
1.180.8.1! paf       147:        int fbreak;
1.162     paf       148: 
                    149: public:
1.175     paf       150:        uint register_file(String::Body file_spec);
1.162     paf       151: 
                    152:        struct Exception_details {
                    153:                const Trace trace;
                    154:                const String* problem_source;
                    155:                VHash& vhash;
                    156: 
                    157:                Exception_details(
                    158:                        const Trace atrace,
                    159:                        const String* aproblem_source,
1.166     paf       160:                        VHash& avhash): trace(atrace), problem_source(aproblem_source), vhash(avhash) {}
1.162     paf       161:        };
                    162:        Exception_details get_details(const Exception& e);
1.176     paf       163:        const char* get_exception_cstr(const Exception& e, Exception_details& details);
1.162     paf       164: 
                    165:        /// @see Stack::wipe_unused
                    166:        void wipe_unused_execution_stack() {
                    167:                stack.wipe_unused();
                    168:        }
1.112     paf       169: 
                    170: #ifdef RESOURCES_DEBUG
                    171:        /// measures
                    172:        double sql_connect_time;
                    173:        double sql_request_time;
                    174: #endif 
1.61      paf       175: 
1.162     paf       176:        Request(SAPI_Info& asapi_info, Request_info& arequest_info,
                    177:                String::Language adefault_lang, ///< all tainted data default untainting lang
1.110     paf       178:                bool status_allowed ///<  status class allowed
1.50      paf       179:        );
1.118     paf       180:        ~Request();
1.1       paf       181: 
1.61      paf       182:        /// global classes
1.162     paf       183:        HashStringValue& classes() { return fclasses; }
1.6       paf       184: 
1.65      paf       185:        /**
                    186:                core request processing
                    187: 
                    188:                BEWARE: may throw exception to you: catch it!
                    189:        */
                    190:        void core(
1.162     paf       191:                const char* config_filespec, ///< system config filespec
1.138     paf       192:                bool config_fail_on_read_problem, ///< fail if system config file not found
1.65      paf       193:                bool header_only);
1.17      paf       194: 
1.61      paf       195:        /// executes ops
1.162     paf       196:        void execute(ArrayOperation& ops); // execute.C
1.127     paf       197:        /// execute ops with anti-recoursion check
1.162     paf       198:        void recoursion_checked_execute(/*const String& name, */ArrayOperation& ops) {
1.128     paf       199:                // anti_endless_execute_recoursion
                    200:                if(++anti_endless_execute_recoursion==ANTI_ENDLESS_EXECUTE_RECOURSION) {
                    201:                        anti_endless_execute_recoursion=0; // give @exception a chance
                    202:                        throw Exception("parser.runtime",
1.162     paf       203:                                0, //&name,
1.128     paf       204:                                "call canceled - endless recursion detected");
                    205:                }
1.147     paf       206:                execute(ops); // execute it
1.128     paf       207:                anti_endless_execute_recoursion--;
                    208:        }
1.40      paf       209: 
1.64      paf       210:        /// compiles the file, maybe forcing it's class @a name and @a base_class.
1.162     paf       211:        void use_file(VStateless_class& aclass,
1.94      parser    212:                const String& file_name, 
1.162     paf       213:                const String* main_alias=0,
1.149     paf       214:                bool ignore_class_path=false, 
1.162     paf       215:                bool fail_on_read_problem=true, 
                    216:                bool fail_on_file_absence=true); // pa_request.C
1.64      paf       217:        /// compiles a @a source buffer
1.162     paf       218:        void use_buf(VStateless_class& aclass,
                    219:                const char* source, 
                    220:                const String* main_alias,
1.175     paf       221:                uint file_no,
                    222:                int line_no_offset=0); // pa_request.C
1.28      paf       223: 
1.129     paf       224:        /// processes any code-junction there may be inside of @a value
1.131     paf       225:        StringOrValue process(Value& input_value, bool intercept_string=true); // execute.C
1.129     paf       226:        //@{ convinient helpers
                    227:        const String& process_to_string(Value& input_value) {
                    228:                return process(input_value, true/*intercept_string*/).as_string();
                    229:        }
                    230:        Value& process_to_value(Value& input_value, bool intercept_string=true) {
                    231:                return process(input_value, intercept_string).as_value();
1.126     paf       232:        }
                    233:        //@}
1.131     paf       234: 
1.126     paf       235:        
1.131     paf       236: #define DEFINE_DUAL(modification) \
                    237:        void write_##modification##_lang(StringOrValue dual) { \
1.162     paf       238:                if(const String* string=dual.get_string()) \
1.131     paf       239:                        write_##modification##_lang(*string); \
                    240:                else \
                    241:                        write_##modification##_lang(*dual.get_value()); \
                    242:        }
                    243: 
1.61      paf       244:        /// appending, sure of clean string inside
1.65      paf       245:        void write_no_lang(const String& astring) {
1.111     paf       246:                wcontext->write(astring, 
1.162     paf       247:                        (String::Language)(String::L_CLEAN | flang&String::L_OPTIMIZE_BIT));
1.49      paf       248:        }
1.131     paf       249:        /// appending sure value, that would be converted to clean string
                    250:        void write_no_lang(Value& avalue) {
                    251:                if(wcontext->get_in_expression())
                    252:                        wcontext->write(avalue);
                    253:                else
                    254:                        wcontext->write(avalue, 
1.162     paf       255:                                (String::Language)(String::L_CLEAN | flang&String::L_OPTIMIZE_BIT));
1.131     paf       256:        }
                    257: 
1.61      paf       258:        /// appending string, passing language built into string being written
1.65      paf       259:        void write_pass_lang(const String& astring) {
1.162     paf       260:                wcontext->write(astring, String::L_PASS_APPENDED); 
1.59      paf       261:        }
1.131     paf       262:        /// appending possible string, passing language built into string being written
                    263:        void write_pass_lang(Value& avalue) {
1.162     paf       264:                wcontext->write(avalue, String::L_PASS_APPENDED); 
1.131     paf       265:        }
                    266:        DEFINE_DUAL(pass)
                    267: 
1.61      paf       268:        /// appending possible string, assigning untaint language
1.40      paf       269:        void write_assign_lang(Value& avalue) {
1.39      paf       270:                wcontext->write(avalue, flang); 
1.106     parser    271:        }
                    272:        /// appending string, assigning untaint language
                    273:        void write_assign_lang(const String& astring) {
                    274:                wcontext->write(astring, flang); 
1.22      paf       275:        }
1.131     paf       276:        DEFINE_DUAL(assign)
1.22      paf       277: 
1.64      paf       278:        /// returns relative to @a path  path to @a file 
1.162     paf       279:        const String& relative(const char* apath, const String& relative_name);
1.61      paf       280: 
1.64      paf       281:        /// returns an absolute @a path to relative @a name
1.69      paf       282:        const String& absolute(const String& relative_name);
1.42      paf       283: 
1.80      paf       284:        /// returns the mime type of 'user_file_name_cstr'
1.162     paf       285:        const String& mime_type_of(const char* user_file_name_cstr);
1.80      paf       286: 
1.117     paf       287:        /// returns current SQL connection if any
1.162     paf       288:        SQL_Connection* connection(bool fail_on_error=true) { 
                    289:                if(fail_on_error && !fconnection)
1.125     paf       290:                        throw Exception("parser.runtime",
1.162     paf       291:                                0,
1.117     paf       292:                                "outside of 'connect' operator");
                    293: 
                    294:                return fconnection; 
1.133     paf       295:        }
                    296: 
1.162     paf       297:        void set_interrupted(bool ainterrupted) { finterrupted=ainterrupted; }
                    298:        bool get_interrupted() { return finterrupted; }
1.158     paf       299: 
1.180.8.1! paf       300:        void set_break(int abreak) { fbreak=abreak; }
        !           301:        int get_break() { return fbreak; }
        !           302: 
1.17      paf       303: public:
1.22      paf       304:        
1.61      paf       305:        /// info from web server
1.162     paf       306:        Request_info& request_info;
                    307: 
                    308:        /// info about ServerAPI
                    309:        SAPI_Info& sapi_info;
                    310: 
                    311:        /// source, client, mail charsets
                    312:        Request_charsets charsets;
1.53      paf       313: 
1.154     paf       314:        /// 'MAIN' class conglomerat & operators are methods of this class
1.162     paf       315:        VStateless_class& main_class;
1.86      paf       316:        /// $form:elements
1.162     paf       317:        VForm& form;
1.140     paf       318:        /// $mail
1.162     paf       319:        VMail& mail;
1.86      paf       320:        /// $response:elements
1.162     paf       321:        VResponse& response;
1.86      paf       322:        /// $cookie:elements
1.162     paf       323:        VCookie& cookie;
1.70      paf       324: 
1.148     paf       325:        /// classes configured data
1.169     paf       326:        Hash<const String::Body, void*> classes_conf;
1.85      paf       327: 
1.148     paf       328: public: // status read methods
1.76      paf       329: 
1.148     paf       330:        VMethodFrame *get_method_frame() { return method_frame; }
1.162     paf       331:        Value& get_self();
                    332: #define GET_SELF(request, type) (static_cast<type &>(request.get_self()))
                    333:        /* for strange reason call to this: 
                    334:                r.get_self<VHash>() 
                    335:                refuses to compile
1.87      paf       336: 
1.162     paf       337:        template<typename T> T& get_self() {
                    338:                return *static_cast<T*>(get_self().get());
                    339:        }
                    340:        */
1.6       paf       341: 
1.173     paf       342:        /// for @main[]
                    343:        const String* execute_virtual_method(Value& aself, const String& method_name);
                    344: 
                    345:        /// for @postprocess[body]
                    346:        StringOrValue execute_method(VMethodFrame& amethodFrame, const Method& method);
                    347:        //{ for @conf[filespec] and @auto[filespec] and parser://method/call
                    348:        const String* execute_method(Value& aself, 
                    349:                const Method& method, VString* optional_param,
                    350:                bool do_return_string);
                    351:        struct Execute_nonvirtual_method_result {
                    352:                const String* string;
                    353:                Method* method;
                    354:                Execute_nonvirtual_method_result(): string(0), method(0) {}
                    355:        };
                    356:        Execute_nonvirtual_method_result execute_nonvirtual_method(VStateless_class& aclass, 
                    357:                const String& method_name, VString* optional_param,
                    358:                bool do_return_string);
                    359:        //}
                    360: 
1.162     paf       361: #ifdef XML
                    362: public: // charset helpers
1.67      paf       363: 
1.162     paf       364:        /// @see Charset::transcode
1.180     paf       365:        xmlChar* transcode(const String& s);
1.162     paf       366:        /// @see Charset::transcode
1.180     paf       367:        xmlChar* transcode(const String::Body s);
1.162     paf       368:        /// @see Charset::transcode
1.180     paf       369:        const String& transcode(const xmlChar* s);
1.89      parser    370: 
1.162     paf       371: #endif
1.136     paf       372: 
                    373: private:
                    374: 
                    375:        /// already executed some @conf method
                    376:        bool configure_admin_done;
                    377: 
1.162     paf       378:        void configure_admin(VStateless_class& conf_class);
1.177     paf       379: 
                    380:        void configure();
1.99      parser    381: 
1.7       paf       382: private: // compile.C
                    383: 
1.162     paf       384:        VStateless_class& compile(VStateless_class* aclass, 
                    385:                const char* source, const String* main_alias, 
1.175     paf       386:                uint file_no,
                    387:                int line_no_offset);
1.7       paf       388: 
                    389: private: // execute.C
1.9       paf       390: 
1.178     paf       391:        void put_element(Value& ncontext, const String& name, Value& value);
1.162     paf       392:        Value& get_element(Value& ncontext, const String& name, bool can_call_operator);
1.22      paf       393: 
1.58      paf       394: private: // defaults
                    395: 
1.162     paf       396:        const String::Language fdefault_lang;
1.80      paf       397: 
                    398: private: // mime types
                    399: 
                    400:        /// $MAIN:MIME-TYPES
                    401:        Table *mime_types;
1.22      paf       402: 
1.39      paf       403: private: // lang manipulation
1.22      paf       404: 
1.162     paf       405:        String::Language set_lang(String::Language alang) {
                    406:                String::Language result=flang;
1.39      paf       407:                flang=alang;
                    408:                return result;
                    409:        }
1.162     paf       410:        void restore_lang(String::Language alang) {
1.39      paf       411:                flang=alang;
                    412:        }
                    413: 
1.117     paf       414: private: // connection manipulation
                    415: 
1.162     paf       416:        SQL_Connection* set_connection(SQL_Connection* aconnection) {
                    417:                SQL_Connection* result=fconnection;
1.117     paf       418:                fconnection=aconnection;
                    419:                return result;
                    420:        }
1.162     paf       421:        void restore_connection(SQL_Connection* aconnection) {
1.117     paf       422:                fconnection=aconnection;
                    423:        }
                    424: 
                    425: private:
                    426: 
1.162     paf       427:        void output_result(VFile* body_file, bool header_only, bool as_attachment);
1.148     paf       428: };
                    429: 
                    430: /// Auto-object used to save request context across ^try body
                    431: class Request_context_saver {
                    432:        Request& fr;
                    433: 
                    434:        /// exception stack trace
1.165     paf       435:        size_t exception_trace_top;
                    436:        size_t exception_trace_bottom;
1.148     paf       437:        /// execution stack
1.162     paf       438:        size_t stack;
1.166     paf       439:        uint anti_endless_execute_recoursion;
1.148     paf       440:        /// contexts
1.162     paf       441:        VMethodFrame* method_frame;
                    442:        Value* rcontext;
                    443:        WContext* wcontext;
1.148     paf       444:        /// current language
1.162     paf       445:        String::Language flang; 
1.148     paf       446:        /// current connection
1.162     paf       447:        SQL_Connection* fconnection;
1.126     paf       448: 
1.148     paf       449: public:
                    450:        Request_context_saver(Request& ar) : 
1.171     paf       451:                fr(ar),
1.165     paf       452:                exception_trace_top(ar.exception_trace.top_index()),    
                    453:                exception_trace_bottom(ar.exception_trace.bottom_index()),      
                    454:                stack(ar.stack.top_index()),
1.166     paf       455:                anti_endless_execute_recoursion(ar.anti_endless_execute_recoursion),
1.148     paf       456:                method_frame(ar.method_frame),
                    457:                rcontext(ar.rcontext),
                    458:                wcontext(ar.wcontext),
                    459:                flang(ar.flang),
1.171     paf       460:                fconnection(ar.fconnection) {}
1.153     paf       461:        void restore() {
1.165     paf       462:                fr.exception_trace.set_top_index(exception_trace_top);
                    463:                fr.exception_trace.set_bottom_index(exception_trace_bottom);
                    464:                fr.stack.set_top_index(stack);
1.166     paf       465:                fr.anti_endless_execute_recoursion=anti_endless_execute_recoursion;
1.157     paf       466:                fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148     paf       467:                fr.flang=flang;
                    468:                fr.fconnection=fconnection;
                    469:        }
1.39      paf       470: };
                    471: 
1.61      paf       472: ///    Auto-object used for temporary changing Request::flang.
1.39      paf       473: class Temp_lang {
                    474:        Request& frequest;
1.162     paf       475:        String::Language saved_lang;
1.39      paf       476: public:
1.162     paf       477:        Temp_lang(Request& arequest, String::Language alang) : 
1.39      paf       478:                frequest(arequest),
                    479:                saved_lang(arequest.set_lang(alang)) {
                    480:        }
                    481:        ~Temp_lang() { 
                    482:                frequest.restore_lang(saved_lang); 
1.117     paf       483:        }
                    484: };
                    485: 
                    486: ///    Auto-object used for temporary changing Request::fconnection.
                    487: class Temp_connection {
                    488:        Request& frequest;
1.162     paf       489:        SQL_Connection* saved_connection;
1.117     paf       490: public:
1.162     paf       491:        Temp_connection(Request& arequest, SQL_Connection* aconnection) : 
1.117     paf       492:                frequest(arequest),
                    493:                saved_connection(arequest.set_connection(aconnection)) {
                    494:        }
                    495:        ~Temp_connection() { 
                    496:                frequest.restore_connection(saved_connection); 
1.39      paf       497:        }
1.91      parser    498: };
                    499: 
                    500: 
1.162     paf       501: // defines for externs
1.91      parser    502: 
1.162     paf       503: #define CONTENT_DISPOSITION_NAME "content-disposition"
                    504: #define CONTENT_DISPOSITION_VALUE "attachment"
                    505: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
1.170     paf       506: #define EXCEPTION_HANDLED_PART_NAME "handled"
                    507: 
1.162     paf       508: 
                    509: // externs
                    510: 
                    511: extern const String main_method_name;
                    512: extern const String auto_method_name;
                    513: extern const String body_name;
                    514: extern const String content_disposition_name;
                    515: extern const String content_disposition_value;
                    516: extern const String content_disposition_filename_name;
                    517: 
                    518: extern const String exception_type_part_name;
                    519: extern const String exception_source_part_name;
                    520: extern const String exception_comment_part_name;
                    521: extern const String exception_handled_part_name;
1.91      parser    522: 
1.162     paf       523: // defines for statics
1.91      parser    524: 
1.162     paf       525: #define MAIN_CLASS_NAME "MAIN"
                    526: #define AUTO_FILE_NAME "auto.p"
1.1       paf       527: 
                    528: #endif

E-mail: