From ba1ea3c62a90af6098e3dd5ea9402072d8215f0e Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 27 Jun 2022 01:15:01 +0200 Subject: [PATCH] Cleaned up WebRoot aswell. --- modules/web/http/web_root.cpp | 67 +++++++++++++++++++---------------- modules/web/http/web_root.h | 55 +++++++++++++++------------- 2 files changed, 67 insertions(+), 55 deletions(-) diff --git a/modules/web/http/web_root.cpp b/modules/web/http/web_root.cpp index 91136eee1..c18fce197 100644 --- a/modules/web/http/web_root.cpp +++ b/modules/web/http/web_root.cpp @@ -1,17 +1,24 @@ #include "web_root.h" -#include -#include +#include "web_server_request.h" -#include "request.h" - -#include "web/file_cache.h" - -#include +#include "../file_cache.h" #include "web_permission.h" -#include -#include + +String WebRoot::get_default_error_404_body() { + return default_error_404_body; +} +void WebRoot::set_default_error_404_body(const String &val) { + default_error_404_body = val; +} + +String WebRoot::get_default_generic_error_body() { + return default_generic_error_body; +} +void WebRoot::set_default_generic_error_body(const String &val) { + default_generic_error_body = val; +} void WebRoot::setup() { setup_error_handlers(); @@ -58,17 +65,17 @@ void WebRoot::setup_middleware() { // _middlewares.push_back(csrf_middleware); } -void WebRoot::default_fallback_error_handler(Request *request, int error_code) { +void WebRoot::default_fallback_error_handler(Ref request, int error_code) { request->compiled_body = default_generic_error_body; request->send(); } -void WebRoot::default_404_error_handler(Request *request, int error_code) { +void WebRoot::default_404_error_handler(Ref request, int error_code) { request->compiled_body = default_error_404_body; request->send(); } -void WebRoot::handle_request_main(Request *request) { +void WebRoot::handle_request_main(Ref request) { if (process_middlewares(request)) { return; } @@ -95,8 +102,8 @@ void WebRoot::handle_request_main(Request *request) { } } -void WebRoot::handle_error_send_request(Request *request, const int error_code) { - std::function func = error_handler_map[error_code]; +void WebRoot::handle_error_send_request(Ref request, const int error_code) { + void (*func)(Ref, int) = error_handler_map[error_code]; if (!func) { if (!default_error_handler_func) { @@ -111,9 +118,9 @@ void WebRoot::handle_error_send_request(Request *request, const int error_code) func(request, error_code); } -bool WebRoot::process_middlewares(Request *request) { +bool WebRoot::process_middlewares(Ref request) { for (int i = 0; i < _middlewares.size(); ++i) { - if (_middlewares[i]->on_before_handle_request_main(request)) { + if (_middlewares.write[i]->on_before_handle_request_main(request)) { // handled return true; } @@ -122,7 +129,7 @@ bool WebRoot::process_middlewares(Request *request) { return false; } -bool WebRoot::try_send_wwwroot_file(Request *request) { +bool WebRoot::try_send_wwwroot_file(Ref request) { const String &path = request->get_path_full(); if (FileCache::get_singleton()->wwwroot_has_file(path)) { @@ -134,44 +141,44 @@ bool WebRoot::try_send_wwwroot_file(Request *request) { return false; } -void WebRoot::send_file(const String &path, Request *request) { +void WebRoot::send_file(const String &path, Ref request) { String fp = FileCache::get_singleton()->wwwroot + path; request->send_file(fp); } -void WebRoot::register_request_update(Request *request) { - std::lock_guard lock(_update_registered_requests_mutex); - +void WebRoot::register_request_update(Ref request) { + _update_registered_requests_mutex.lock(); _update_registered_requests.push_back(request); + _update_registered_requests_mutex.unlock(); } -void WebRoot::unregister_request_update(Request *request) { - std::lock_guard lock(_update_registered_requests_mutex); +void WebRoot::unregister_request_update(Ref request) { + _update_registered_requests_mutex.lock(); std::size_t s = _update_registered_requests.size(); for (std::size_t i = 0; i < s; ++i) { - Request *r = _update_registered_requests[i]; + Ref r = _update_registered_requests[i]; if (r == request) { - _update_registered_requests[i] = _update_registered_requests[s - 1]; - - _update_registered_requests.pop_back(); + _update_registered_requests.remove(i); + _update_registered_requests_mutex.unlock(); return; } } + _update_registered_requests_mutex.unlock(); } void WebRoot::update() { for (std::size_t i = 0; i < _update_registered_requests.size(); ++i) { - Request *r = _update_registered_requests[i]; + Ref r = _update_registered_requests[i]; r->update(); } } -WebRoot::WebRoot() : - WebNode() { +WebRoot::WebRoot() { + default_error_handler_func = nullptr; } WebRoot::~WebRoot() { diff --git a/modules/web/http/web_root.h b/modules/web/http/web_root.h index c9f8fd690..057c9f60c 100644 --- a/modules/web/http/web_root.h +++ b/modules/web/http/web_root.h @@ -1,59 +1,64 @@ #ifndef WEB_ROOT_H #define WEB_ROOT_H -#include "core/containers/vector.h" -#include "core/string.h" -#include "mutex" -#include -#include +#include "core/hash_map.h" +#include "core/os/mutex.h" +#include "core/ustring.h" +#include "core/vector.h" #include "web_node.h" -#include "middleware.h" +#include "web_server_middleware.h" -class Request; +class WebServerRequest; // TODO FileCache -> set up, for this webroot, don't use singleton class WebRoot : public WebNode { - RCPP_OBJECT(WebRoot, WebNode); + GDCLASS(WebRoot, WebNode); public: - static String default_error_404_body; - static String default_generic_error_body; + String get_default_error_404_body(); + void set_default_error_404_body(const String &val); - void handle_request_main(Request *request); - void handle_error_send_request(Request *request, const int error_code); + String get_default_generic_error_body(); + void set_default_generic_error_body(const String &val); - bool process_middlewares(Request *request); - bool try_send_wwwroot_file(Request *request); - void send_file(const String &path, Request *request); + void handle_request_main(Ref request); + void handle_error_send_request(Ref request, const int error_code); - static void default_fallback_error_handler(Request *request, int error_code); - static void default_404_error_handler(Request *request, int error_code); + bool process_middlewares(Ref request); + bool try_send_wwwroot_file(Ref request); + void send_file(const String &path, Ref request); + + static void default_fallback_error_handler(Ref request, int error_code); + static void default_404_error_handler(Ref request, int error_code); virtual void setup(); virtual void setup_error_handlers(); virtual void setup_middleware(); - void default_routing_middleware(Object *instance, Request *request); + void default_routing_middleware(Object *instance, Ref request); - void register_request_update(Request *request); - void unregister_request_update(Request *request); + void register_request_update(Ref request); + void unregister_request_update(Ref request); void update(); WebRoot(); - virtual ~WebRoot(); + ~WebRoot(); + + static String default_error_404_body; + static String default_generic_error_body; public: Vector> _middlewares; - std::map> error_handler_map; - std::function default_error_handler_func; + HashMap, int)> error_handler_map; + void (*default_error_handler_func)(Ref, int); protected: - std::mutex _update_registered_requests_mutex; - std::vector _update_registered_requests; + Mutex _update_registered_requests_mutex; + Vector> _update_registered_requests; }; #endif