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

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

E-mail: