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

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

E-mail: