SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Public Member Functions | Protected Attributes | Private Slots | Private Attributes | List of all members
qtwebapp::HttpSessionStore Class Reference

#include <httpsessionstore.h>

Inherits QObject.

+ Collaboration diagram for qtwebapp::HttpSessionStore:

Public Member Functions

 HttpSessionStore (QSettings *settings, QObject *parent=NULL)
 
 HttpSessionStore (const HttpSessionsSettings &settings, QObject *parent=NULL)
 
virtual ~HttpSessionStore ()
 
QByteArray getSessionId (HttpRequest &request, HttpResponse &response)
 
HttpSession getSession (HttpRequest &request, HttpResponse &response, bool allowCreate=true)
 
HttpSession getSession (const QByteArray id)
 
void removeSession (HttpSession session)
 
HttpSessionsSettings getListenerSettings () const
 
void setListenerSettings (const HttpSessionsSettings &settings)
 

Protected Attributes

QMap< QByteArray, HttpSessionsessions
 

Private Slots

void sessionTimerEvent ()
 

Private Attributes

QSettings * settings
 
HttpSessionsSettings sessionsSettings
 
QTimer cleanupTimer
 
QByteArray cookieName
 
int expirationTime
 
QMutex mutex
 
bool useQtSettings
 

Detailed Description

Stores HTTP sessions and deletes them when they have expired. The following configuration settings are required in the config file:

expirationTime=3600000
cookieName=sessionid

The following additional configurations settings are optionally:

cookiePath=/
cookieComment=Session ID
;cookieDomain=stefanfrings.de

Definition at line 38 of file httpsessionstore.h.

Constructor & Destructor Documentation

◆ HttpSessionStore() [1/2]

HttpSessionStore::HttpSessionStore ( QSettings *  settings,
QObject *  parent = NULL 
)

Constructor with Qt settings.

Definition at line 13 of file httpsessionstore.cpp.

References cleanupTimer, cookieName, expirationTime, sessionTimerEvent(), and settings.

14  :QObject(parent), useQtSettings(true)
15 {
16  this->settings=settings;
17  connect(&cleanupTimer,SIGNAL(timeout()),this,SLOT(sessionTimerEvent()));
18  cleanupTimer.start(60000);
19  cookieName=settings->value("cookieName","sessionid").toByteArray();
20  expirationTime=settings->value("expirationTime",3600000).toInt();
21  qDebug("HttpSessionStore: Sessions expire after %i milliseconds",expirationTime);
22 }
+ Here is the call graph for this function:

◆ HttpSessionStore() [2/2]

HttpSessionStore::HttpSessionStore ( const HttpSessionsSettings settings,
QObject *  parent = NULL 
)

Constructor with settings structure.

Definition at line 24 of file httpsessionstore.cpp.

References cleanupTimer, qtwebapp::HttpSessionsSettings::cookieName, cookieName, qtwebapp::HttpSessionsSettings::expirationTime, expirationTime, sessionsSettings, sessionTimerEvent(), and settings.

25  :QObject(parent), settings(0), useQtSettings(false)
26 {
28  connect(&cleanupTimer,SIGNAL(timeout()),this,SLOT(sessionTimerEvent()));
29  cleanupTimer.start(60000);
30  cookieName=QByteArray(qPrintable(settings.cookieName));
32  qDebug("HttpSessionStore: Sessions expire after %i milliseconds",expirationTime);
33 }
HttpSessionsSettings sessionsSettings
+ Here is the call graph for this function:

◆ ~HttpSessionStore()

HttpSessionStore::~HttpSessionStore ( )
virtual

Destructor

Definition at line 35 of file httpsessionstore.cpp.

References cleanupTimer.

36 {
37  cleanupTimer.stop();
38 }

Member Function Documentation

◆ getListenerSettings()

HttpSessionsSettings qtwebapp::HttpSessionStore::getListenerSettings ( ) const
inline

Get a sessions settings copy

Returns
The current sessions settings

Definition at line 91 of file httpsessionstore.h.

91 { return sessionsSettings; }
HttpSessionsSettings sessionsSettings

◆ getSession() [1/2]

HttpSession HttpSessionStore::getSession ( HttpRequest request,
HttpResponse response,
bool  allowCreate = true 
)

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.

Parameters
requestUsed to get the session cookie
responseUsed to get and set the new session cookie
allowCreatecan be set to false, to disable the automatic creation of a new session.
Returns
If autoCreate is disabled, the function returns a null session if there is no session.
See also
HttpSession::isNull()

Definition at line 64 of file httpsessionstore.cpp.

