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

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

E-mail: