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

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

E-mail: