Smaller cleanups and improvements.

This commit is contained in:
Relintai 2022-06-27 14:16:23 +02:00
parent 90c0a9937b
commit f819383012
4 changed files with 82 additions and 12 deletions

View File

@ -3,6 +3,8 @@
#include "web_node.h"
#include "web_server_request.h"
#include "http_session_manager.h"
NodePath WebServer::get_web_root_path() const {
return _web_root_path;
}
@ -27,10 +29,24 @@ void WebServer::set_web_root_bind(Node *root) {
_web_root = web_root;
}
HTTPSessionManager *WebServer::get_session_manager() {
return _session_manager;
}
void WebServer::set_session_manager(HTTPSessionManager *sess_man) {
_session_manager = sess_man;
}
Node *WebServer::get_session_manager_bind() {
return _session_manager;
}
void WebServer::set_session_manager_bind(Node *sess_man) {
_session_manager = Object::cast_to<HTTPSessionManager>(sess_man);
}
void WebServer::server_handle_request(Ref<WebServerRequest> request) {
ERR_FAIL_COND(!_web_root);
request->set_server(this);
request->server = this;
request->web_root = _web_root;
_rw_lock.read_lock();
@ -38,6 +54,19 @@ void WebServer::server_handle_request(Ref<WebServerRequest> request) {
_rw_lock.read_unlock();
}
void WebServer::start() {
call("_start");
}
void WebServer::stop() {
call("_stop");
}
void WebServer::_start() {
//look up root node, and sessionmanager, if not set.
}
void WebServer::_stop() {
}
WebServer::WebServer() {
_web_root = nullptr;
@ -52,10 +81,21 @@ void WebServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_web_root_path", "val"), &WebServer::set_web_root_path);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "web_root_path"), "set_web_root_path", "get_web_root_path");
ClassDB::bind_method(D_METHOD("get_web_root_bind"), &WebServer::get_web_root_bind);
ClassDB::bind_method(D_METHOD("set_web_root_bind", "val"), &WebServer::set_web_root_bind);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "web_root", PROPERTY_HINT_RESOURCE_TYPE, "Node"), "set_web_root_bind", "get_web_root_bind");
ClassDB::bind_method(D_METHOD("get_web_root"), &WebServer::get_web_root_bind);
ClassDB::bind_method(D_METHOD("set_web_root", "val"), &WebServer::set_web_root_bind);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "web_root", PROPERTY_HINT_RESOURCE_TYPE, "WebRoot"), "set_web_root", "get_web_root");
ClassDB::bind_method(D_METHOD("get_session_manager"), &WebServer::get_session_manager_bind);
ClassDB::bind_method(D_METHOD("set_session_manager", "val"), &WebServer::set_session_manager_bind);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "session_manager", PROPERTY_HINT_RESOURCE_TYPE, "HTTPSessionManager"), "set_session_manager", "get_session_manager");
ClassDB::bind_method(D_METHOD("server_handle_request", "request"), &WebServer::server_handle_request);
ClassDB::bind_method(D_METHOD("request_write_lock"), &WebServer::request_write_lock);
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);
}

View File

@ -6,6 +6,8 @@
class WebServerRequest;
class WebNode;
class WebRoot;
class HTTPSessionManager;
class WebServer : public Node {
GDCLASS(WebServer, Node);
@ -20,10 +22,22 @@ public:
Node *get_web_root_bind();
void set_web_root_bind(Node *root);
HTTPSessionManager *get_session_manager();
void set_session_manager(HTTPSessionManager *sess_man);
Node *get_session_manager_bind();
void set_session_manager_bind(Node *sess_man);
void server_handle_request(Ref<WebServerRequest> request);
void request_write_lock();
void start();
void stop();
virtual void _start();
virtual void _stop();
WebServer();
~WebServer();
@ -33,6 +47,8 @@ protected:
NodePath _web_root_path;
WebNode *_web_root;
HTTPSessionManager *_session_manager;
bool _write_lock_requested;
RWLock _rw_lock;
};

View File

@ -6,8 +6,8 @@
#include "http_session.h"
#include "session_manager.h"
#include "web_root.h"
#include "http_session_manager.h"
#include "web_node.h"
#include "web_permission.h"
@ -79,7 +79,11 @@ Ref<HTTPSession> WebServerRequest::get_or_create_session() {
return session;
}
session = SessionManager::get_singleton()->create_session();
HTTPSessionManager *sm = server->get_session_manager();
ERR_FAIL_COND_V(!sm, session);
session = sm->create_session();
return session;
}
@ -119,7 +123,11 @@ void WebServerRequest::set_csrf_token(const String &value) {
if (session.is_valid()) {
session->add("csrf_token", value);
SessionManager::get_singleton()->save_session(session);
HTTPSessionManager *sm = server->get_session_manager();
ERR_FAIL_COND(!sm);
sm->save_session(session);
}
}
@ -275,15 +283,15 @@ String WebServerRequest::get_path(const bool beginning_slash, const bool end_sla
return path;
}
String WebServerRequest::path_full() const {
String WebServerRequest::get_path_full() const {
return _full_path;
}
String WebServerRequest::path_segment(const uint32_t i) const {
String WebServerRequest::get_path_segment(const uint32_t i) const {
return _path_stack[i];
}
String WebServerRequest::current_path_segment() const {
String WebServerRequest::get_current_path_segment() const {
if (_path_stack_pointer >= _path_stack.size()) {
// for convenience
static const String e_str = "";
@ -293,7 +301,7 @@ String WebServerRequest::current_path_segment() const {
return _path_stack[_path_stack_pointer];
}
String WebServerRequest::next_path_segment() const {
String WebServerRequest::get_next_path_segment() const {
int pst = _path_stack_pointer + 1;
if (pst >= _path_stack.size()) {
@ -427,6 +435,8 @@ WebServerRequest::WebServerRequest() {
//compiled_body.clear();
//data.clear();
server = nullptr;
web_root = nullptr;
}
WebServerRequest::~WebServerRequest() {

View File

@ -14,6 +14,7 @@ class WebServer;
class WebServerCookie;
class HTTPSession;
class WebPermission;
class WebNode;
class WebServerRequest : public Reference {
GDCLASS(WebServerRequest, Reference);
@ -108,11 +109,14 @@ public:
WebServer *get_server();
Node *get_server_bind();
WebNode *get_web_root();
Node *get_web_root_bind();
WebServerRequest();
~WebServerRequest();
WebServer *server;
WebNode *web_root;
String head;
String body;