From 0bbff6ae3bc8c1bd5d51f94afbf023d0141360c7 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 1 Jul 2022 18:50:56 +0200 Subject: [PATCH] Small improvements / fixes. --- modules/web/http/web_node.cpp | 32 ++++++++++++++++++++++++-------- modules/web/http/web_node.h | 2 ++ modules/web/http/web_root.cpp | 2 +- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/modules/web/http/web_node.cpp b/modules/web/http/web_node.cpp index 9f7d337a7..2d5fd8e30 100644 --- a/modules/web/http/web_node.cpp +++ b/modules/web/http/web_node.cpp @@ -4,6 +4,7 @@ #include "core/error_macros.h" #include "core/object.h" +#include "core/print_string.h" #include "http_server_enums.h" #include "web_server_request.h" @@ -22,11 +23,6 @@ #include "database/table_builder.h" #endif -/* -void WebNode::update() { -} -*/ - String WebNode::get_uri_segment() { return _uri_segment; } @@ -43,6 +39,8 @@ String WebNode::get_full_uri(const bool slash_at_the_end) { } String WebNode::get_full_uri_parent(const bool slash_at_the_end) { + _rw_lock.read_lock(); + String uri = "/"; WebNode *n = get_parent_webnode(); @@ -57,6 +55,8 @@ String WebNode::get_full_uri_parent(const bool slash_at_the_end) { n = n->get_parent_webnode(); } + _rw_lock.read_unlock(); + if (!slash_at_the_end) { uri.resize(uri.length() - 1); } @@ -232,7 +232,7 @@ void WebNode::_create_default_entries() { } void WebNode::migrate(const bool clear, const bool seed_db) { - _migrate(clear, seed_db); + call("_migrate", clear, seed_db); for (int i = 0; i < get_child_count(); ++i) { WebNode *c = Object::cast_to(get_child(i)); @@ -261,16 +261,20 @@ void WebNode::_migrate(const bool clear, const bool seed_db) { bool WebNode::try_route_request_to_children(Ref request) { WebNode *handler = nullptr; + _handler_map_lock.read_lock(); + // if (path == "/") { if (request->get_path_segment_count() == 0) { // quick shortcut handler = _index_node; } else { - const String &main_route = request->get_current_path_segment(); + String main_route = request->get_current_path_segment(); handler = _node_route_map[main_route]; } + _handler_map_lock.read_unlock(); + if (!handler) { return false; } @@ -284,6 +288,8 @@ bool WebNode::try_route_request_to_children(Ref request) { WebNode *WebNode::get_request_handler_child(Ref request) { WebNode *handler = nullptr; + _handler_map_lock.read_lock(); + // if (path == "/") { if (request->get_path_segment_count() == 0) { // quick shortcut @@ -293,10 +299,14 @@ WebNode *WebNode::get_request_handler_child(Ref request) { handler = _node_route_map[main_route]; } + _handler_map_lock.read_unlock(); + return handler; } void WebNode::build_handler_map() { + _handler_map_lock.write_lock(); + _index_node = nullptr; _node_route_map.clear(); @@ -317,16 +327,22 @@ void WebNode::build_handler_map() { _index_node = c; } else { - ERR_CONTINUE_MSG(_node_route_map[uri_segment], "You have multiple of the same uri! URI:" + uri_segment); + ERR_CONTINUE_MSG(_node_route_map[uri_segment], "You have multiple of the same uri! URI: " + uri_segment); _node_route_map[uri_segment] = c; } } + + _handler_map_lock.write_unlock(); } void WebNode::clear_handlers() { + _handler_map_lock.write_lock(); + _index_node = nullptr; _node_route_map.clear(); + + _handler_map_lock.write_unlock(); } void WebNode::request_write_lock() { diff --git a/modules/web/http/web_node.h b/modules/web/http/web_node.h index b68029469..2cb6412a3 100644 --- a/modules/web/http/web_node.h +++ b/modules/web/http/web_node.h @@ -2,6 +2,7 @@ #define WEB_NODE_H #include "core/hash_map.h" +#include "core/os/rw_lock.h" #include "core/reference.h" #include "core/variant.h" #include "scene/main/node.h" @@ -122,6 +123,7 @@ protected: bool _routing_enabled; WebNode *_index_node; HashMap _node_route_map; + RWLock _handler_map_lock; Ref _web_permission; diff --git a/modules/web/http/web_root.cpp b/modules/web/http/web_root.cpp index aa386bdf9..d8fb53703 100644 --- a/modules/web/http/web_root.cpp +++ b/modules/web/http/web_root.cpp @@ -133,7 +133,7 @@ bool WebRoot::process_middlewares(Ref request) { } bool WebRoot::try_send_wwwroot_file(Ref request) { - const String &path = request->get_path_full(); + String path = request->get_path_full(); if (_www_root_file_cache->wwwroot_has_file(path)) { send_file(path, request);