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

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

E-mail: