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

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

E-mail: