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.
httpsessionstore.cpp
Go to the documentation of this file.
1 
6 #include "httpsessionstore.h"
7 
8 #include <QDateTime>
9 #include <QUuid>
10 
11 using namespace qtwebapp;
12 
13 HttpSessionStore::HttpSessionStore(QSettings* settings, QObject* parent)
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 }
23 
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 }
34 
36 {
37  cleanupTimer.stop();
38 }
39 
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 }
63 
64 HttpSession HttpSessionStore::getSession(HttpRequest& request, HttpResponse& response, bool allowCreate)
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 }
102 
104 {
105  mutex.lock();
106  HttpSession session=sessions.value(id);
107  mutex.unlock();
108  session.setLastAccess();
109  return session;
110 }
111 
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 }
131 
132 
135 {
136  mutex.lock();
137  sessions.remove(session.getId());
138  mutex.unlock();
139 }
QByteArray getSessionId(HttpRequest &request, HttpResponse &response)
HttpSession getSession(HttpRequest &request, HttpResponse &response, bool allowCreate=true)
QMap< QByteArray, HttpCookie > & getCookies()
qint64 getLastAccess() const
QMap< QByteArray, HttpSession > sessions
void setCookie(const HttpCookie &cookie)
HttpSessionStore(QSettings *settings, QObject *parent=NULL)
int32_t i
Definition: decimators.h:244
QByteArray getCookie(const QByteArray &name) const
void removeSession(HttpSession session)
QByteArray getId() const
Definition: httpsession.cpp:97
HttpSessionsSettings sessionsSettings