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

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

E-mail: