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

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

E-mail: