Added an option to defer unmathed requests to index nodes in WebNode.

This commit is contained in:
Relintai 2024-05-04 16:40:23 +02:00
parent 602d8a6836
commit d880ff660f
2 changed files with 25 additions and 1 deletions

View File

@ -58,7 +58,7 @@ void WebNode::set_uri_segment(const String &val) {
} }
String WebNode::get_full_uri(const bool slash_at_the_end) { String WebNode::get_full_uri(const bool slash_at_the_end) {
// Special case for WebRoots for ease of use // Special case for index nodes for ease of use
if (_uri_segment == "/") { if (_uri_segment == "/") {
if (slash_at_the_end) { if (slash_at_the_end) {
return _uri_segment; return _uri_segment;
@ -126,6 +126,13 @@ void WebNode::set_routing_enabled(const bool value) {
} }
} }
bool WebNode::get_send_unmatched_request_to_index() const {
return _send_unmatched_request_to_index;
}
void WebNode::set_send_unmatched_request_to_index(const bool val) {
_send_unmatched_request_to_index = val;
}
#ifdef MODULE_DATABASE_ENABLED #ifdef MODULE_DATABASE_ENABLED
String WebNode::get_database_table_name() { String WebNode::get_database_table_name() {
@ -342,6 +349,13 @@ bool WebNode::try_route_request_to_children(Ref<WebServerRequest> request) {
_handler_map_lock.read_unlock(); _handler_map_lock.read_unlock();
if (!handler) { if (!handler) {
if (_send_unmatched_request_to_index && _index_node) {
// Don't push path here!
// request->push_path();
_index_node->handle_request_main(request);
return true;
}
return false; return false;
} }
@ -497,6 +511,8 @@ WebNode::WebNode() {
//#endif //#endif
_routing_enabled = true; _routing_enabled = true;
_send_unmatched_request_to_index = false;
_index_node = nullptr; _index_node = nullptr;
#ifdef MODULE_DATABASE_ENABLED #ifdef MODULE_DATABASE_ENABLED
@ -526,6 +542,10 @@ void WebNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_routing_enabled", "val"), &WebNode::set_routing_enabled); ClassDB::bind_method(D_METHOD("set_routing_enabled", "val"), &WebNode::set_routing_enabled);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "routing_enabled"), "set_routing_enabled", "get_routing_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "routing_enabled"), "set_routing_enabled", "get_routing_enabled");
ClassDB::bind_method(D_METHOD("get_send_unmatched_request_to_index"), &WebNode::get_send_unmatched_request_to_index);
ClassDB::bind_method(D_METHOD("set_send_unmatched_request_to_index", "val"), &WebNode::set_send_unmatched_request_to_index);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "send_unmatched_request_to_index"), "set_send_unmatched_request_to_index", "get_send_unmatched_request_to_index");
#ifdef MODULE_DATABASE_ENABLED #ifdef MODULE_DATABASE_ENABLED
ClassDB::bind_method(D_METHOD("get_database_table_name"), &WebNode::get_database_table_name); ClassDB::bind_method(D_METHOD("get_database_table_name"), &WebNode::get_database_table_name);
ClassDB::bind_method(D_METHOD("set_database_table_name", "val"), &WebNode::set_database_table_name); ClassDB::bind_method(D_METHOD("set_database_table_name", "val"), &WebNode::set_database_table_name);

View File

@ -73,6 +73,9 @@ public:
bool get_routing_enabled(); bool get_routing_enabled();
void set_routing_enabled(const bool value); void set_routing_enabled(const bool value);
bool get_send_unmatched_request_to_index() const;
void set_send_unmatched_request_to_index(const bool val);
#ifdef MODULE_DATABASE_ENABLED #ifdef MODULE_DATABASE_ENABLED
String get_database_table_name(); String get_database_table_name();
void set_database_table_name(const String &val); void set_database_table_name(const String &val);
@ -151,6 +154,7 @@ protected:
#endif #endif
bool _routing_enabled; bool _routing_enabled;
bool _send_unmatched_request_to_index;
WebNode *_index_node; WebNode *_index_node;
HashMap<String, WebNode *> _node_route_map; HashMap<String, WebNode *> _node_route_map;
RWLock _handler_map_lock; RWLock _handler_map_lock;