From df2859b9d01a88c04aff1823748e5ff44d142e5b Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 22 Dec 2023 12:38:38 +0100 Subject: [PATCH] Added a way to unregister connections and get out their raw connection data from the http server. --- .../http_server_simple/http_server_simple.cpp | 76 +++++++++++++++++++ .../http_server_simple/http_server_simple.h | 3 + .../http_server_simple/web_server_simple.cpp | 4 + .../http_server_simple/web_server_simple.h | 2 + modules/web/http/web_server.cpp | 12 +++ modules/web/http/web_server.h | 3 + 6 files changed, 100 insertions(+) diff --git a/modules/http_server_simple/http_server_simple.cpp b/modules/http_server_simple/http_server_simple.cpp index ddacccc99..d7d146b20 100644 --- a/modules/http_server_simple/http_server_simple.cpp +++ b/modules/http_server_simple/http_server_simple.cpp @@ -581,6 +581,80 @@ void HTTPServerSimple::poll() { } } +Dictionary HTTPServerSimple::unregister_connection_for_request(const Ref &request) { + Ref srequest = request; + + Dictionary d; + d["result"] = ERR_DOES_NOT_EXIST; + + if (!srequest.is_valid()) { + return d; + } + + bool found = false; + + _connections_lock.write_lock(); + + List>::Element *e = _connections.front(); + + while (e) { + Ref c = e->get(); + + if (c->_current_request == srequest) { + d["result"] = OK; + + d["use_ssl"] = c->use_ssl; + d["key"] = c->key; + + d["tcp"] = c->tcp; + d["ssl"] = c->ssl; + d["peer"] = c->peer; + + c->_closed = true; + + found = true; + + _connections.erase(e); + + break; + } + + e = e->next(); + } + + if (!found) { + for (int i = 0; i < _threads.size(); ++i) { + ServerWorkerThread *t = _threads[i]; + + Ref &c = t->current_connection; + + if (!c.is_valid()) { + continue; + } + + if (c->_current_request == srequest) { + d["result"] = OK; + + d["use_ssl"] = c->use_ssl; + d["key"] = c->key; + + d["tcp"] = c->tcp; + d["ssl"] = c->ssl; + d["peer"] = c->peer; + + //So the thread will not put it back to the connections array + c->_closed = true; + + break; + } + } + } + + _connections_lock.write_unlock(); + + return d; +} + HTTPServerSimple::HTTPServerSimple() { _web_server = nullptr; @@ -716,6 +790,7 @@ void HTTPServerSimple::_worker_thread_func(void *data) { } Ref c = e->get(); + context->current_connection = c; server->_connections.pop_front(); server->_connections_lock.write_unlock(); @@ -732,6 +807,7 @@ void HTTPServerSimple::_worker_thread_func(void *data) { server->_connections_lock.write_lock(); server->_connections.push_back(c); + context->current_connection.unref(); server->_connections_lock.write_unlock(); } diff --git a/modules/http_server_simple/http_server_simple.h b/modules/http_server_simple/http_server_simple.h index f6b203af4..64bbf05a8 100644 --- a/modules/http_server_simple/http_server_simple.h +++ b/modules/http_server_simple/http_server_simple.h @@ -109,6 +109,8 @@ public: bool is_listening() const; void poll(); + Dictionary unregister_connection_for_request(const Ref &request); + HTTPServerSimple(); ~HTTPServerSimple(); @@ -145,6 +147,7 @@ private: Thread *thread; Semaphore *semaphore; Ref server; + Ref current_connection; bool running; bool working; diff --git a/modules/http_server_simple/web_server_simple.cpp b/modules/http_server_simple/web_server_simple.cpp index aded79243..8f62a0cd1 100644 --- a/modules/http_server_simple/web_server_simple.cpp +++ b/modules/http_server_simple/web_server_simple.cpp @@ -223,6 +223,10 @@ void WebServerSimple::_stop() { _server_lock.unlock(); } +Dictionary WebServerSimple::_unregister_connection_for_request(const Ref &request) { + return _server->unregister_connection_for_request(request); +} + WebServerSimple::WebServerSimple() { _max_request_size_type = MAX_REQUEST_SIZE_TYPE_MEGA_BYTE; _max_request_size = 3; diff --git a/modules/http_server_simple/web_server_simple.h b/modules/http_server_simple/web_server_simple.h index 2057359e9..ba852f419 100644 --- a/modules/http_server_simple/web_server_simple.h +++ b/modules/http_server_simple/web_server_simple.h @@ -103,6 +103,8 @@ public: void _start(); void _stop(); + Dictionary _unregister_connection_for_request(const Ref &request); + WebServerSimple(); ~WebServerSimple(); diff --git a/modules/web/http/web_server.cpp b/modules/web/http/web_server.cpp index 8227ddeef..0cc90063b 100644 --- a/modules/web/http/web_server.cpp +++ b/modules/web/http/web_server.cpp @@ -103,6 +103,13 @@ void WebServer::_stop() { _is_running = false; } +Dictionary WebServer::unregister_connection_for_request(const Ref &request) { + return call("_unregister_connection_for_request", request); +} +Dictionary WebServer::_unregister_connection_for_request(const Ref &request) { + return Dictionary(); +} + String WebServer::get_configuration_warning() const { int webnode_count = 0; int session_manager_count = 0; @@ -184,6 +191,11 @@ void WebServer::_bind_methods() { ClassDB::bind_method(D_METHOD("_start"), &WebServer::_start); ClassDB::bind_method(D_METHOD("_stop"), &WebServer::_stop); + BIND_VMETHOD(MethodInfo(Variant::DICTIONARY, "_unregister_connection_for_request", + PropertyInfo(Variant::OBJECT, "request", PROPERTY_HINT_RESOURCE_TYPE, "WebServerRequest"))); + ClassDB::bind_method(D_METHOD("unregister_connection_for_request", "request"), &WebServer::unregister_connection_for_request); + ClassDB::bind_method(D_METHOD("_unregister_connection_for_request", "request"), &WebServer::_unregister_connection_for_request); + BIND_CONSTANT(NOTIFICATION_WEB_SERVER_STARTED); BIND_CONSTANT(NOTIFICATION_WEB_SERVER_STOPPED); BIND_CONSTANT(NOTIFICATION_WEB_SERVER_WRITE_LOCK_ACQUIRED); diff --git a/modules/web/http/web_server.h b/modules/web/http/web_server.h index f5d19e771..b8ee6c289 100644 --- a/modules/web/http/web_server.h +++ b/modules/web/http/web_server.h @@ -75,6 +75,9 @@ public: virtual void _start(); virtual void _stop(); + Dictionary unregister_connection_for_request(const Ref &request); + virtual Dictionary _unregister_connection_for_request(const Ref &request); + String get_configuration_warning() const; WebServer();