References qtwebapp::HttpSessionsSettings::cookieComment, qtwebapp::HttpSessionsSettings::cookieDomain, qtwebapp::HttpSessionsSettings::cookieName, cookieName, qtwebapp::HttpSessionsSettings::cookiePath, expirationTime, qtwebapp::HttpSession::getId(), getSessionId(), qtwebapp::HttpSession::isNull(), mutex, sessions, sessionsSettings, qtwebapp::HttpResponse::setCookie(), qtwebapp::HttpSession::setLastAccess(), settings, and useQtSettings.

65 {
66  QByteArray sessionId=getSessionId(request,response);
67  mutex.lock();
68  if (!sessionId.isEmpty())
69  {
70  HttpSession session=sessions.value(sessionId);
71  if (!session.isNull())
72  {
73  mutex.unlock();
74  // Refresh the session cookie
75  QByteArray cookieName = useQtSettings ? settings->value("cookieName","sessionid").toByteArray() : QByteArray(qPrintable(sessionsSettings.cookieName));
76  QByteArray cookiePath = useQtSettings ? settings->value("cookiePath").toByteArray() : QByteArray(qPrintable(sessionsSettings.cookiePath));
77  QByteArray cookieComment = useQtSettings ? settings->value("cookieComment").toByteArray() : QByteArray(qPrintable(sessionsSettings.cookieComment));
78  QByteArray cookieDomain = useQtSettings ? settings->value("cookieDomain").toByteArray() : QByteArray(qPrintable(sessionsSettings.cookieDomain));
79  response.setCookie(HttpCookie(cookieName,session.getId(),expirationTime/1000,cookiePath,cookieComment,cookieDomain));
80  session.setLastAccess();
81  return session;
82  }
83  }
84  // Need to create a new session
85  if (allowCreate)
86  {
87  QByteArray cookieName = useQtSettings ? settings->value("cookieName","sessionid").toByteArray() : QByteArray(qPrintable(sessionsSettings.cookieName));
88  QByteArray cookiePath = useQtSettings ? settings->value("cookiePath").toByteArray() : QByteArray(qPrintable(sessionsSettings.cookiePath));
89  QByteArray cookieComment = useQtSettings ? settings->value("cookieComment").toByteArray() : QByteArray(qPrintable(sessionsSettings.cookieComment));
90  QByteArray cookieDomain = useQtSettings ? settings->value("cookieDomain").toByteArray() : QByteArray(qPrintable(sessionsSettings.cookieDomain));
91  HttpSession session(true);
92  qDebug("HttpSessionStore: create new session with ID %s",session.getId().data());
93  sessions.insert(session.getId(),session);
94  response.setCookie(HttpCookie(cookieName,session.getId(),expirationTime/1000,cookiePath,cookieComment,cookieDomain));
95  mutex.unlock();
96  return session;
97  }
98  // Return a null session
99  mutex.unlock();
100  return HttpSession();
101 }
QByteArray getSessionId(HttpRequest &request, HttpResponse &response)
QMap< QByteArray, HttpSession > sessions
void setCookie(const HttpCookie &cookie)
QByteArray getId() const
Definition: httpsession.cpp:97
HttpSessionsSettings sessionsSettings
+ Here is the call graph for this function:

◆ getSession() [2/2]

HttpSession HttpSessionStore::getSession ( const QByteArray  id)

Get a HTTP session by it's ID number. This method is thread safe.

Returns
If there is no such session, the function returns a null session.
Parameters
idID number of the session
See also
HttpSession::isNull()

Definition at line 103 of file httpsessionstore.cpp.

References mutex, sessions, and qtwebapp::HttpSession::setLastAccess().

104 {
105  mutex.lock();
106  HttpSession session=sessions.value(id);
107  mutex.unlock();
108  session.setLastAccess();
109  return session;
110 }
QMap< QByteArray, HttpSession > sessions
+ Here is the call graph for this function:

◆ getSessionId()

QByteArray HttpSessionStore::getSessionId ( HttpRequest request,
HttpResponse response 
)

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.
Parameters
requestUsed to get the session cookie
responseUsed to get and set the new session cookie
Returns
Empty string, if there is no valid session.

Definition at line 40 of file httpsessionstore.cpp.

References cookieName, qtwebapp::HttpRequest::getCookie(), qtwebapp::HttpResponse::getCookies(), mutex, and sessions.

Referenced by getSession().

41 {
42  // The session ID in the response has priority because this one will be used in the next request.
43  mutex.lock();
44  // Get the session ID from the response cookie
45  QByteArray sessionId=response.getCookies().value(cookieName).getValue();
46  if (sessionId.isEmpty())
47  {
48  // Get the session ID from the request cookie
49  sessionId=request.getCookie(cookieName);
50  }
51  // Clear the session ID if there is no such session in the storage.
52  if (!sessionId.isEmpty())
53  {
54  if (!sessions.contains(sessionId))
55  {
56  qDebug("HttpSessionStore: received invalid session cookie with ID %s",sessionId.data());
57  sessionId.clear();
58  }
59  }
60  mutex.unlock();
61  return sessionId;
62 }
QMap< QByteArray, HttpCookie > & getCookies()
QMap< QByteArray, HttpSession > sessions
QByteArray getCookie(const QByteArray &name) const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removeSession()

void HttpSessionStore::removeSession ( HttpSession  session)

Delete a session

Definition at line 134 of file httpsessionstore.cpp.

References qtwebapp::HttpSession::getId(), mutex, and sessions.

135 {
136  mutex.lock();
137  sessions.remove(session.getId());
138  mutex.unlock();
139 }
QMap< QByteArray, HttpSession > sessions
QByteArray getId() const
Definition: httpsession.cpp:97
+ Here is the call graph for this function:

◆ sessionTimerEvent

void HttpSessionStore::sessionTimerEvent ( )
privateslot

Called every minute to cleanup expired sessions.

Definition at line 112 of file httpsessionstore.cpp.

References expirationTime, qtwebapp::HttpSession::getId(), qtwebapp::HttpSession::getLastAccess(), i, mutex, and sessions.

Referenced by HttpSessionStore().

113 {
114  mutex.lock();
115  qint64 now=QDateTime::currentMSecsSinceEpoch();
116  QMap<QByteArray,HttpSession>::iterator i = sessions.begin();
117  while (i != sessions.end())
118  {
119  QMap<QByteArray,HttpSession>::iterator prev = i;
120  ++i;
121  HttpSession session=prev.value();
122  qint64 lastAccess=session.getLastAccess();
123  if (now-lastAccess>expirationTime)
124  {
125  qDebug("HttpSessionStore: session %s expired",session.getId().data());
126  sessions.erase(prev);
127  }
128  }
129  mutex.unlock();
130 }
qint64 getLastAccess() const
QMap< QByteArray, HttpSession > sessions
int32_t i
Definition: decimators.h:244
QByteArray getId() const
Definition: httpsession.cpp:97
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setListenerSettings()

void qtwebapp::HttpSessionStore::setListenerSettings ( const HttpSessionsSettings settings)
inline

Set new sessions settings data

Parameters
sessionssettings to replace current data

Definition at line 97 of file httpsessionstore.h.

Member Data Documentation

◆ cleanupTimer

QTimer qtwebapp::HttpSessionStore::cleanupTimer
private

Timer to remove expired sessions

Definition at line 112 of file httpsessionstore.h.

Referenced by HttpSessionStore(), and ~HttpSessionStore().

◆ cookieName

QByteArray qtwebapp::HttpSessionStore::cookieName
private

Name of the session cookie

Definition at line 115 of file httpsessionstore.h.

Referenced by getSession(), getSessionId(), and HttpSessionStore().

◆ expirationTime

int qtwebapp::HttpSessionStore::expirationTime
private

Time when sessions expire (in ms)

Definition at line 118 of file httpsessionstore.h.

Referenced by getSession(), HttpSessionStore(), and sessionTimerEvent().

◆ mutex

QMutex qtwebapp::HttpSessionStore::mutex
private

Used to synchronize threads

Definition at line 121 of file httpsessionstore.h.

Referenced by getSession(), getSessionId(), removeSession(), and sessionTimerEvent().

◆ sessions

QMap<QByteArray,HttpSession> qtwebapp::HttpSessionStore::sessions
protected

Storage for the sessions

Definition at line 101 of file httpsessionstore.h.

Referenced by getSession(), getSessionId(), removeSession(), and sessionTimerEvent().

◆ sessionsSettings

HttpSessionsSettings qtwebapp::HttpSessionStore::sessionsSettings
private

Configuration settings as a structure

Definition at line 109 of file httpsessionstore.h.

Referenced by getSession(), and HttpSessionStore().

◆ settings

QSettings* qtwebapp::HttpSessionStore::settings
private

Configuration settings as Qt settings

Definition at line 106 of file httpsessionstore.h.

Referenced by getSession(), and HttpSessionStore().

◆ useQtSettings

bool qtwebapp::HttpSessionStore::useQtSettings
private

Settings flag

Definition at line 124 of file httpsessionstore.h.

Referenced by getSession().


The documentation for this class was generated from the following files: