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

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

E-mail: