|
|
| version 1.258, 2020/12/15 17:10:31 | version 1.281, 2026/10/01 12:46:17 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: request class decl. | Parser: request class decl. |
| Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| #ifndef PA_REQUEST_H | #ifndef PA_REQUEST_H |
| Line 12 | Line 12 |
| #include "pa_pool.h" | #include "pa_pool.h" |
| #include "pa_hash.h" | #include "pa_hash.h" |
| #include "pa_file.h" | |
| #include "pa_wcontext.h" | #include "pa_wcontext.h" |
| #include "pa_value.h" | #include "pa_value.h" |
| #include "pa_vhash.h" | |
| #include "pa_stack.h" | #include "pa_stack.h" |
| #include "pa_request_info.h" | #include "pa_request_info.h" |
| #include "pa_request_charsets.h" | #include "pa_request_charsets.h" |
| #include "pa_sapi.h" | #include "pa_sapi.h" |
| // defines for externs | |
| #define EXCEPTION_HANDLED_PART_NAME "handled" | |
| // externs | // externs |
| extern const String main_method_name; | extern const String main_method_name; |
| extern const String auto_method_name; | extern const String auto_method_name; |
| extern const String exception_type_part_name; | |
| extern const String exception_source_part_name; | |
| extern const String exception_comment_part_name; | |
| extern const String exception_handled_part_name; | |
| // defines for statics | // defines for statics |
| #define MAIN_CLASS_NAME "MAIN" | #define MAIN_CLASS_NAME "MAIN" |
| Line 54 class VResponse; | Line 46 class VResponse; |
| class VCookie; | class VCookie; |
| class VStateless_class; | class VStateless_class; |
| class VConsole; | class VConsole; |
| class VException; | |
| extern int pa_loop_limit; | extern int pa_loop_limit; |
| extern int pa_execute_recoursion_limit; | extern int pa_array_limit; |
| extern int pa_execute_recursion_limit; | |
| extern int pa_httpd_timeout; | extern int pa_httpd_timeout; |
| extern size_t pa_file_size_limit; | extern size_t pa_file_size_limit; |
| Line 66 class Request: public PA_Object { | Line 60 class Request: public PA_Object { |
| friend class Temp_connection; | friend class Temp_connection; |
| friend class Temp_request_self; | friend class Temp_request_self; |
| friend class Temp_value_element; | friend class Temp_value_element; |
| friend class Temp_recursion; | |
| friend class Request_context_saver; | friend class Request_context_saver; |
| friend class Exception_trace; | friend class Exception_trace; |
| friend class VException; | |
| public: | public: |
| class Trace { | class Trace { |
| Line 123 private: | Line 119 private: |
| size_t bottom_index() { return fbottom; } | size_t bottom_index() { return fbottom; } |
| void set_bottom_index(size_t abottom) { fbottom=abottom; } | void set_bottom_index(size_t abottom) { fbottom=abottom; } |
| element_type bottom_value() { return get(bottom_index()); } | element_type bottom_value() { return get(bottom_index()); } |
| void set_range(size_t bottom, size_t top) { fbottom=bottom; fsize=top; } | |
| void clear() { | void clear() { |
| fused=fbottom=0; | fsize=fbottom=0; |
| } | } |
| bool is_empty() { | bool is_empty() { |
| return fused==fbottom; | return fsize==fbottom; |
| } | |
| Table &table(Request &r, const String* problem_source) { | |
| return table(r, problem_source, bottom_index(), top_index()); | |
| } | } |
| Table &table(Request &r, const String* problem_source, size_t bottom, size_t top); | |
| }; | }; |
| ///@{ core data | ///@{ core data |
| /// classes | /// classes |
| HashString<VStateless_class*> fclasses; | OrderedHashString<VStateless_class*> fclasses; |
| /// already used files to avoid cyclic uses | /// already used files to avoid cyclic uses |
| HashString<bool> used_files; | HashString<bool> used_files; |
| Line 145 private: | Line 147 private: |
| Array<String::Body> file_list; | Array<String::Body> file_list; |
| /// endless execute(execute(... preventing counter | /// endless execute(execute(... preventing counter |
| int anti_endless_execute_recoursion; | int anti_endless_execute_recursion; |
| int execute_recursion_limit; | |
| void recursion_limit_reached(); | |
| ///@} | ///@} |
| /// execution stack | /// execution stack |
| Stack<StackItem> stack; | Stack<StackItem> stack; |
| public: | |
| /// exception stack trace | /// exception stack trace |
| Exception_trace exception_trace; | Exception_trace exception_trace; |
| public: | |
| bool allow_class_replace; | bool allow_class_replace; |
| Line 178 private: | Line 182 private: |
| public: | public: |
| uint register_file(String::Body file_spec); | uint register_file(String::Body file_spec); |
| struct Exception_details { | const char* get_exception_cstr(const Exception& e, VException& details); |
| const Operation::Origin origin; | |
| const String* problem_source; | |
| VHash& vhash; | |
| Exception_details(const Operation::Origin aorigin, const String* aproblem_source, VHash& avhash): origin(aorigin), problem_source(aproblem_source), vhash(avhash) {} | |
| }; | |
| Exception_details get_details(const Exception& e); | |
| const char* get_exception_cstr(const Exception& e, Exception_details& details); | |
| /// @see Stack::wipe_unused | /// @see Stack::wipe_unused |
| void wipe_unused_execution_stack() { | void wipe_unused_execution_stack() { |
| Line 204 public: | Line 201 public: |
| ~Request(); | ~Request(); |
| /// global classes | /// global classes |
| HashString<VStateless_class*>& classes() { return fclasses; } | OrderedHashString<VStateless_class*>& classes() { return fclasses; } |
| VStateless_class* get_class(const String& name); | VStateless_class* get_class(const String& name); |
| VStateless_class& get_class_ref(const String& name); | |
| void put_class(VStateless_class *aclass){ classes().put(aclass->type(), aclass); } | void put_class(VStateless_class *aclass){ classes().put(aclass->type(), aclass); } |
| bool add_class(const char* atype, VStateless_class *aclass); | |
| /** | /** |
| core request processing | core request processing |
| BEWARE: may throw exception to you: catch it! | BEWARE: may throw exception to you: catch it! |
| */ | */ |
| void core(const char* config_filespec, bool header_only, const String& amain_method_name = main_method_name); | void core(const char* config_filespec, bool header_only, const String& amain_method_name = main_method_name, const String* amain_class_name = NULL); |
| /// executes ops | /// executes ops |
| void execute(ArrayOperation& ops); // execute.C | void execute(ArrayOperation& ops); // execute.C |
| Line 245 public: | Line 244 public: |
| Value& construct(VStateless_class &class_value, const Method &method); | Value& construct(VStateless_class &class_value, const Method &method); |
| /// execute ops with anti-recoursion check | /// execute ops with anti-recursion check |
| void recoursion_checked_execute(ArrayOperation& ops) { | void recursion_checked_execute(ArrayOperation& ops) { |
| if(++anti_endless_execute_recoursion==pa_execute_recoursion_limit) { | if(++anti_endless_execute_recursion>=execute_recursion_limit) |
| anti_endless_execute_recoursion=0; // give @exception a chance | recursion_limit_reached(); |
| throw Exception(PARSER_RUNTIME, 0, "call canceled - endless recursion detected"); | |
| } | |
| execute(ops); // execute it | execute(ops); // execute it |
| anti_endless_execute_recoursion--; | anti_endless_execute_recursion--; |
| } | |
| /// take the recovery reserve back if recursion limit is far enough | |
| void recursion_limit_try_restore() { | |
| if(execute_recursion_limit>pa_execute_recursion_limit && anti_endless_execute_recursion<pa_execute_recursion_limit/2) | |
| execute_recursion_limit=pa_execute_recursion_limit; | |
| } | } |
| /// | /// |
| Line 300 public: | Line 303 public: |
| /// returns relative to @a path path to @a file | /// returns relative to @a path path to @a file |
| const String& relative(const char* apath, const String& relative_name); | const String& relative(const char* apath, const String& relative_name); |
| const String& full_disk_path(const String& relative_name); | /// '/'-normalized disk path of a file name; a uri of another scheme is returned as is if allowed, otherwise throws |
| const String& full_disk_path(const String& relative_name, uint allowed=0); | |
| /// '/'-normalized full_disk_path of a real file on disk, which does not throw: 0 for a uri or a remote, invalid or tainted file:// one | |
| const String* real_disk_path(const String& name); | |
| /// returns the mime type of 'user_file_name' | /// returns the mime type of 'user_file_name' |
| const String& mime_type_of(const String* file_name); | const String& mime_type_of(const String* file_name); |
| Line 366 public: // status read methods | Line 372 public: // status read methods |
| //{ for @conf[filespec] and @auto[filespec] | //{ for @conf[filespec] and @auto[filespec] |
| bool execute_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param); | bool execute_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param); |
| bool execute_auto_method_if_exists(VStateless_class& aclass, const String& method_name, Value* optional_param); | |
| //} | //} |
| Line 426 private: | Line 433 private: |
| void output_result(VFile* body_file, bool header_only, bool as_attachment); | void output_result(VFile* body_file, bool header_only, bool as_attachment); |
| }; | }; |
| /// Hash-compatible exception; hash is built on demand. | |
| class VException: public VHashReference { | |
| Exception fexception; | |
| String::Body ffile; | |
| Value* fhandled; | |
| size_t ftrace_bottom, ftrace_top; | |
| public: | |
| Operation::Origin origin; | |
| const String* problem_source; | |
| VException(Request& r, const Exception& e); | |
| override HashStringValue& hash(); | |
| override Value* get_element(const String& name); | |
| override const VJunction* put_element(const String& name, Value* value); | |
| Value* handled(); | |
| Table& stack(Request& r); | |
| void expire_trace() { ftrace_top=size_t(-1); } | |
| }; | |
| /// Auto-object used to save request context across ^try body | /// Auto-object used to save request context across ^try body |
| class Request_context_saver { | class Request_context_saver { |
| Request& fr; | Request& fr; |
| /// exception stack trace | |
| size_t exception_trace_top; | |
| size_t exception_trace_bottom; | |
| /// execution stack | /// execution stack |
| size_t stack; | size_t stack; |
| uint anti_endless_execute_recoursion; | int anti_endless_execute_recursion; |
| /// contexts | /// contexts |
| VMethodFrame* method_frame; | VMethodFrame* method_frame; |
| Value* rcontext; | Value* rcontext; |
| Line 448 class Request_context_saver { | Line 473 class Request_context_saver { |
| public: | public: |
| Request_context_saver(Request& ar) : | Request_context_saver(Request& ar) : |
| fr(ar), | fr(ar), |
| exception_trace_top(ar.exception_trace.top_index()), | |
| exception_trace_bottom(ar.exception_trace.bottom_index()), | |
| stack(ar.stack.top_index()), | stack(ar.stack.top_index()), |
| anti_endless_execute_recoursion(ar.anti_endless_execute_recoursion), | anti_endless_execute_recursion(ar.anti_endless_execute_recursion), |
| method_frame(ar.method_frame), | method_frame(ar.method_frame), |
| rcontext(ar.rcontext), | rcontext(ar.rcontext), |
| wcontext(ar.wcontext), | wcontext(ar.wcontext), |
| flang(ar.flang), | flang(ar.flang), |
| fconnection(ar.fconnection) {} | fconnection(ar.fconnection) {} |
| void restore() { | void restore() { |
| fr.exception_trace.set_top_index(exception_trace_top); | |
| fr.exception_trace.set_bottom_index(exception_trace_bottom); | |
| fr.stack.set_top_index(stack); | fr.stack.set_top_index(stack); |
| fr.anti_endless_execute_recoursion=anti_endless_execute_recoursion; | fr.anti_endless_execute_recursion=anti_endless_execute_recursion; |
| fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext; | fr.method_frame=method_frame, fr.rcontext=rcontext; fr.wcontext=wcontext; |
| fr.flang=flang; | fr.flang=flang; |
| fr.fconnection=fconnection; | fr.fconnection=fconnection; |
| Line 534 public: | Line 555 public: |
| ~Temp_value_element(); | ~Temp_value_element(); |
| }; | }; |
| /// Auto-object that temporarily increases recursion | |
| class Temp_recursion { | |
| Request& frequest; | |
| public: | |
| Temp_recursion(Request& arequest) : frequest(arequest){ | |
| frequest.anti_endless_execute_recursion++; | |
| } | |
| ~Temp_recursion(){ | |
| frequest.anti_endless_execute_recursion--; | |
| } | |
| }; | |
| #endif | #endif |