Set up inheritance and virtuals for HTTPSessionManagerDB and HTTPSessionManager.

This commit is contained in:
Relintai 2022-12-18 14:22:21 +01:00
parent 9aced75966
commit db51b273a7
4 changed files with 27 additions and 294 deletions

View File

@ -79,66 +79,15 @@ Ref<QueryBuilder> HTTPSessionManagerDB::get_query_builder() {
return conn->get_query_builder();
}
void HTTPSessionManagerDB::add_session(Ref<HTTPSession> session) {
if (!session.is_valid()) {
printf("HTTPSessionManagerDB::add_session: ERROR, session is null!\n");
return;
}
_mutex.lock();
_sessions_vec.push_back(session);
_sessions[session->session_id] = session;
_mutex.unlock();
}
void HTTPSessionManagerDB::remove_session(Ref<HTTPSession> session) {
if (!session.is_valid()) {
printf("HTTPSessionManagerDB::remove_session: ERROR, session is null!\n");
return;
}
_mutex.lock();
_sessions.erase(session->session_id);
for (int i = 0; i < _sessions_vec.size(); ++i) {
if (_sessions_vec[i] == session) {
_sessions_vec.remove(i);
_mutex.unlock();
return;
}
}
_mutex.unlock();
}
void HTTPSessionManagerDB::delete_session(const String &session_id) {
_mutex.lock();
Ref<HTTPSession> s = _sessions[session_id];
_sessions.erase(session_id);
for (int i = 0; i < _sessions_vec.size(); ++i) {
Ref<HTTPSession> sess = _sessions_vec[i];
if (sess->session_id == session_id) {
_sessions_vec.remove(i);
break;
}
}
_mutex.unlock();
Ref<HTTPSession> HTTPSessionManagerDB::delete_session(const String &session_id) {
Ref<HTTPSession> s = HTTPSessionManager::delete_session(session_id);
if (!s.is_valid()) {
return;
return s;
}
if (!s->id) {
return;
return s;
}
Ref<QueryBuilder> b = get_query_builder();
@ -146,6 +95,8 @@ void HTTPSessionManagerDB::delete_session(const String &session_id) {
b->del(_database_data_table_name)->where()->wpi("session_db_id", s->id)->end_command();
b->del(_database_table_name)->where()->wpi("id", s->id)->end_command();
b->run_query();
return s;
}
void HTTPSessionManagerDB::save_session(Ref<HTTPSession> session) {
@ -190,36 +141,6 @@ void HTTPSessionManagerDB::save_session(Ref<HTTPSession> session) {
b->run_query();
}
Ref<HTTPSession> HTTPSessionManagerDB::get_session(const String &session_id) {
return _sessions[session_id];
}
Ref<HTTPSession> HTTPSessionManagerDB::create_session() {
Ref<HTTPSession> session;
session.instance();
while (true) {
session->session_id = generate_session_id(session->session_id);
_mutex.lock();
if (_sessions[session->session_id] == nullptr) {
_sessions_vec.push_back(session);
_sessions[session->session_id] = session;
_mutex.unlock();
return session;
}
_mutex.unlock();
}
save_session(session);
return session;
}
void HTTPSessionManagerDB::load_sessions() {
clear();
@ -279,25 +200,6 @@ void HTTPSessionManagerDB::load_sessions() {
}
}
void HTTPSessionManagerDB::clear() {
_mutex.lock();
_sessions.clear();
_sessions_vec.clear();
_mutex.unlock();
}
String HTTPSessionManagerDB::generate_session_id(const String &base) {
// todo make something simpler / better
String sid = base;
sid += itos(Math::rand());
return sid.sha256_text().substr(0, 20);
}
void HTTPSessionManagerDB::migrate() {
drop_table();
create_table();
@ -334,12 +236,11 @@ void HTTPSessionManagerDB::drop_table() {
}
HTTPSessionManagerDB::HTTPSessionManagerDB() {
_database_table_name = "sessions";
_database_data_table_name = "session_data";
_database_table_name = "http_sessions";
_database_data_table_name = "http_session_data";
}
HTTPSessionManagerDB::~HTTPSessionManagerDB() {
clear();
}
void HTTPSessionManagerDB::_notification(const int what) {
@ -371,15 +272,4 @@ void HTTPSessionManagerDB::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_database_connection"), &HTTPSessionManagerDB::get_database_connection);
ClassDB::bind_method(D_METHOD("get_table_builder"), &HTTPSessionManagerDB::get_table_builder);
ClassDB::bind_method(D_METHOD("get_query_builder"), &HTTPSessionManagerDB::get_query_builder);
ClassDB::bind_method(D_METHOD("add_session", "session"), &HTTPSessionManagerDB::add_session);
ClassDB::bind_method(D_METHOD("remove_session", "session"), &HTTPSessionManagerDB::remove_session);
ClassDB::bind_method(D_METHOD("delete_session", "session_id"), &HTTPSessionManagerDB::delete_session);
ClassDB::bind_method(D_METHOD("save_session", "session"), &HTTPSessionManagerDB::save_session);
ClassDB::bind_method(D_METHOD("get_session", "session_id"), &HTTPSessionManagerDB::get_session);
ClassDB::bind_method(D_METHOD("create_session"), &HTTPSessionManagerDB::create_session);
ClassDB::bind_method(D_METHOD("load_sessions"), &HTTPSessionManagerDB::load_sessions);
ClassDB::bind_method(D_METHOD("clear"), &HTTPSessionManagerDB::clear);
ClassDB::bind_method(D_METHOD("generate_session_id", "base"), &HTTPSessionManagerDB::generate_session_id, "");
}

View File

@ -9,6 +9,8 @@
#include "core/object/reference.h"
#include "scene/main/node.h"
#include "../http/http_session_manager.h"
#include "../http/web_server_middleware.h"
class HTTPSession;
@ -18,8 +20,8 @@ class DatabaseConnection;
class TableBuilder;
class QueryBuilder;
class HTTPSessionManagerDB : public Node {
GDCLASS(HTTPSessionManagerDB, Node);
class HTTPSessionManagerDB : public HTTPSessionManager {
GDCLASS(HTTPSessionManagerDB, HTTPSessionManager);
public:
String get_database_table_name();
@ -35,19 +37,11 @@ public:
Ref<TableBuilder> get_table_builder();
Ref<QueryBuilder> get_query_builder();
void add_session(Ref<HTTPSession> session);
void remove_session(Ref<HTTPSession> session);
void delete_session(const String &session_id);
Ref<HTTPSession> delete_session(const String &session_id);
void save_session(Ref<HTTPSession> session);
Ref<HTTPSession> get_session(const String &session_id);
Ref<HTTPSession> create_session();
void load_sessions();
void clear();
virtual String generate_session_id(const String &base = "");
virtual void migrate();
virtual void create_table();
virtual void drop_table();
@ -55,10 +49,6 @@ public:
HTTPSessionManagerDB();
~HTTPSessionManagerDB();
HashMap<String, Ref<HTTPSession>> _sessions;
Vector<Ref<HTTPSession>> _sessions_vec;
Mutex _mutex;
protected:
void _notification(const int what);

View File

@ -48,7 +48,7 @@ void HTTPSessionManager::remove_session(Ref<HTTPSession> session) {
_mutex.unlock();
}
void HTTPSessionManager::delete_session(const String &session_id) {
Ref<HTTPSession> HTTPSessionManager::delete_session(const String &session_id) {
_mutex.lock();
Ref<HTTPSession> s = _sessions[session_id];
@ -67,54 +67,10 @@ void HTTPSessionManager::delete_session(const String &session_id) {
_mutex.unlock();
if (!s.is_valid()) {
return;
}
if (!s->id) {
return;
}
#if DATABASES_ENABLED
Ref<QueryBuilder> b = DatabaseManager::get_singleton()->ddb->get_query_builder();
b->del(_data_table_name)->where()->wp("session_db_id", s->id)->end_command();
b->del(_table_name)->where()->wp("id", s->id)->end_command();
b->run_query();
#endif
return s;
}
void HTTPSessionManager::save_session(Ref<HTTPSession> session) {
#if DATABASES_ENABLED
Ref<QueryBuilder> b = DatabaseManager::get_singleton()->ddb->get_query_builder();
if (!session->id) {
b->insert(_table_name, "session_id");
b->values();
b->val(session->session_id);
b->cvalues();
b->end_command();
b->select_last_insert_id();
session->id = b->run()->get_last_insert_rowid();
b->reset();
}
b->del(_data_table_name)->where()->wp("session_db_id", session->id)->end_command();
int id = session->id;
std::map<String, Variant> *m = session->get_data();
for (std::map<String, Variant>::iterator it = m->begin(); it != m->end(); it++) {
const Variant &val = it->second;
if (val.is_simple_type()) {
b->insert(_data_table_name, "session_db_id,key,value")->values()->val(id)->val(it->first)->val(val.to_string())->cvalues()->end_command();
}
}
b->run_query();
#endif
}
Ref<HTTPSession> HTTPSessionManager::get_session(const String &session_id) {
@ -149,61 +105,6 @@ Ref<HTTPSession> HTTPSessionManager::create_session() {
void HTTPSessionManager::load_sessions() {
clear();
#if DATABASES_ENABLED
Ref<QueryBuilder> b = DatabaseManager::get_singleton()->ddb->get_query_builder();
b->select("id, session_id");
b->from(_table_name);
// b->print();
Ref<QueryResult> r = b->run();
while (r->next_row()) {
int id = r->get_cell_int(0);
String session_id = r->get_cell(1);
Ref<HTTPSession> s;
s.instance();
s->id = id;
s->session_id = session_id;
add_session(s);
}
b->reset();
b->select("session_db_id,key,value");
b->from(_data_table_name);
// b->print();
r = b->run();
while (r->next_row()) {
int session_db_id = r->get_cell_int(0);
Ref<HTTPSession> s;
for (int i = 0; i < _sessions_vec.size(); ++i) {
Ref<HTTPSession> ss = _sessions_vec[i];
if (ss.is_valid() && session_db_id == ss->id) {
s = ss;
break;
}
}
if (!s.is_valid()) {
printf("Error: HTTPSessionManager::load_sessions(): %d sid doesn't exists!\n", session_db_id);
continue;
}
String key = r->get_cell(1);
String value = r->get_cell(2);
s->add(key, Variant::parse_string(value));
}
#endif
}
void HTTPSessionManager::clear() {
@ -225,52 +126,10 @@ String HTTPSessionManager::generate_session_id(const String &base) {
return sid.sha256_text().substr(0, 20);
}
void HTTPSessionManager::migrate() {
drop_table();
create_table();
}
void HTTPSessionManager::create_table() {
#if DATABASES_ENABLED
Ref<TableBuilder> tb = DatabaseManager::get_singleton()->ddb->get_table_builder();
tb->create_table(_table_name);
tb->integer("id")->auto_increment()->next_row();
tb->varchar("session_id", 100)->next_row();
tb->primary_key("id");
tb->ccreate_table();
// tb->print();
tb->run_query();
tb->result = "";
tb->create_table(_data_table_name);
tb->integer("session_db_id")->not_null()->next_row();
tb->varchar("key", 100)->next_row();
tb->text("value")->not_null()->next_row();
tb->foreign_key("session_db_id");
tb->references(_table_name, "id");
tb->ccreate_table();
// tb->print();
tb->run_query();
#endif
}
void HTTPSessionManager::drop_table() {
#if DATABASES_ENABLED
Ref<TableBuilder> tb = DatabaseManager::get_singleton()->ddb->get_table_builder();
tb->drop_table_if_exists(_table_name)->run_query();
tb->drop_table_if_exists(_data_table_name)->run_query();
#endif
}
HTTPSessionManager::HTTPSessionManager() {
_table_name = "sessions";
_data_table_name = "session_data";
}
HTTPSessionManager::~HTTPSessionManager() {
clear();
}
void HTTPSessionManager::_bind_methods() {

View File

@ -2,9 +2,9 @@
#define HTTP_SESSION_MANAGER_H
#include "core/containers/hash_map.h"
#include "core/containers/vector.h"
#include "core/os/mutex.h"
#include "core/string/ustring.h"
#include "core/containers/vector.h"
#include "core/object/reference.h"
#include "scene/main/node.h"
@ -18,34 +18,28 @@ class HTTPSessionManager : public Node {
GDCLASS(HTTPSessionManager, Node);
public:
void add_session(Ref<HTTPSession> session);
void remove_session(Ref<HTTPSession> session);
void delete_session(const String &session_id);
void save_session(Ref<HTTPSession> session);
Ref<HTTPSession> get_session(const String &session_id);
Ref<HTTPSession> create_session();
virtual void add_session(Ref<HTTPSession> session);
virtual void remove_session(Ref<HTTPSession> session);
virtual Ref<HTTPSession> delete_session(const String &session_id);
virtual void save_session(Ref<HTTPSession> session);
virtual Ref<HTTPSession> get_session(const String &session_id);
virtual Ref<HTTPSession> create_session();
void load_sessions();
virtual void load_sessions();
void clear();
virtual void clear();
virtual String generate_session_id(const String &base = "");
virtual void migrate();
virtual void create_table();
virtual void drop_table();
HTTPSessionManager();
~HTTPSessionManager();
protected:
static void _bind_methods();
HashMap<String, Ref<HTTPSession>> _sessions;
Vector<Ref<HTTPSession>> _sessions_vec;
Mutex _mutex;
String _table_name;
String _data_table_name;
protected:
static void _bind_methods();
};
class SessionSetupWebServerMiddleware : public WebServerMiddleware {