pandemonium_engine/modules/web/http/web_server.cpp

160 lines
3.6 KiB
C++
Raw Normal View History

#include "web_server.h"
#include "core/object/class_db.h"
2022-06-26 21:34:29 +02:00
#include "web_node.h"
#include "web_server_request.h"
2022-06-27 14:16:23 +02:00
#include "http_session_manager.h"
bool WebServer::get_is_running() {
return _is_running;
2022-06-26 21:34:29 +02:00
}
WebNode *WebServer::get_web_root() {
return _web_root;
}
2022-06-27 14:16:23 +02:00
HTTPSessionManager *WebServer::get_session_manager() {
return _session_manager;
}
2022-06-27 01:03:01 +02:00
void WebServer::server_handle_request(Ref<WebServerRequest> request) {
ERR_FAIL_COND(!_web_root);
2022-07-21 23:25:04 +02:00
request->_set_server(this);
request->_set_web_root(_web_root);
2022-06-27 01:03:01 +02:00
_rw_lock.read_lock();
_web_root->handle_request_main(request);
_rw_lock.read_unlock();
}
void WebServer::request_write_lock() {
_write_lock_requested = true;
}
void WebServer::refresh_root() {
_web_root = nullptr;
_session_manager = nullptr;
for (int i = 0; i < get_child_count(); ++i) {
if (!_web_root) {
_web_root = Object::cast_to<WebNode>(get_child(i));
}
if (!_session_manager) {
_session_manager = Object::cast_to<HTTPSessionManager>(get_child(i));
}
}
}
2022-06-27 14:16:23 +02:00
void WebServer::start() {
call("_start");
}
void WebServer::stop() {
call("_stop");
}
void WebServer::_start() {
if (_is_running) {
return;
2022-06-27 15:42:40 +02:00
}
_is_running = true;
refresh_root();
2022-06-27 14:16:23 +02:00
}
void WebServer::_stop() {
if (!_is_running) {
return;
}
_is_running = false;
}
String WebServer::get_configuration_warning() const {
int webnode_count = 0;
int session_manager_count = 0;
for (int i = 0; i < get_child_count(); ++i) {
WebNode *wn = Object::cast_to<WebNode>(get_child(i));
if (wn) {
++webnode_count;
}
if (!_session_manager) {
HTTPSessionManager *sm = Object::cast_to<HTTPSessionManager>(get_child(i));
if (sm) {
++session_manager_count;
}
}
}
String err;
if (webnode_count == 0) {
err += "You need one (and only one) WebNode as the child for the webserver to work. (A WebRoot Node is recommended by default.)";
} else if (webnode_count > 1) {
err += "You have more than one root WebNode as the child of the server. Please move them under a new common WebNode, else the server will only use the first one!";
}
if (session_manager_count > 1) {
if (err != "") {
err += "\n";
}
err += "You have more than one HTTPSessionManager nodes as child, the server will only be able use the first one. Please remove the other.";
}
return err;
2022-06-27 14:16:23 +02:00
}
2022-06-26 21:34:29 +02:00
WebServer::WebServer() {
_is_running = false;
_web_root = nullptr;
_session_manager = nullptr;
2022-06-26 21:34:29 +02:00
_write_lock_requested = false;
set_process_internal(true);
}
WebServer::~WebServer() {
}
2022-06-26 21:34:29 +02:00
void WebServer::_notification(int p_what) {
if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
if (_write_lock_requested) {
_rw_lock.write_lock();
notification(NOTIFICATION_WEB_SERVER_WRITE_LOCK_ACQUIRED);
//the root could have changed.
refresh_root();
_rw_lock.write_unlock();
_write_lock_requested = false;
}
}
}
2022-06-26 21:34:29 +02:00
void WebServer::_bind_methods() {
2022-07-22 12:05:05 +02:00
ClassDB::bind_method(D_METHOD("get_web_root"), &WebServer::get_web_root);
ClassDB::bind_method(D_METHOD("get_session_manager"), &WebServer::get_session_manager);
2022-06-26 21:34:29 +02:00
2022-06-27 01:03:01 +02:00
ClassDB::bind_method(D_METHOD("server_handle_request", "request"), &WebServer::server_handle_request);
2022-06-26 21:34:29 +02:00
ClassDB::bind_method(D_METHOD("request_write_lock"), &WebServer::request_write_lock);
2022-06-27 14:16:23 +02:00
BIND_VMETHOD(MethodInfo("_start"));
BIND_VMETHOD(MethodInfo("_stop"));
ClassDB::bind_method(D_METHOD("start"), &WebServer::start);
ClassDB::bind_method(D_METHOD("stop"), &WebServer::stop);
ClassDB::bind_method(D_METHOD("_start"), &WebServer::_start);
ClassDB::bind_method(D_METHOD("_stop"), &WebServer::_stop);
BIND_CONSTANT(NOTIFICATION_WEB_SERVER_STARTED);
BIND_CONSTANT(NOTIFICATION_WEB_SERVER_STOPPED);
BIND_CONSTANT(NOTIFICATION_WEB_SERVER_WRITE_LOCK_ACQUIRED);
2022-06-26 21:34:29 +02:00
}