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

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

E-mail: