--- parser3/src/include/pa_request.h 2020/10/27 21:25:25 1.251 +++ parser3/src/include/pa_request.h 2026/10/16 13:55:16 1.288 @@ -1,52 +1,71 @@ /** @file Parser: request class decl. - Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #ifndef PA_REQUEST_H #define PA_REQUEST_H -#define IDENT_PA_REQUEST_H "$Id: pa_request.h,v 1.251 2020/10/27 21:25:25 moko Exp $" +#define IDENT_PA_REQUEST_H "$Id: pa_request.h,v 1.288 2026/10/16 13:55:16 moko Exp $" #include "pa_pool.h" #include "pa_hash.h" +#include "pa_file.h" #include "pa_wcontext.h" #include "pa_value.h" +#include "pa_vhash.h" +#include "pa_vresponse.h" #include "pa_stack.h" #include "pa_request_info.h" #include "pa_request_charsets.h" #include "pa_sapi.h" +// externs + +extern const String main_method_name; +extern const String auto_method_name; + +// defines for statics + +#define MAIN_CLASS_NAME "MAIN" +#define AUTO_FILE_NAME "auto.p" + // consts const size_t pseudo_file_no__process=1; // forwards -class Temp_lang; class Methoded; class VMethodFrame; class VMail; class VForm; -class VResponse; class VCookie; class VStateless_class; class VConsole; +class WebSocket; +class VException; 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 size_t pa_file_size_limit; +class Code_cache; + /// Main workhorse. class Request: public PA_Object { - friend class Temp_lang; friend class Temp_connection; friend class Temp_request_self; friend class Temp_value_element; + friend class Temp_recursion; friend class Request_context_saver; friend class Exception_trace; + friend class VException; + friend class Code_cache_manager; public: class Trace { @@ -102,20 +121,26 @@ private: size_t bottom_index() { return fbottom; } void set_bottom_index(size_t abottom) { fbottom=abottom; } element_type bottom_value() { return get(bottom_index()); } + void set_range(size_t bottom, size_t top) { fbottom=bottom; fsize=top; } void clear() { - fused=fbottom=0; + fsize=fbottom=0; } 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 /// classes - HashString fclasses; + OrderedHashString fclasses; /// already used files to avoid cyclic uses HashString used_files; @@ -124,16 +149,18 @@ private: Array file_list; /// 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 Stack stack; +public: /// exception stack trace Exception_trace exception_trace; -public: bool allow_class_replace; @@ -142,8 +169,6 @@ public: VMethodFrame* method_frame; Value* rcontext; WContext* wcontext; - /// current language - String::Language flang; /// current connection SQL_Connection* fconnection; //@} @@ -157,14 +182,7 @@ private: public: uint register_file(String::Body file_spec); - struct Exception_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); + const char* get_exception_cstr(const Exception& e, VException& details); /// @see Stack::wipe_unused void wipe_unused_execution_stack() { @@ -183,15 +201,23 @@ public: ~Request(); /// global classes - HashString& classes() { return fclasses; } + OrderedHashString& classes() { return fclasses; } 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); } + bool add_class(const char* atype, VStateless_class *aclass); /** core request processing BEWARE: may throw exception to you: catch it! */ - void core(const char* config_filespec, bool config_fail_on_read_problem, bool header_only); + void core(const char* config_filespec, const String& amain_method_name = main_method_name, const String* amain_class_name = NULL); + + /// ^response:stream[part]: the headers go with the first part + void stream(Value& part); + + /// sends headers and sets response.started + void start(VResponse::Started mode); /// executes ops void execute(ArrayOperation& ops); // execute.C @@ -224,27 +250,31 @@ public: Value& construct(VStateless_class &class_value, const Method &method); - /// execute ops with anti-recoursion check - void recoursion_checked_execute(ArrayOperation& ops) { - if(++anti_endless_execute_recoursion==pa_execute_recoursion_limit) { - anti_endless_execute_recoursion=0; // give @exception a chance - throw Exception(PARSER_RUNTIME, 0, "call canceled - endless recursion detected"); - } + /// execute ops with anti-recursion check + void recursion_checked_execute(ArrayOperation& ops) { + if(++anti_endless_execute_recursion>=execute_recursion_limit) + recursion_limit_reached(); 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 classes_conf; @@ -346,6 +380,7 @@ public: // status read methods //{ for @conf[filespec] and @auto[filespec] 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); //} @@ -372,7 +407,7 @@ private: private: // compile.C - ArrayClass& compile(VStateless_class* aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset); + ArrayClass& compile(VStateless_class* aclass, const char* source, const String* main_alias, uint file_no, int line_no_offset, Code_cache* code); private: // execute.C @@ -381,10 +416,6 @@ private: // execute.C Value& get_element4call(Value& ncontext, const String& name); #endif -private: // defaults - - const String::Language fdefault_lang; - private: // mime types /// $MAIN:MIME-TYPES @@ -406,44 +437,54 @@ private: 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 class Request_context_saver { Request& fr; - /// exception stack trace - size_t exception_trace_top; - size_t exception_trace_bottom; /// execution stack size_t stack; - uint anti_endless_execute_recoursion; + int anti_endless_execute_recursion; /// contexts VMethodFrame* method_frame; Value* rcontext; WContext* wcontext; - /// current language - String::Language flang; /// current connection SQL_Connection* fconnection; public: Request_context_saver(Request& ar) : fr(ar), - exception_trace_top(ar.exception_trace.top_index()), - exception_trace_bottom(ar.exception_trace.bottom_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), rcontext(ar.rcontext), wcontext(ar.wcontext), - flang(ar.flang), fconnection(ar.fconnection) {} 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.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.flang=flang; fr.fconnection=fconnection; } }; @@ -514,24 +555,16 @@ public: ~Temp_value_element(); }; -// defines for externs - -#define EXCEPTION_HANDLED_PART_NAME "handled" - - -// externs - -extern const String main_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 - -#define MAIN_CLASS_NAME "MAIN" -#define AUTO_FILE_NAME "auto.p" +/// 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