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

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.260   ! moko       11: #define IDENT_PA_REQUEST_H "$Id: pa_request.h,v 1.259 2020/12/16 19:45:28 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);
1.260   ! moko      211:        VStateless_class& get_class_ref(const String& name);
1.224     moko      212:        void put_class(VStateless_class *aclass){ classes().put(aclass->type(), aclass); }
1.6       paf       213: 
1.65      paf       214:        /**
                    215:                core request processing
                    216:                BEWARE: may throw exception to you: catch it!
                    217:        */
1.257     moko      218:        void core(const char* config_filespec, bool header_only, const String& amain_method_name = main_method_name);
1.17      paf       219: 
1.61      paf       220:        /// executes ops
1.162     paf       221:        void execute(ArrayOperation& ops); // execute.C
1.239     moko      222: 
                    223:        template<typename Frame> void call(Frame& frame){
                    224:                VMethodFrame *saved_method_frame=method_frame;
                    225:                Value* saved_rcontext=rcontext;
                    226:                WContext *saved_wcontext=wcontext;
                    227: 
                    228:                rcontext=wcontext=method_frame=&frame;
                    229: 
                    230:                frame.call(*this);
                    231: 
                    232:                wcontext=saved_wcontext;
                    233:                rcontext=saved_rcontext;
                    234:                method_frame=saved_method_frame;
                    235:        }
                    236: 
                    237:        template<typename Frame> void call_write(Frame& frame){
                    238:                VMethodFrame *saved_method_frame=method_frame;
                    239:                Value* saved_rcontext=rcontext;
                    240: 
                    241:                rcontext=method_frame=&frame;
                    242: 
                    243:                frame.call(*this);
                    244: 
                    245:                rcontext=saved_rcontext;
                    246:                method_frame=saved_method_frame;
                    247:        }
                    248: 
1.225     moko      249:        Value& construct(VStateless_class &class_value, const Method &method);
1.202     moko      250: 
1.127     paf       251:        /// execute ops with anti-recoursion check
1.229     moko      252:        void recoursion_checked_execute(ArrayOperation& ops) {
                    253:                if(++anti_endless_execute_recoursion==pa_execute_recoursion_limit) {
1.128     paf       254:                        anti_endless_execute_recoursion=0; // give @exception a chance
1.228     moko      255:                        throw Exception(PARSER_RUNTIME, 0, "call canceled - endless recursion detected");
1.128     paf       256:                }
1.147     paf       257:                execute(ops); // execute it
1.128     paf       258:                anti_endless_execute_recoursion--;
                    259:        }
1.40      paf       260: 
1.199     misha     261:        ///
1.255     moko      262:        void use_file_directly(const String& file_spec, bool fail_on_file_absence=true, bool with_auto_p=false);
1.228     moko      263: 
1.255     moko      264:        /// compiles the file in main class context by default
                    265:        void use_file(const String& file_name, const String* use_filespec, bool with_auto_p=false);
1.241     moko      266: 
                    267:        /// for @USE only, calls ^use (which may be user-defined)
                    268:        void use_file(const String& file_name, const String* use_filespec, Operation::Origin origin);
1.199     misha     269: 
1.64      paf       270:        /// compiles a @a source buffer
1.228     moko      271:        void use_buf(VStateless_class& aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset=0);
1.28      paf       272: 
1.129     paf       273:        /// processes any code-junction there may be inside of @a value
1.227     moko      274:        Value& process_getter(Junction& junction); // execute.C
1.231     moko      275:        Value& process(Value& input_value); // execute.C
1.192     misha     276:        void process_write(Value& input_value); // execute.C
1.228     moko      277: 
1.129     paf       278:        //@{ convinient helpers
                    279:        const String& process_to_string(Value& input_value) {
1.231     moko      280:                return process(input_value).as_string();
1.129     paf       281:        }
1.126     paf       282:        //@}
1.247     moko      283:        const Operation::Origin get_method_origin(const Method* method); // execute.C
1.242     moko      284:        const String* get_method_filespec(const Method* method); // execute.C
                    285:        const String* get_used_filespec(uint file_no);
1.126     paf       286:        
1.238     moko      287:        /// appending string with it's languages
1.239     moko      288:        inline void write(const String& astring) {
1.236     moko      289:                wcontext->write(astring);
1.49      paf       290:        }
1.238     moko      291:        
                    292:        /// in [] and {} appending string if get_string is not null, else appending value
1.240     moko      293:        /// in () appending string if is_string, else appending value
1.239     moko      294:        inline void write(Value& avalue) {
1.236     moko      295:                wcontext->write_as_string(avalue);
1.131     paf       296:        }
                    297: 
1.238     moko      298:        /// allways appending value
1.239     moko      299:        inline void write_value(Value& avalue) {
1.226     moko      300:                wcontext->write(avalue);
                    301:        }
                    302: 
1.64      paf       303:        /// returns relative to @a path  path to @a file 
1.162     paf       304:        const String& relative(const char* apath, const String& relative_name);
1.61      paf       305: 
1.256     moko      306:        const String& full_disk_path(const String& relative_name);
1.42      paf       307: 
1.209     misha     308:        /// returns the mime type of 'user_file_name'
                    309:        const String& mime_type_of(const String* file_name);
                    310: 
1.80      paf       311:        /// returns the mime type of 'user_file_name_cstr'
1.162     paf       312:        const String& mime_type_of(const char* user_file_name_cstr);
1.80      paf       313: 
1.117     paf       314:        /// returns current SQL connection if any
1.228     moko      315:        SQL_Connection* connection(bool fail_on_error=true) {
1.162     paf       316:                if(fail_on_error && !fconnection)
1.228     moko      317:                        throw Exception(PARSER_RUNTIME, 0, "outside of 'connect' operator");
                    318:                return fconnection;
1.133     paf       319:        }
                    320: 
1.245     moko      321:        Skip get_skip() { return fskip; }
1.181     paf       322:        void set_skip(Skip askip) { fskip=askip; }
1.246     moko      323:        void set_skip_return(VMethodFrame& amethod_frame) { fskip=SKIP_RETURN; freturn_method_frame=&amethod_frame; }
1.245     moko      324:        inline bool check_skip_break() { bool result=fskip >= SKIP_BREAK; if(fskip <= SKIP_BREAK) fskip=SKIP_NOTHING; return result; }
                    325:        inline void check_skip_return() { if(fskip==SKIP_RETURN && method_frame==freturn_method_frame) fskip=SKIP_NOTHING; }
1.181     paf       326: 
1.196     misha     327:        void set_in_cycle(int adelta) { fin_cycle+=adelta; }
                    328:        bool get_in_cycle() { return fin_cycle>0; }
                    329: 
1.17      paf       330: public:
1.22      paf       331:        
1.61      paf       332:        /// info from web server
1.162     paf       333:        Request_info& request_info;
                    334: 
                    335:        /// info about ServerAPI
                    336:        SAPI_Info& sapi_info;
                    337: 
                    338:        /// source, client, mail charsets
                    339:        Request_charsets charsets;
1.53      paf       340: 
1.154     paf       341:        /// 'MAIN' class conglomerat & operators are methods of this class
1.162     paf       342:        VStateless_class& main_class;
1.86      paf       343:        /// $form:elements
1.162     paf       344:        VForm& form;
1.140     paf       345:        /// $mail
1.162     paf       346:        VMail& mail;
1.86      paf       347:        /// $response:elements
1.162     paf       348:        VResponse& response;
1.86      paf       349:        /// $cookie:elements
1.162     paf       350:        VCookie& cookie;
1.183     misha     351:        /// $console
                    352:        VConsole& console;
1.70      paf       353: 
1.148     paf       354:        /// classes configured data
1.193     misha     355:        HashString<void*> classes_conf;
1.85      paf       356: 
1.148     paf       357: public: // status read methods
1.76      paf       358: 
1.148     paf       359:        VMethodFrame *get_method_frame() { return method_frame; }
1.162     paf       360:        Value& get_self();
1.227     moko      361: 
1.162     paf       362: #define GET_SELF(request, type) (static_cast<type &>(request.get_self()))
1.6       paf       363: 
1.205     moko      364:        /// public for ^reflection:copy[]
                    365:        void put_element(Value& ncontext, const String& name, Value* value);
                    366: 
1.251     moko      367:        /// for @main[] and parser://method/call
                    368:        const String* execute_method(VStateless_class& aclass, const String& method_name, Value* optional_param = 0);
1.173     paf       369: 
1.251     moko      370:        //{ for @conf[filespec] and @auto[filespec]
                    371:        bool execute_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param);
1.228     moko      372: 
1.173     paf       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.228     moko      398:        ArrayClass& compile(VStateless_class* aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset);
1.7       paf       399: 
                    400: private: // execute.C
1.9       paf       401: 
1.191     misha     402:        Value& get_element(Value& ncontext, const String& name);
1.219     moko      403: #ifdef FEATURE_GET_ELEMENT4CALL
                    404:        Value& get_element4call(Value& ncontext, const String& name);
                    405: #endif
1.22      paf       406: 
1.58      paf       407: private: // defaults
                    408: 
1.162     paf       409:        const String::Language fdefault_lang;
1.80      paf       410: 
                    411: private: // mime types
                    412: 
                    413:        /// $MAIN:MIME-TYPES
                    414:        Table *mime_types;
1.22      paf       415: 
1.117     paf       416: private: // connection manipulation
                    417: 
1.162     paf       418:        SQL_Connection* set_connection(SQL_Connection* aconnection) {
                    419:                SQL_Connection* result=fconnection;
1.117     paf       420:                fconnection=aconnection;
                    421:                return result;
                    422:        }
1.162     paf       423:        void restore_connection(SQL_Connection* aconnection) {
1.117     paf       424:                fconnection=aconnection;
                    425:        }
                    426: 
                    427: private:
                    428: 
