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

1.61      paf         1: /** @file
1.62      paf         2:        Parser: request class decl.
                      3: 
1.177.6.1! 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.177.6.1! paf        11: static const char * const IDENT_REQUEST_H="$Date: 2004/10/05 11:07:48 $";
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 GdomeDOMString_auto_ptr;
                     37: class VMail;
                     38: class VForm;
                     39: class VResponse;
                     40: class VCookie;
                     41: class VStateless_class;
1.1       paf        42: 
1.61      paf        43: /// Main workhorse.
1.162     paf        44: class Request: public PA_Object {
1.107     paf        45:        friend class Temp_lang;
1.117     paf        46:        friend class Temp_connection;
1.148     paf        47:        friend class Request_context_saver;
1.155     paf        48:        friend class Temp_request_self;
1.162     paf        49:        friend class Exception_trace;
                     50: 
1.1       paf        51: public:
1.162     paf        52:        class Trace {
                     53:                const String* fname;
                     54:                Operation::Origin forigin;
                     55:        public:
                     56:                Trace(): fname(0) {}
                     57:                void clear() { fname=0; }
                     58:                operator bool() const { return fname!=0; }
                     59: 
                     60:                Trace(const String* aname, const Operation::Origin aorigin):
                     61:                        fname(aname), forigin(aorigin) {}
                     62: 
                     63:                const String* name() const { return fname; }
                     64:                const Operation::Origin origin() const { return forigin; }
                     65:        };
                     66: 
1.168     paf        67: private:
                     68:        Pool fpool;
                     69: public:
                     70:        Pool& pool() { return fpool; }
1.162     paf        71: 
                     72: private:
                     73:        union StackItem {
                     74:                Value* fvalue;
                     75:                ArrayOperation* fops;
                     76:                VMethodFrame* fmethod_frame;
                     77:        public:
                     78:                Value& value() const { return *fvalue; }
                     79:                const String& string() const { 
1.167     paf        80:                        return fvalue->as_string();
1.162     paf        81:                }
                     82:                ArrayOperation& ops() const { return *fops; }
                     83:                VMethodFrame& method_frame() const { return *fmethod_frame; }
                     84: 
                     85:                /// needed to fill unused Array entries
                     86:                StackItem() {}
                     87:                StackItem(Value& avalue): fvalue(&avalue) {}            
                     88:                StackItem(ArrayOperation& aops): fops(&aops) {}
                     89:                StackItem(VMethodFrame& amethod_frame): fmethod_frame(&amethod_frame) {}
                     90:        };
                     91: 
                     92:        class Exception_trace: public Stack<Trace> {
                     93:                size_t fbottom;
                     94:        public:
                     95:                Exception_trace(): fbottom(0) {}
                     96: 
                     97:                size_t bottom_index() { return fbottom; }
1.165     paf        98:                void set_bottom_index(size_t abottom) { fbottom=abottom; }
1.162     paf        99:                element_type bottom_value() { return get(bottom_index()); }
                    100: 
                    101:                void clear() {
                    102:                        ftop=fbottom=0;
                    103:                }
                    104: 
                    105:                bool is_empty() {
                    106:                        return ftop==fbottom;
                    107:                }
                    108: 
                    109:                const element_type extract_origin(const String*& problem_source);
                    110:        };
                    111: 
                    112:        ///@{ core data
                    113: 
                    114:        /// classes
                    115:        HashStringValue fclasses;
                    116: 
                    117:        /// already used files to avoid cyclic uses
1.163     paf       118:        Hash<const String::Body, bool> used_files;
1.162     paf       119:        /// list of all used files, Operation::file_no = index to it
1.163     paf       120:        Array<String::Body> file_list;
1.162     paf       121: 
                    122:        /**     endless execute(execute(... preventing counter 
                    123:                @see ANTI_ENDLESS_EXECUTE_RECOURSION
                    124:        */
                    125:        uint anti_endless_execute_recoursion;
                    126: 
                    127:        ///@}
                    128: 
                    129:        /// execution stack
                    130:        Stack<StackItem> stack;
                    131: 
                    132:        /// exception stack trace
                    133:        Exception_trace exception_trace;
                    134: public:
                    135: 
                    136:        //@{ request processing status
                    137:        /// contexts
                    138:        VMethodFrame* method_frame;
                    139:        Value* rcontext;
                    140:        WContext* wcontext;
                    141:        /// current language
                    142:        String::Language flang; 
                    143:        /// current connection
                    144:        SQL_Connection* fconnection;
                    145:        //@}
                    146:        /// interrupted flag, raised on signals [SIGPIPE]
                    147:        bool finterrupted;
                    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.17      paf       300: public:
1.22      paf       301:        
1.61      paf       302:        /// info from web server
1.162     paf       303:        Request_info& request_info;
                    304: 
                    305:        /// info about ServerAPI
                    306:        SAPI_Info& sapi_info;
                    307: 
                    308:        /// source, client, mail charsets
                    309:        Request_charsets charsets;
1.53      paf       310: 
1.154     paf       311:        /// 'MAIN' class conglomerat & operators are methods of this class
1.162     paf       312:        VStateless_class& main_class;
1.86      paf       313:        /// $form:elements
1.162     paf       314:        VForm& form;
1.140     paf       315:        /// $mail
1.162     paf       316:        VMail& mail;
1.86      paf       317:        /// $response:elements
1.162     paf       318:        VResponse& response;
1.86      paf       319:        /// $cookie:elements
1.162     paf       320:        VCookie& cookie;
1.70      paf       321: 
1.148     paf       322:        /// classes configured data
1.169     paf       323:        Hash<const String::Body, void*> classes_conf;
1.85      paf       324: 
1.148     paf       325: public: // status read methods
1.76      paf       326: 
1.148     paf       327:        VMethodFrame *get_method_frame() { return method_frame; }
1.162     paf       328:        Value& get_self();
                    329: #define GET_SELF(request, type) (static_cast<type &>(request.get_self()))
                    330:        /* for strange reason call to this: 
                    331:                r.get_self<VHash>() 
                    332:                refuses to compile
1.87      paf       333: 
1.162     paf       334:        template<typename T> T& get_self() {
                    335:                return *static_cast<T*>(get_self().get());
                    336:        }
                    337:        */
1.6       paf       338: 
1.173     paf       339:        /// for @main[]
                    340:        const String* execute_virtual_method(Value& aself, const String& method_name);
                    341: 
                    342:        /// for @postprocess[body]
                    343:        StringOrValue execute_method(VMethodFrame& amethodFrame, const Method& method);
                    344:        //{ for @conf[filespec] and @auto[filespec] and parser://method/call
                    345:        const String* execute_method(Value& aself, 
                    346:                const Method& method, VString* optional_param,
                    347:                bool do_return_string);
                    348:        struct Execute_nonvirtual_method_result {
                    349:                const String* string;
                    350:                Method* method;
                    351:                Execute_nonvirtual_method_result(): string(0), method(0) {}
                    352:        };
                    353:        Execute_nonvirtual_method_result execute_nonvirtual_method(VStateless_class& aclass, 
                    354:                const String& method_name, VString* optional_param,
                    355:                bool do_return_string);
                    356:        //}
                    357: 
1.162     paf       358: #ifdef XML
                    359: public: // charset helpers
1.67      paf       360: 
1.162     paf       361:        /// @see Charset::transcode
                    362:        GdomeDOMString_auto_ptr transcode(const String& s);
                    363:        /// @see Charset::transcode
1.163     paf       364:        GdomeDOMString_auto_ptr transcode(const String::Body s);
1.162     paf       365:        /// @see Charset::transcode
                    366:        const String& transcode(GdomeDOMString* s);
                    367:        /// @see Charset::transcode
                    368:        const String& transcode(xmlChar* s);
1.89      parser    369: 
1.162     paf       370: #endif
1.136     paf       371: 
                    372: private:
                    373: 
                    374:        /// already executed some @conf method
                    375:        bool configure_admin_done;
                    376: 
1.162     paf       377:        void configure_admin(VStateless_class& conf_class);
1.177     paf       378: 
                    379:        void configure();
1.99      parser    380: 
1.7       paf       381: private: // compile.C
                    382: 
1.162     paf       383:        VStateless_class& compile(VStateless_class* aclass, 
                    384:                const char* source, const String* main_alias, 
1.175     paf       385:                uint file_no,
                    386:                int line_no_offset);
1.7       paf       387: 
                    388: private: // execute.C
1.9       paf       389: 
1.162     paf       390:        Value& get_element(Value& ncontext, const String& name, bool can_call_operator);
1.22      paf       391: 
1.58      paf       392: private: // defaults
                    393: 
1.162     paf       394:        const String::Language fdefault_lang;
1.80      paf       395: 
                    396: private: // mime types
                    397: 
                    398:        /// $MAIN:MIME-TYPES
                    399:        Table *mime_types;
1.22      paf       400: 
1.39      paf       401: private: // lang manipulation
1.22      paf       402: 
1.162     paf       403:        String::Language set_lang(String::Language alang) {
                    404:                String::Language result=flang;
1.39      paf       405:                flang=alang;
                    406:                return result;
                    407:        }
1.162     paf       408:        void restore_lang(String::Language alang) {
1.39      paf       409:                flang=alang;
                    410:        }
                    411: 
1.117     paf       412: private: // connection manipulation
                    413: 
1.162     paf       414:        SQL_Connection* set_connection(SQL_Connection* aconnection) {
                    415:                SQL_Connection* result=fconnection;
1.117     paf       416:                fconnection=aconnection;
                    417:                return result;
                    418:        }
