2017-08-23 16:47:07 +00:00
|
|
|
/**
|
|
|
|
@file
|
|
|
|
@author Stefan Frings
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef HTTPSESSIONSTORE_H
|
|
|
|
#define HTTPSESSIONSTORE_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QMutex>
|
|
|
|
#include "httpglobal.h"
|
|
|
|
#include "httpsession.h"
|
|
|
|
#include "httpresponse.h"
|
|
|
|
#include "httprequest.h"
|
2017-11-13 12:46:02 +00:00
|
|
|
#include "httpsessionssettings.h"
|
2017-08-23 16:47:07 +00:00
|
|
|
|
2018-03-20 12:49:21 +00:00
|
|
|
#include "export.h"
|
2018-03-03 19:23:38 +00:00
|
|
|
|
2017-11-11 08:32:15 +00:00
|
|
|
namespace qtwebapp {
|
2017-08-23 16:47:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Stores HTTP sessions and deletes them when they have expired.
|
|
|
|
The following configuration settings are required in the config file:
|
|
|
|
<code><pre>
|
|
|
|
expirationTime=3600000
|
|
|
|
cookieName=sessionid
|
|
|
|
</pre></code>
|
|
|
|
The following additional configurations settings are optionally:
|
|
|
|
<code><pre>
|
|
|
|
cookiePath=/
|
|
|
|
cookieComment=Session ID
|
|
|
|
;cookieDomain=stefanfrings.de
|
|
|
|
</pre></code>
|
|
|
|
*/
|
|
|
|
|
2018-03-03 19:23:38 +00:00
|
|
|
class HTTPSERVER_API HttpSessionStore : public QObject {
|
2017-08-23 16:47:07 +00:00
|
|
|
Q_OBJECT
|
|
|
|
Q_DISABLE_COPY(HttpSessionStore)
|
|
|
|
public:
|
|
|
|
|
2017-11-13 12:46:02 +00:00
|
|
|
/** Constructor with Qt settings. */
|
2017-08-23 16:47:07 +00:00
|
|
|
HttpSessionStore(QSettings* settings, QObject* parent=NULL);
|
2017-11-13 12:46:02 +00:00
|
|
|
|
|
|
|
/** Constructor with settings structure. */
|
|
|
|
HttpSessionStore(const HttpSessionsSettings& settings, QObject* parent=NULL);
|
2017-08-23 16:47:07 +00:00
|
|
|
|
|
|
|
/** Destructor */
|
|
|
|
virtual ~HttpSessionStore();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get the ID of the current HTTP session, if it is valid.
|
|
|
|
This method is thread safe.
|
|
|
|
@warning Sessions may expire at any time, so subsequent calls of
|
|
|
|
getSession() might return a new session with a different ID.
|
|
|
|
@param request Used to get the session cookie
|
|
|
|
@param response Used to get and set the new session cookie
|
|
|
|
@return Empty string, if there is no valid session.
|
|
|
|
*/
|
|
|
|
QByteArray getSessionId(HttpRequest& request, HttpResponse& response);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get the session of a HTTP request, eventually create a new one.
|
|
|
|
This method is thread safe. New sessions can only be created before
|
|
|
|
the first byte has been written to the HTTP response.
|
|
|
|
@param request Used to get the session cookie
|
|
|
|
@param response Used to get and set the new session cookie
|
|
|
|
@param allowCreate can be set to false, to disable the automatic creation of a new session.
|
|
|
|
@return If autoCreate is disabled, the function returns a null session if there is no session.
|
|
|
|
@see HttpSession::isNull()
|
|
|
|
*/
|
|
|
|
HttpSession getSession(HttpRequest& request, HttpResponse& response, bool allowCreate=true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get a HTTP session by it's ID number.
|
|
|
|
This method is thread safe.
|
|
|
|
@return If there is no such session, the function returns a null session.
|
|
|
|
@param id ID number of the session
|
|
|
|
@see HttpSession::isNull()
|
|
|
|
*/
|
|
|
|
HttpSession getSession(const QByteArray id);
|
|
|
|
|
|
|
|
/** Delete a session */
|
|
|
|
void removeSession(HttpSession session);
|
|
|
|
|
2017-11-13 12:46:02 +00:00
|
|
|
/**
|
|
|
|
* Get a sessions settings copy
|
|
|
|
* @return The current sessions settings
|
|
|
|
*/
|
|
|
|
HttpSessionsSettings getListenerSettings() const { return sessionsSettings; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set new sessions settings data
|
|
|
|
* @param sessions settings to replace current data
|
|
|
|
*/
|
|
|
|
void setListenerSettings(const HttpSessionsSettings& settings) { sessionsSettings = settings; }
|
|
|
|
|
2017-08-23 16:47:07 +00:00
|
|
|
protected:
|
|
|
|
/** Storage for the sessions */
|
|
|
|
QMap<QByteArray,HttpSession> sessions;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2017-11-13 12:46:02 +00:00
|
|
|
/** Configuration settings as Qt settings*/
|
2017-08-23 16:47:07 +00:00
|
|
|
QSettings* settings;
|
2017-11-13 12:46:02 +00:00
|
|
|
|
|
|
|
/** Configuration settings as a structure*/
|
|
|
|
HttpSessionsSettings sessionsSettings;
|
2017-08-23 16:47:07 +00:00
|
|
|
|
|
|
|
/** Timer to remove expired sessions */
|
|
|
|
QTimer cleanupTimer;
|
|
|
|
|
|
|
|
/** Name of the session cookie */
|
|
|
|
QByteArray cookieName;
|
|
|
|
|
|
|
|
/** Time when sessions expire (in ms)*/
|
|
|
|
int expirationTime;
|
|
|
|
|
|
|
|
/** Used to synchronize threads */
|
|
|
|
QMutex mutex;
|
|
|
|
|
2017-11-13 00:17:55 +00:00
|
|
|
/** Settings flag */
|
|
|
|
bool useQtSettings;
|
|
|
|
|
2017-08-23 16:47:07 +00:00
|
|
|
private slots:
|
|
|
|
|
|
|
|
/** Called every minute to cleanup expired sessions. */
|
|
|
|
void sessionTimerEvent();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end of namespace
|
|
|
|
|
|
|
|
#endif // HTTPSESSIONSTORE_H
|