Class renames.

This commit is contained in:
Relintai 2022-06-26 15:00:25 +02:00
parent e6d45eeb40
commit 544863b923
12 changed files with 61 additions and 65 deletions

View File

@ -5,13 +5,12 @@
#include "request.h"
#include <time.h>
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<SHA256> 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() {
}

View File

@ -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<String> ignored_urls;
};
#endif
#endif

View File

@ -62,7 +62,6 @@ void SessionManager::delete_session(const String &session_id) {
Ref<HTTPSession> 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<HTTPSession> 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() {
}

View File

@ -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 <map>
@ -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
#endif

View File

@ -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<SessionSetupMiddleware>(new SessionSetupMiddleware()));
// _middlewares.push_back(Ref<SessionSetupWebServerMiddleware>(new SessionSetupWebServerMiddleware()));
// --- UserSessionSetupMiddleware ---
// --- UserSessionSetupWebServerMiddleware ---
// This one looks up users based on sessions
// (#include "web_modules/users/user_controller.h")
// _middlewares.push_back(Ref<UserSessionSetupMiddleware>(new UserSessionSetupMiddleware()));
// _middlewares.push_back(Ref<UserSessionSetupWebServerMiddleware>(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<RBACUserSessionSetupMiddleware>(new RBACUserSessionSetupMiddleware()));
// _middlewares.push_back(Ref<RBACDefaultUserSessionSetupMiddleware>(new RBACDefaultUserSessionSetupMiddleware()));
// _middlewares.push_back(Ref<RBACUserSessionSetupWebServerMiddleware>(new RBACUserSessionSetupWebServerMiddleware()));
// _middlewares.push_back(Ref<RBACDefaultUserSessionSetupWebServerMiddleware>(new RBACDefaultUserSessionSetupWebServerMiddleware()));
// --- CSRF Token Middlweare ---
// (#include "web/http/csrf_token.h")
// Ref<CSRFTokenMiddleware> csrf_middleware;
// Ref<CSRFTokenWebServerMiddleware> csrf_middleware;
// csrf_middleware.instance();
// csrf_middleware->ignored_urls.push_back("/user/login");
// csrf_middleware->ignored_urls.push_back("/user/register");

View File

@ -2,10 +2,10 @@
#define WEB_ROOT_H
#include "core/containers/vector.h"
#include "core/string.h"
#include "mutex"
#include <functional>
#include <map>
#include "core/string.h"
#include "web_node.h"
@ -46,9 +46,9 @@ public:
virtual ~WebRoot();
public:
Vector<Ref<Middleware> > _middlewares;
Vector<Ref<WebServerMiddleware>> _middlewares;
std::map<int, std::function<void(Request *, int)> > error_handler_map;
std::map<int, std::function<void(Request *, int)>> error_handler_map;
std::function<void(Request *, int)> default_error_handler_func;
protected:
@ -56,4 +56,4 @@ protected:
std::vector<Request *> _update_registered_requests;
};
#endif
#endif

View File

@ -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() {
}
WebServerCookie::~WebServerCookie() {
}

View File

@ -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
#endif

View File

@ -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() {
}
WebServerMiddleware::~WebServerMiddleware() {
}

View File

@ -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
#endif

View File

@ -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) {

View File

@ -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 <map>
#include <mutex>
#include <vector>
#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<HTTPSession> session;
std::map<String, Object *> data;
std::map<String, Ref<Reference>> reference_data;
HashMap<String, Object *> data;
HashMap<String, Ref<Reference>> reference_data;
Ref<HTTPSession> 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;