1.162     paf       419:        void restore_connection(SQL_Connection* aconnection) {
1.117     paf       420:                fconnection=aconnection;
                    421:        }
                    422: 
                    423: private:
                    424: 
1.162     paf       425:        void output_result(VFile* body_file, bool header_only, bool as_attachment);
1.148     paf       426: };
                    427: 
                    428: /// Auto-object used to save request context across ^try body
                    429: class Request_context_saver {
                    430:        Request& fr;
                    431: 
                    432:        /// exception stack trace
1.165     paf       433:        size_t exception_trace_top;
                    434:        size_t exception_trace_bottom;
1.148     paf       435:        /// execution stack
1.162     paf       436:        size_t stack;
1.166     paf       437:        uint anti_endless_execute_recoursion;
1.148     paf       438:        /// contexts
1.162     paf       439:        VMethodFrame* method_frame;
                    440:        Value* rcontext;
                    441:        WContext* wcontext;
1.148     paf       442:        /// current language
1.162     paf       443:        String::Language flang; 
1.148     paf       444:        /// current connection
1.162     paf       445:        SQL_Connection* fconnection;
1.126     paf       446: 
1.148     paf       447: public:
                    448:        Request_context_saver(Request& ar) : 
1.171     paf       449:                fr(ar),
1.165     paf       450:                exception_trace_top(ar.exception_trace.top_index()),    
                    451:                exception_trace_bottom(ar.exception_trace.bottom_index()),      
                    452:                stack(ar.stack.top_index()),
1.166     paf       453:                anti_endless_execute_recoursion(ar.anti_endless_execute_recoursion),
1.148     paf       454:                method_frame(ar.method_frame),
                    455:                rcontext(ar.rcontext),
                    456:                wcontext(ar.wcontext),
                    457:                flang(ar.flang),
1.171     paf       458:                fconnection(ar.fconnection) {}
1.153     paf       459:        void restore() {
1.165     paf       460:                fr.exception_trace.set_top_index(exception_trace_top);
                    461:                fr.exception_trace.set_bottom_index(exception_trace_bottom);
                    462:                fr.stack.set_top_index(stack);
1.166     paf       463:                fr.anti_endless_execute_recoursion=anti_endless_execute_recoursion;
1.157     paf       464:                fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148     paf       465:                fr.flang=flang;
                    466:                fr.fconnection=fconnection;
                    467:        }
1.39      paf       468: };
                    469: 
1.61      paf       470: ///    Auto-object used for temporary changing Request::flang.
1.39      paf       471: class Temp_lang {
                    472:        Request& frequest;
1.162     paf       473:        String::Language saved_lang;
1.39      paf       474: public:
1.162     paf       475:        Temp_lang(Request& arequest, String::Language alang) : 
1.39      paf       476:                frequest(arequest),
                    477:                saved_lang(arequest.set_lang(alang)) {
                    478:        }
                    479:        ~Temp_lang() { 
                    480:                frequest.restore_lang(saved_lang); 
1.117     paf       481:        }
                    482: };
                    483: 
                    484: ///    Auto-object used for temporary changing Request::fconnection.
                    485: class Temp_connection {
                    486:        Request& frequest;
1.162     paf       487:        SQL_Connection* saved_connection;
1.117     paf       488: public:
1.162     paf       489:        Temp_connection(Request& arequest, SQL_Connection* aconnection) : 
1.117     paf       490:                frequest(arequest),
                    491:                saved_connection(arequest.set_connection(aconnection)) {
                    492:        }
                    493:        ~Temp_connection() { 
                    494:                frequest.restore_connection(saved_connection); 
1.39      paf       495:        }
1.91      parser    496: };
                    497: 
                    498: 
1.162     paf       499: // defines for externs
1.91      parser    500: 
1.162     paf       501: #define CONTENT_DISPOSITION_NAME "content-disposition"
                    502: #define CONTENT_DISPOSITION_VALUE "attachment"
                    503: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
1.170     paf       504: #define EXCEPTION_HANDLED_PART_NAME "handled"
                    505: 
1.162     paf       506: 
                    507: // externs
                    508: 
                    509: extern const String main_method_name;
                    510: extern const String auto_method_name;
                    511: extern const String body_name;
                    512: extern const String content_disposition_name;
                    513: extern const String content_disposition_value;
                    514: extern const String content_disposition_filename_name;
                    515: 
                    516: extern const String exception_type_part_name;
                    517: extern const String exception_source_part_name;
                    518: extern const String exception_comment_part_name;
                    519: extern const String exception_handled_part_name;
1.91      parser    520: 
1.162     paf       521: // defines for statics
1.91      parser    522: 
1.162     paf       523: #define MAIN_CLASS_NAME "MAIN"
                    524: #define AUTO_FILE_NAME "auto.p"
1.1       paf       525: 
                    526: #endif

E-mail: