diff --git a/modules/web/http/csrf_token.cpp b/modules/web/http/csrf_token.cpp index fef09ebb2..670e5bff7 100644 --- a/modules/web/http/csrf_token.cpp +++ b/modules/web/http/csrf_token.cpp @@ -5,13 +5,12 @@ #include "request.h" #include -bool CSRFTokenMiddleware::on_before_handle_request_main(Request *request) { +bool CSRFTokenWebServerMiddleware::on_before_handle_request_main(Request *request) { switch (request->get_method()) { case HTTP_METHOD_POST: case HTTP_METHOD_DELETE: case HTTP_METHOD_PATCH: case HTTP_METHOD_PUT: { - if (shold_ignore(request)) { return false; } @@ -44,7 +43,7 @@ bool CSRFTokenMiddleware::on_before_handle_request_main(Request *request) { return false; } -bool CSRFTokenMiddleware::shold_ignore(Request *request) { +bool CSRFTokenWebServerMiddleware::shold_ignore(Request *request) { const String &path = request->get_path_full(); for (int i = 0; i < ignored_urls.size(); ++i) { @@ -56,7 +55,7 @@ bool CSRFTokenMiddleware::shold_ignore(Request *request) { return false; } -String CSRFTokenMiddleware::create_token() { +String CSRFTokenWebServerMiddleware::create_token() { Ref h = SHA256::get(); String s = h->compute(String::num(time(NULL))); @@ -64,7 +63,7 @@ String CSRFTokenMiddleware::create_token() { return s.substr(0, 10); } -CSRFTokenMiddleware::CSRFTokenMiddleware() { +CSRFTokenWebServerMiddleware::CSRFTokenWebServerMiddleware() { } -CSRFTokenMiddleware::~CSRFTokenMiddleware() { +CSRFTokenWebServerMiddleware::~CSRFTokenWebServerMiddleware() { } diff --git a/modules/web/http/csrf_token.h b/modules/web/http/csrf_token.h index 19bb18e34..770d7b1ed 100644 --- a/modules/web/http/csrf_token.h +++ b/modules/web/http/csrf_token.h @@ -8,8 +8,8 @@ class Request; -class CSRFTokenMiddleware : public Middleware { - RCPP_OBJECT(CSRFTokenMiddleware, Middleware); +class CSRFTokenWebServerMiddleware : public WebServerMiddleware { + RCPP_OBJECT(CSRFTokenWebServerMiddleware, WebServerMiddleware); public: //returnring true means handled, false means continue @@ -19,10 +19,10 @@ public: virtual String create_token(); - CSRFTokenMiddleware(); - ~CSRFTokenMiddleware(); + CSRFTokenWebServerMiddleware(); + ~CSRFTokenWebServerMiddleware(); Vector ignored_urls; }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/http/http_session_manager.cpp b/modules/web/http/http_session_manager.cpp index 4ac596af9..019f13549 100644 --- a/modules/web/http/http_session_manager.cpp +++ b/modules/web/http/http_session_manager.cpp @@ -62,7 +62,6 @@ void SessionManager::delete_session(const String &session_id) { Ref sess = _sessions_vec[i]; if (sess->session_id == session_id) { - _sessions_vec[i] = _sessions_vec[_sessions_vec.size() - 1]; _sessions_vec.pop_back(); @@ -131,7 +130,6 @@ Ref SessionManager::create_session() { _mutex.lock(); if (_sessions[session->session_id] == nullptr) { - _sessions_vec.push_back(session); _sessions[session->session_id] = session; @@ -269,7 +267,6 @@ SessionManager *SessionManager::get_singleton() { SessionManager::SessionManager() : Object() { - if (_self) { printf("SessionManager::SessionManager(): Error! self is not null!/n"); } @@ -291,7 +288,7 @@ SessionManager *SessionManager::_self = nullptr; String SessionManager::_table_name = "sessions"; String SessionManager::_data_table_name = "session_data"; -bool SessionSetupMiddleware::on_before_handle_request_main(Request *request) { +bool SessionSetupWebServerMiddleware::on_before_handle_request_main(Request *request) { const String sid = request->get_cookie("session_id"); if (sid == "") { @@ -309,7 +306,7 @@ bool SessionSetupMiddleware::on_before_handle_request_main(Request *request) { return false; } -SessionSetupMiddleware::SessionSetupMiddleware() { +SessionSetupWebServerMiddleware::SessionSetupWebServerMiddleware() { } -SessionSetupMiddleware::~SessionSetupMiddleware() { +SessionSetupWebServerMiddleware::~SessionSetupWebServerMiddleware() { } diff --git a/modules/web/http/http_session_manager.h b/modules/web/http/http_session_manager.h index a0da9a94b..296429019 100644 --- a/modules/web/http/http_session_manager.h +++ b/modules/web/http/http_session_manager.h @@ -1,8 +1,8 @@ #ifndef SESSION_MANAGER_H #define SESSION_MANAGER_H -#include "core/string.h" #include "core/containers/vector.h" +#include "core/string.h" #include "core/threading/mutex.h" #include @@ -50,15 +50,15 @@ protected: static String _data_table_name; }; -class SessionSetupMiddleware : public Middleware { - RCPP_OBJECT(SessionSetupMiddleware, Middleware); +class SessionSetupWebServerMiddleware : public WebServerMiddleware { + RCPP_OBJECT(SessionSetupWebServerMiddleware, WebServerMiddleware); public: //returnring true means handled, false means continue bool on_before_handle_request_main(Request *request); - SessionSetupMiddleware(); - ~SessionSetupMiddleware(); + SessionSetupWebServerMiddleware(); + ~SessionSetupWebServerMiddleware(); }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/http/web_root.cpp b/modules/web/http/web_root.cpp index 158bf74a5..91136eee1 100644 --- a/modules/web/http/web_root.cpp +++ b/modules/web/http/web_root.cpp @@ -24,34 +24,34 @@ void WebRoot::setup_error_handlers() { } void WebRoot::setup_middleware() { - // Middlewares get processed in the order they are in the _middlewares array + // WebServerMiddlewares get processed in the order they are in the _middlewares array // ------- Built in middleware selection ------- - // --- SessionSetupMiddleware --- + // --- SessionSetupWebServerMiddleware --- // If you want sessions add this to your inherited class. // (#include "web/http/session_manager.h") - // _middlewares.push_back(Ref(new SessionSetupMiddleware())); + // _middlewares.push_back(Ref(new SessionSetupWebServerMiddleware())); - // --- UserSessionSetupMiddleware --- + // --- UserSessionSetupWebServerMiddleware --- // This one looks up users based on sessions // (#include "web_modules/users/user_controller.h") - // _middlewares.push_back(Ref(new UserSessionSetupMiddleware())); + // _middlewares.push_back(Ref(new UserSessionSetupWebServerMiddleware())); - // --- RBACUserSessionSetupMiddleware / RBACDefaultUserSessionSetupMiddleware --- + // --- RBACUserSessionSetupWebServerMiddleware / RBACDefaultUserSessionSetupWebServerMiddleware --- // Same as the previous, but if you want the RBAC system to work use one of these - // UserSessionSetupMiddleware is not needed if you need these + // UserSessionSetupWebServerMiddleware is not needed if you need these // (#include "web_modules/rbac_users/rbac_user_controller.h") - // _middlewares.push_back(Ref(new RBACUserSessionSetupMiddleware())); - // _middlewares.push_back(Ref(new RBACDefaultUserSessionSetupMiddleware())); + // _middlewares.push_back(Ref(new RBACUserSessionSetupWebServerMiddleware())); + // _middlewares.push_back(Ref(new RBACDefaultUserSessionSetupWebServerMiddleware())); // --- CSRF Token Middlweare --- // (#include "web/http/csrf_token.h") - // Ref csrf_middleware; + // Ref csrf_middleware; // csrf_middleware.instance(); // csrf_middleware->ignored_urls.push_back("/user/login"); // csrf_middleware->ignored_urls.push_back("/user/register"); diff --git a/modules/web/http/web_root.h b/modules/web/http/web_root.h index 394d39fa0..c9f8fd690 100644 --- a/modules/web/http/web_root.h +++ b/modules/web/http/web_root.h @@ -2,10 +2,10 @@ #define WEB_ROOT_H #include "core/containers/vector.h" +#include "core/string.h" #include "mutex" #include #include -#include "core/string.h" #include "web_node.h" @@ -46,9 +46,9 @@ public: virtual ~WebRoot(); public: - Vector > _middlewares; + Vector> _middlewares; - std::map > error_handler_map; + std::map> error_handler_map; std::function default_error_handler_func; protected: @@ -56,4 +56,4 @@ protected: std::vector _update_registered_requests; }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/http/web_server_cookie.cpp b/modules/web/http/web_server_cookie.cpp index 9c37396ad..7e2efe3f3 100644 --- a/modules/web/http/web_server_cookie.cpp +++ b/modules/web/http/web_server_cookie.cpp @@ -1,7 +1,7 @@ #include "cookie.h" -Cookie::Cookie(const String &p_key, const String &p_value) { +WebServerCookie::WebServerCookie(const String &p_key, const String &p_value) { http_only = true; secure = false; @@ -9,10 +9,10 @@ Cookie::Cookie(const String &p_key, const String &p_value) { value = p_value; } -Cookie::Cookie() { +WebServerCookie::WebServerCookie() { http_only = true; secure = false; } -Cookie::~Cookie() { -} \ No newline at end of file +WebServerCookie::~WebServerCookie() { +} diff --git a/modules/web/http/web_server_cookie.h b/modules/web/http/web_server_cookie.h index a8533252f..2d12060c8 100644 --- a/modules/web/http/web_server_cookie.h +++ b/modules/web/http/web_server_cookie.h @@ -3,7 +3,7 @@ #include "core/string.h" -class Cookie { +class WebServerCookie { public: //todo date String domain; @@ -13,9 +13,9 @@ public: bool http_only; bool secure; - Cookie(); - Cookie(const String &p_key, const String &p_value); - ~Cookie(); + WebServerCookie(); + WebServerCookie(const String &p_key, const String &p_value); + ~WebServerCookie(); }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/http/web_server_middleware.cpp b/modules/web/http/web_server_middleware.cpp index 4fdf988ef..970f1c50e 100644 --- a/modules/web/http/web_server_middleware.cpp +++ b/modules/web/http/web_server_middleware.cpp @@ -3,12 +3,13 @@ #include "request.h" -bool Middleware::on_before_handle_request_main(Request *request) { +bool WebServerMiddleware::on_before_handle_request_main(Request *request) { return false; } -Middleware::Middleware() : Reference() { +WebServerMiddleware::WebServerMiddleware() : + Reference() { } -Middleware::~Middleware() { -} \ No newline at end of file +WebServerMiddleware::~WebServerMiddleware() { +} diff --git a/modules/web/http/web_server_middleware.h b/modules/web/http/web_server_middleware.h index 1c4bf73a0..77554dba9 100644 --- a/modules/web/http/web_server_middleware.h +++ b/modules/web/http/web_server_middleware.h @@ -7,15 +7,15 @@ class Request; -class Middleware : public Reference { - RCPP_OBJECT(Middleware, Reference); +class WebServerMiddleware : public Reference { + RCPP_OBJECT(WebServerMiddleware, Reference); public: //returnring true means handled, false, means continue virtual bool on_before_handle_request_main(Request *request); - Middleware(); - ~Middleware(); + WebServerMiddleware(); + ~WebServerMiddleware(); }; -#endif \ No newline at end of file +#endif diff --git a/modules/web/http/web_server_request.cpp b/modules/web/http/web_server_request.cpp index b03b5bc3b..e53a86533 100644 --- a/modules/web/http/web_server_request.cpp +++ b/modules/web/http/web_server_request.cpp @@ -85,7 +85,7 @@ const String WebServerRequest::get_cookie(const String &key) { return str; } -void WebServerRequest::add_cookie(const ::Cookie &cookie) { +void WebServerRequest::add_cookie(const ::WebServerCookie &cookie) { } void WebServerRequest::remove_cookie(const String &key) { diff --git a/modules/web/http/web_server_request.h b/modules/web/http/web_server_request.h index 0b9c66fbd..7cf3304a3 100644 --- a/modules/web/http/web_server_request.h +++ b/modules/web/http/web_server_request.h @@ -1,24 +1,23 @@ #ifndef WEB_SERVER_REQUEST_H #define WEB_SERVER_REQUEST_H +#include "core/hash_map.h" #include "core/ustring.h" #include "core/vector.h" -#include -#include -#include - #include "core/object.h" #include "core/reference.h" #include "http_enums.h" class WebServer; -class Cookie; +class WebServerCookie; class HTTPSession; class WebPermission; -class WebServerRequest { +class WebServerRequest : public Reference { + GDCLASS(WebServerRequest, Reference); + public: WebServer *server; @@ -36,8 +35,8 @@ public: bool connection_closed; Ref session; - std::map data; - std::map> reference_data; + HashMap data; + HashMap> reference_data; Ref get_or_create_session(); @@ -55,7 +54,7 @@ public: bool validate_csrf_token(); virtual const String get_cookie(const String &key); - virtual void add_cookie(const ::Cookie &cookie); + virtual void add_cookie(const ::WebServerCookie &cookie); virtual void remove_cookie(const String &key); virtual HTTPMethod get_method() const;