Added a way to unregister connections and get out their raw connection data from the http server.

This commit is contained in:
Relintai 2023-12-22 12:38:38 +01:00
parent cf76e6debf
commit df2859b9d0
6 changed files with 100 additions and 0 deletions

View File

@ -581,6 +581,80 @@ void HTTPServerSimple::poll() {
}
}
Dictionary HTTPServerSimple::unregister_connection_for_request(const Ref<WebServerRequest> &request) {
Ref<SimpleWebServerRequest> srequest = request;
Dictionary d;
d["result"] = ERR_DOES_NOT_EXIST;
if (!srequest.is_valid()) {
return d;
}
bool found = false;
_connections_lock.write_lock();
List<Ref<HTTPServerConnection>>::Element *e = _connections.front();
while (e) {
Ref<HTTPServerConnection> 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<HTTPServerConnection> &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<HTTPServerConnection> 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();
}

View File

@ -109,6 +109,8 @@ public:
bool is_listening() const;
void poll();
Dictionary unregister_connection_for_request(const Ref<WebServerRequest> &request);
HTTPServerSimple();
~HTTPServerSimple();
@ -145,6 +147,7 @@ private:
Thread *thread;
Semaphore *semaphore;
Ref<HTTPServerSimple> server;
Ref<HTTPServerConnection> current_connection;
bool running;
bool working;

View File

@ -223,6 +223,10 @@ void WebServerSimple::_stop() {
_server_lock.unlock();
}
Dictionary WebServerSimple::_unregister_connection_for_request(const Ref<WebServerRequest> &request) {
return _server->unregister_connection_for_request(request);
}
WebServerSimple::WebServerSimple() {
_max_request_size_type = MAX_REQUEST_SIZE_TYPE_MEGA_BYTE;
_max_request_size = 3;

View File

@ -103,6 +103,8 @@ public:
void _start();
void _stop();
Dictionary _unregister_connection_for_request(const Ref<WebServerRequest> &request);
WebServerSimple();
~WebServerSimple();

View File

@ -103,6 +103,13 @@ void WebServer::_stop() {
_is_running = false;
}
Dictionary WebServer::unregister_connection_for_request(const Ref<WebServerRequest> &request) {
return call("_unregister_connection_for_request", request);
}
Dictionary WebServer::_unregister_connection_for_request(const Ref<WebServerRequest> &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);

View File

@ -75,6 +75,9 @@ public:
virtual void _start();
virtual void _stop();
Dictionary unregister_connection_for_request(const Ref<WebServerRequest> &request);
virtual Dictionary _unregister_connection_for_request(const Ref<WebServerRequest> &request);
String get_configuration_warning() const;
WebServer();