1.162     paf       429:        void output_result(VFile* body_file, bool header_only, bool as_attachment);
1.148     paf       430: };
                    431: 
                    432: /// Auto-object used to save request context across ^try body
                    433: class Request_context_saver {
                    434:        Request& fr;
                    435: 
                    436:        /// exception stack trace
1.165     paf       437:        size_t exception_trace_top;
                    438:        size_t exception_trace_bottom;
1.148     paf       439:        /// execution stack
1.162     paf       440:        size_t stack;
1.166     paf       441:        uint anti_endless_execute_recoursion;
1.148     paf       442:        /// contexts
1.162     paf       443:        VMethodFrame* method_frame;
                    444:        Value* rcontext;
                    445:        WContext* wcontext;
1.148     paf       446:        /// current language
1.162     paf       447:        String::Language flang; 
1.148     paf       448:        /// current connection
1.162     paf       449:        SQL_Connection* fconnection;
1.126     paf       450: 
1.148     paf       451: public:
1.228     moko      452:        Request_context_saver(Request& ar) :
1.171     paf       453:                fr(ar),
1.228     moko      454:                exception_trace_top(ar.exception_trace.top_index()),
                    455:                exception_trace_bottom(ar.exception_trace.bottom_index()),
1.165     paf       456:                stack(ar.stack.top_index()),
1.166     paf       457:                anti_endless_execute_recoursion(ar.anti_endless_execute_recoursion),
1.148     paf       458:                method_frame(ar.method_frame),
                    459:                rcontext(ar.rcontext),
                    460:                wcontext(ar.wcontext),
                    461:                flang(ar.flang),
1.171     paf       462:                fconnection(ar.fconnection) {}
1.153     paf       463:        void restore() {
1.165     paf       464:                fr.exception_trace.set_top_index(exception_trace_top);
                    465:                fr.exception_trace.set_bottom_index(exception_trace_bottom);
                    466:                fr.stack.set_top_index(stack);
1.166     paf       467:                fr.anti_endless_execute_recoursion=anti_endless_execute_recoursion;
1.157     paf       468:                fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext;
1.148     paf       469:                fr.flang=flang;
                    470:                fr.fconnection=fconnection;
                    471:        }
1.39      paf       472: };
                    473: 
1.117     paf       474: ///    Auto-object used for temporary changing Request::fconnection.
                    475: class Temp_connection {
                    476:        Request& frequest;
1.162     paf       477:        SQL_Connection* saved_connection;
1.117     paf       478: public:
1.228     moko      479:        Temp_connection(Request& arequest, SQL_Connection* aconnection) :
1.117     paf       480:                frequest(arequest),
                    481:                saved_connection(arequest.set_connection(aconnection)) {
                    482:        }
1.228     moko      483:        ~Temp_connection() {
1.117     paf       484:                frequest.restore_connection(saved_connection); 
1.39      paf       485:        }
1.91      parser    486: };
                    487: 
1.196     misha     488: ///    Auto-object used for break out of cycle check
                    489: class InCycle {
                    490:        Request& frequest;
                    491: public:
                    492:        InCycle(Request& arequest) : frequest(arequest) {
                    493:                frequest.set_in_cycle(1);
                    494:        }
1.228     moko      495:        ~InCycle() {
1.196     misha     496:                frequest.set_in_cycle(-1);
                    497:        }
                    498: };
1.91      parser    499: 
1.245     moko      500: ///    Auto-object used for break out of cycle check
                    501: class TempSkip4Delimiter {
                    502:        Request& frequest;
                    503:        Request::Skip fskip;
                    504: public:
                    505:        TempSkip4Delimiter(Request& arequest) : frequest(arequest), fskip(arequest.get_skip()) {
                    506:                frequest.set_skip(Request::SKIP_NOTHING);
                    507:        }
                    508:        // returns true if break required, should be called
                    509:        bool check_break() {
                    510:                if(frequest.get_skip())
                    511:                        fskip=frequest.get_skip();
                    512:                frequest.set_skip(fskip <= Request::SKIP_BREAK ? Request::SKIP_NOTHING : fskip);
                    513:                return fskip >= Request::SKIP_BREAK;
                    514:        }
                    515: };
                    516: 
1.213     moko      517: ///    Auto-object used for temporary changing Request::allow_class_replace.
                    518: class Temp_class_replace {
                    519:        Request& frequest;
                    520: public:
                    521:        Temp_class_replace(Request& arequest, bool avalue) : frequest(arequest){
                    522:                frequest.allow_class_replace=avalue;
                    523:        }
                    524:        ~Temp_class_replace() {
                    525:                frequest.allow_class_replace=false;
                    526:        }
                    527: };
                    528: 
1.214     moko      529: ///    Auto-object used for temporarily substituting/removing elements
                    530: class Temp_value_element {
                    531:        Request& frequest;
                    532:        Value& fwhere;
                    533:        const String& fname;
1.216     moko      534:        Value* saved;
1.214     moko      535: public:
1.216     moko      536:        Temp_value_element(Request& arequest, Value& awhere, const String& aname, Value* awhat);
                    537:        ~Temp_value_element();
1.214     moko      538: };
                    539: 
1.1       paf       540: #endif

E-mail: