2022-06-26 20:53:54 +02:00
|
|
|
#include "http_session_manager.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
#include "http_session.h"
|
|
|
|
|
2022-06-26 20:53:54 +02:00
|
|
|
#if DATABASES_ENABLED
|
2022-06-25 01:55:54 +02:00
|
|
|
#include "database/database_manager.h"
|
|
|
|
#include "database/query_builder.h"
|
|
|
|
#include "database/query_result.h"
|
|
|
|
#include "database/table_builder.h"
|
2022-06-26 20:53:54 +02:00
|
|
|
#endif
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 14:59:09 +02:00
|
|
|
#include "web_server.h"
|
2022-06-26 20:53:54 +02:00
|
|
|
#include "web_server_cookie.h"
|
2022-06-27 14:59:09 +02:00
|
|
|
#include "web_server_request.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 14:59:09 +02:00
|
|
|
void HTTPSessionManager::add_session(Ref<HTTPSession> session) {
|
2022-06-25 01:55:54 +02:00
|
|
|
if (!session.is_valid()) {
|
2022-06-27 13:56:34 +02:00
|
|
|
printf("HTTPSessionManager::add_session: ERROR, session is null!\n");
|
2022-06-25 01:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_mutex.lock();
|
|
|
|
|
|
|
|
_sessions_vec.push_back(session);
|
|
|
|
_sessions[session->session_id] = session;
|
|
|
|
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
2022-06-27 14:59:09 +02:00
|
|
|
void HTTPSessionManager::remove_session(Ref<HTTPSession> session) {
|
2022-06-25 01:55:54 +02:00
|
|
|
if (!session.is_valid()) {
|
2022-06-27 13:56:34 +02:00
|
|
|
printf("HTTPSessionManager::remove_session: ERROR, session is null!\n");
|
2022-06-25 01:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_mutex.lock();
|
|
|
|
|
|
|
|
_sessions.erase(session->session_id);
|
|
|
|
|
|
|
|
for (int i = 0; i < _sessions_vec.size(); ++i) {
|
|
|
|
if (_sessions_vec[i] == session) {
|
2022-07-23 22:37:11 +02:00
|
|
|
_sessions_vec.remove(i);
|
2022-06-25 01:55:54 +02:00
|
|
|
_mutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
2022-12-18 14:22:21 +01:00
|
|
|
Ref<HTTPSession> HTTPSessionManager::delete_session(const String &session_id) {
|
2022-06-25 01:55:54 +02:00
|
|
|
_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) {
|
2022-07-23 22:37:11 +02:00
|
|
|
_sessions_vec.remove(i);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_mutex.unlock();
|
|
|
|
|
2022-12-18 14:22:21 +01:00
|
|
|
return s;
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 14:59:09 +02:00
|
|
|
void HTTPSessionManager::save_session(Ref<HTTPSession> session) {
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 13:56:34 +02:00
|
|
|
Ref<HTTPSession> HTTPSessionManager::get_session(const String &session_id) {
|
2022-06-25 01:55:54 +02:00
|
|
|
return _sessions[session_id];
|
|
|
|
}
|
|
|
|
|
2022-06-27 13:56:34 +02:00
|
|
|
Ref<HTTPSession> HTTPSessionManager::create_session() {
|
2022-07-23 22:37:11 +02:00
|
|
|
Ref<HTTPSession> session;
|
|
|
|
session.instance();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-06-27 13:56:34 +02:00
|
|
|
void HTTPSessionManager::load_sessions() {
|
2022-06-25 01:55:54 +02:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2022-06-27 13:56:34 +02:00
|
|
|
void HTTPSessionManager::clear() {
|
2022-06-25 01:55:54 +02:00
|
|
|
_mutex.lock();
|
|
|
|
|
|
|
|
_sessions.clear();
|
|
|
|
_sessions_vec.clear();
|
|
|
|
|
|
|
|
_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
2022-06-27 13:56:34 +02:00
|
|
|
String HTTPSessionManager::generate_session_id(const String &base) {
|
2022-06-25 01:55:54 +02:00
|
|
|
// todo make something simpler / better
|
|
|
|
|
|
|
|
String sid = base;
|
|
|
|
|
2022-06-26 20:53:54 +02:00
|
|
|
sid += itos(Math::rand());
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-26 20:53:54 +02:00
|
|
|
return sid.sha256_text().substr(0, 20);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 13:56:34 +02:00
|
|
|
HTTPSessionManager::HTTPSessionManager() {
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 13:56:34 +02:00
|
|
|
HTTPSessionManager::~HTTPSessionManager() {
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 13:56:34 +02:00
|
|
|
void HTTPSessionManager::_bind_methods() {
|
2022-06-27 14:59:09 +02:00
|
|
|
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, "");
|
2022-06-26 20:53:54 +02:00
|
|
|
}
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-26 20:53:54 +02:00
|
|
|
bool SessionSetupWebServerMiddleware::_on_before_handle_request_main(Ref<WebServerRequest> request) {
|
2022-06-25 01:55:54 +02:00
|
|
|
const String sid = request->get_cookie("session_id");
|
|
|
|
|
|
|
|
if (sid == "") {
|
|
|
|
// You could create a session here if you want to always assign sessions to visitors.
|
|
|
|
// Example code:
|
2022-06-27 14:59:09 +02:00
|
|
|
// HTTPSessionManager *sm = request->server->get_session_manager();
|
|
|
|
// ERR_FAIL_COND_V(!sm, false);
|
|
|
|
// Ref<HTTPSession> session = sm->create_session();
|
2022-06-25 01:55:54 +02:00
|
|
|
// request->session = session;
|
|
|
|
// request->add_cookie(::Cookie("session_id", session->session_id));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-21 23:25:04 +02:00
|
|
|
HTTPSessionManager *sm = request->get_server()->get_session_manager();
|
2022-06-27 14:59:09 +02:00
|
|
|
ERR_FAIL_COND_V(!sm, false);
|
2022-07-21 23:25:04 +02:00
|
|
|
request->set_session(sm->get_session(sid));
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
SessionSetupWebServerMiddleware::SessionSetupWebServerMiddleware() {
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
2022-06-26 15:00:25 +02:00
|
|
|
SessionSetupWebServerMiddleware::~SessionSetupWebServerMiddleware() {
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
2022-06-26 20:53:54 +02:00
|
|
|
|
|
|
|
void SessionSetupWebServerMiddleware::_bind_methods() {
|
|
|
|
}
|