diff --git a/modules/web/SCsub b/modules/web/SCsub index 56387c0ba..bc48f9f68 100644 --- a/modules/web/SCsub +++ b/modules/web/SCsub @@ -10,6 +10,18 @@ sources = [ "file_cache.cpp", + "http/csrf_token.cpp", + "http/http_server_enums.cpp", + "http/http_session.cpp", + "http/http_session_manager.cpp", + "http/web_node.cpp", + "http/web_permission.cpp", + "http/web_root.cpp", + "http/web_server.cpp", + "http/web_server_cookie.cpp", + "http/web_server_middleware.cpp", + "http/web_server_request.cpp", + "http/http_server_enums.cpp", "http/web_server_cookie.cpp", diff --git a/modules/web/config.py b/modules/web/config.py index 6639f0023..b869a2b48 100644 --- a/modules/web/config.py +++ b/modules/web/config.py @@ -11,11 +11,22 @@ def configure(env): def get_doc_classes(): return [ - #"WorldArea", - "FileCache", + "HTTPServerEnums", "WebServerCookie", + "CSRFTokenWebServerMiddleware", + "HTTPSession", + "HTTPSessionManager", + "SessionSetupWebServerMiddleware", + "WebNode", + "WebPermission", + "WebRoot", + "WebServer", + "WebServerCookie", + "WebServerMiddleware", + "WebServerRequest", + "HTMLBuilder", "HTMLTag", ] diff --git a/modules/web/http/http_session_manager.cpp b/modules/web/http/http_session_manager.cpp index 6da16b52f..4bfc2b31c 100644 --- a/modules/web/http/http_session_manager.cpp +++ b/modules/web/http/http_session_manager.cpp @@ -1,8 +1,6 @@ #include "http_session_manager.h" #include "http_session.h" -#include -#include #if DATABASES_ENABLED #include "database/database_manager.h" @@ -11,11 +9,11 @@ #include "database/table_builder.h" #endif +#include "web_server.h" +#include "web_server_cookie.h" #include "web_server_request.h" -#include "web_server_cookie.h" - -void HTTPSessionManager::add_session(Ref &session) { +void HTTPSessionManager::add_session(Ref session) { if (!session.is_valid()) { printf("HTTPSessionManager::add_session: ERROR, session is null!\n"); return; @@ -29,7 +27,7 @@ void HTTPSessionManager::add_session(Ref &session) { _mutex.unlock(); } -void HTTPSessionManager::remove_session(Ref &session) { +void HTTPSessionManager::remove_session(Ref session) { if (!session.is_valid()) { printf("HTTPSessionManager::remove_session: ERROR, session is null!\n"); return; @@ -88,7 +86,7 @@ void HTTPSessionManager::delete_session(const String &session_id) { #endif } -void HTTPSessionManager::save_session(Ref &session) { +void HTTPSessionManager::save_session(Ref session) { #if DATABASES_ENABLED Ref b = DatabaseManager::get_singleton()->ddb->get_query_builder(); @@ -276,6 +274,16 @@ HTTPSessionManager::~HTTPSessionManager() { } void HTTPSessionManager::_bind_methods() { + ClassDB::bind_method(D_METHOD("add_session", "session"), &HTTPSessionManager::add_session); + ClassDB::bind_method(D_METHOD("remove_session", "session"), &HTTPSessionManager::remove_session); + ClassDB::bind_method(D_METHOD("delete_session", "session_id"), &HTTPSessionManager::delete_session); + ClassDB::bind_method(D_METHOD("save_session", "session"), &HTTPSessionManager::save_session); + ClassDB::bind_method(D_METHOD("get_session", "session_id"), &HTTPSessionManager::get_session); + ClassDB::bind_method(D_METHOD("create_session"), &HTTPSessionManager::create_session); + + ClassDB::bind_method(D_METHOD("load_sessions"), &HTTPSessionManager::load_sessions); + ClassDB::bind_method(D_METHOD("clear"), &HTTPSessionManager::clear); + ClassDB::bind_method(D_METHOD("generate_session_id", "base"), &HTTPSessionManager::generate_session_id, ""); } bool SessionSetupWebServerMiddleware::_on_before_handle_request_main(Ref request) { @@ -284,25 +292,18 @@ bool SessionSetupWebServerMiddleware::_on_before_handle_request_main(Refcreate_session(); + // HTTPSessionManager *sm = request->server->get_session_manager(); + // ERR_FAIL_COND_V(!sm, false); + // Ref session = sm->create_session(); // request->session = session; // request->add_cookie(::Cookie("session_id", session->session_id)); return false; } - //TODO WebServer nodes should have a get_sess-manager() method - - //Probably: - //WebServer (Impl) -> maybe webroot node should not be auto discovered, it should have a nodepath for safety - // I - WebRoot - // I - WebNodes ... (site) - // I - HTTPSessionManager (finds parent websercver, registers itself in enter tree) - // I - Other helper nodes, maybe a DatabaseManager (convert to node) etc These will not be accessible - - //request->server->get_session_manager()->get_session(sid); - - //request->session = HTTPSessionManager::get_singleton()->get_session(sid); + HTTPSessionManager *sm = request->server->get_session_manager(); + ERR_FAIL_COND_V(!sm, false); + request->session = sm->get_session(sid); return false; } diff --git a/modules/web/http/http_session_manager.h b/modules/web/http/http_session_manager.h index 8eb3d9304..3a8867c3a 100644 --- a/modules/web/http/http_session_manager.h +++ b/modules/web/http/http_session_manager.h @@ -18,10 +18,10 @@ class HTTPSessionManager : public Node { GDCLASS(HTTPSessionManager, Node); public: - void add_session(Ref &session); - void remove_session(Ref &session); + void add_session(Ref session); + void remove_session(Ref session); void delete_session(const String &session_id); - void save_session(Ref &session); + void save_session(Ref session); Ref get_session(const String &session_id); Ref create_session(); diff --git a/modules/web/http/web_node.cpp b/modules/web/http/web_node.cpp index ed902a300..868c8f3db 100644 --- a/modules/web/http/web_node.cpp +++ b/modules/web/http/web_node.cpp @@ -329,6 +329,10 @@ void WebNode::clear_handlers() { _node_route_map.clear(); } +void WebNode::request_write_lock() { + _write_lock_requested = true; +} + WebServer *WebNode::get_server() { // todo this shoult probably be cached return Object::cast_to(get_tree()); @@ -404,11 +408,8 @@ void WebNode::_bind_methods() { ClassDB::bind_method(D_METHOD("set_uri_segment", "val"), &WebNode::set_uri_segment); ADD_PROPERTY(PropertyInfo(Variant::STRING, "uri_segment"), "set_uri_segment", "get_uri_segment"); - ClassDB::bind_method(D_METHOD("get_uri_segment"), &WebNode::get_uri_segment); - ClassDB::bind_method(D_METHOD("get_uri_segment"), &WebNode::get_uri_segment); - - String get_full_uri(const bool slash_at_the_end = true); - String get_full_uri_parent(const bool slash_at_the_end = true); + ClassDB::bind_method(D_METHOD("get_full_uri", "slash_at_the_end "), &WebNode::get_full_uri, true); + ClassDB::bind_method(D_METHOD("get_full_uri_parent", "slash_at_the_end "), &WebNode::get_full_uri_parent, true); //#if WEB_SETTINGS_ENABLED // Settings *get_settings(); diff --git a/modules/web/http/web_node.h b/modules/web/http/web_node.h index 39dce83ad..e8c9d7c69 100644 --- a/modules/web/http/web_node.h +++ b/modules/web/http/web_node.h @@ -124,6 +124,7 @@ protected: Ref _web_permission; + //TODO this should be atomic bool _write_lock_requested; RWLock _rw_lock; }; diff --git a/modules/web/http/web_server.cpp b/modules/web/http/web_server.cpp index 0abff1e50..b9b722dc9 100644 --- a/modules/web/http/web_server.cpp +++ b/modules/web/http/web_server.cpp @@ -54,6 +54,10 @@ void WebServer::server_handle_request(Ref request) { _rw_lock.read_unlock(); } +void WebServer::request_write_lock() { + _write_lock_requested = true; +} + void WebServer::start() { call("_start"); } @@ -87,7 +91,6 @@ void WebServer::_bind_methods() { 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); diff --git a/modules/web/http/web_server.h b/modules/web/http/web_server.h index b3bba41ef..ab52446d1 100644 --- a/modules/web/http/web_server.h +++ b/modules/web/http/web_server.h @@ -49,6 +49,7 @@ protected: HTTPSessionManager *_session_manager; + //TODO this should be atomic bool _write_lock_requested; RWLock _rw_lock; }; diff --git a/modules/web/register_types.cpp b/modules/web/register_types.cpp index e5dab2c92..a8c6f81e4 100644 --- a/modules/web/register_types.cpp +++ b/modules/web/register_types.cpp @@ -25,19 +25,40 @@ SOFTWARE. //#include "core/engine.h" #include "file_cache.h" + #include "html/html_builder_bind.h" + +#include "http/csrf_token.h" #include "http/http_server_enums.h" +#include "http/http_session.h" +#include "http/http_session_manager.h" +#include "http/web_node.h" +#include "http/web_permission.h" +#include "http/web_root.h" +#include "http/web_server.h" #include "http/web_server_cookie.h" +#include "http/web_server_middleware.h" +#include "http/web_server_request.h" void register_web_types() { - ClassDB::register_class(); + ClassDB::register_class<_HTMLBuilder>(); + ClassDB::register_class<_HTMLTag>(); ClassDB::register_class(); - ClassDB::register_class(); + ClassDB::register_class(); - ClassDB::register_class<_HTMLBuilder>(); - ClassDB::register_class<_HTMLTag>(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); + ClassDB::register_class(); } void unregister_web_types() {