HttpServer: use settings structures in place of QSettings (2)

pull/127/head
f4exb 2017-11-13 01:17:55 +01:00
rodzic 9a77311b2a
commit 3fb9d56d85
2 zmienionych plików z 29 dodań i 9 usunięć

Wyświetl plik

@ -4,13 +4,15 @@
*/
#include "httpsessionstore.h"
#include "httpsessionssettings.h"
#include <QDateTime>
#include <QUuid>
using namespace qtwebapp;
HttpSessionStore::HttpSessionStore(QSettings* settings, QObject* parent)
:QObject(parent)
:QObject(parent), sessionsSettings(0), useQtSettings(true)
{
this->settings=settings;
connect(&cleanupTimer,SIGNAL(timeout()),this,SLOT(sessionTimerEvent()));
@ -20,6 +22,17 @@ HttpSessionStore::HttpSessionStore(QSettings* settings, QObject* parent)
qDebug("HttpSessionStore: Sessions expire after %i milliseconds",expirationTime);
}
HttpSessionStore::HttpSessionStore(HttpSessionsSettings* settings, QObject* parent)
:QObject(parent), settings(0), useQtSettings(false)
{
this->sessionsSettings=settings;
connect(&cleanupTimer,SIGNAL(timeout()),this,SLOT(sessionTimerEvent()));
cleanupTimer.start(60000);
cookieName=QByteArray(qPrintable(settings->cookieName));
expirationTime=settings->expirationTime;
qDebug("HttpSessionStore: Sessions expire after %i milliseconds",expirationTime);
}
HttpSessionStore::~HttpSessionStore()
{
cleanupTimer.stop();
@ -60,10 +73,10 @@ HttpSession HttpSessionStore::getSession(HttpRequest& request, HttpResponse& res
{
mutex.unlock();
// Refresh the session cookie
QByteArray cookieName=settings->value("cookieName","sessionid").toByteArray();
QByteArray cookiePath=settings->value("cookiePath").toByteArray();
QByteArray cookieComment=settings->value("cookieComment").toByteArray();
QByteArray cookieDomain=settings->value("cookieDomain").toByteArray();
QByteArray cookieName = useQtSettings ? settings->value("cookieName","sessionid").toByteArray() : QByteArray(qPrintable(sessionsSettings->cookieName));
QByteArray cookiePath = useQtSettings ? settings->value("cookiePath").toByteArray() : QByteArray(qPrintable(sessionsSettings->cookiePath));
QByteArray cookieComment = useQtSettings ? settings->value("cookieComment").toByteArray() : QByteArray(qPrintable(sessionsSettings->cookieComment));
QByteArray cookieDomain = useQtSettings ? settings->value("cookieDomain").toByteArray() : QByteArray(qPrintable(sessionsSettings->cookieDomain));
response.setCookie(HttpCookie(cookieName,session.getId(),expirationTime/1000,cookiePath,cookieComment,cookieDomain));
session.setLastAccess();
return session;
@ -72,10 +85,10 @@ HttpSession HttpSessionStore::getSession(HttpRequest& request, HttpResponse& res
// Need to create a new session
if (allowCreate)
{
QByteArray cookieName=settings->value("cookieName","sessionid").toByteArray();
QByteArray cookiePath=settings->value("cookiePath").toByteArray();
QByteArray cookieComment=settings->value("cookieComment").toByteArray();
QByteArray cookieDomain=settings->value("cookieDomain").toByteArray();
QByteArray cookieName = useQtSettings ? settings->value("cookieName","sessionid").toByteArray() : QByteArray(qPrintable(sessionsSettings->cookieName));
QByteArray cookiePath = useQtSettings ? settings->value("cookiePath").toByteArray() : QByteArray(qPrintable(sessionsSettings->cookiePath));
QByteArray cookieComment = useQtSettings ? settings->value("cookieComment").toByteArray() : QByteArray(qPrintable(sessionsSettings->cookieComment));
QByteArray cookieDomain = useQtSettings ? settings->value("cookieDomain").toByteArray() : QByteArray(qPrintable(sessionsSettings->cookieDomain));
HttpSession session(true);
qDebug("HttpSessionStore: create new session with ID %s",session.getId().data());
sessions.insert(session.getId(),session);

Wyświetl plik

@ -17,6 +17,8 @@
namespace qtwebapp {
class HttpSessionsSettings;
/**
Stores HTTP sessions and deletes them when they have expired.
The following configuration settings are required in the config file:
@ -39,6 +41,7 @@ public:
/** Constructor. */
HttpSessionStore(QSettings* settings, QObject* parent=NULL);
HttpSessionStore(HttpSessionsSettings* settings, QObject* parent=NULL);
/** Destructor */
virtual ~HttpSessionStore();
@ -86,6 +89,7 @@ private:
/** Configuration settings */
QSettings* settings;
HttpSessionsSettings *sessionsSettings;
/** Timer to remove expired sessions */
QTimer cleanupTimer;
@ -99,6 +103,9 @@ private:
/** Used to synchronize threads */
QMutex mutex;
/** Settings flag */
bool useQtSettings;
private slots:
/** Called every minute to cleanup expired sessions. */