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

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

E-mail: