Hobrasoft httpd server
Embedded HTTP server for Qt and C++
httpsessionstore.cpp
Go to the documentation of this file.
1 
8 #include "httpsessionstore.h"
9 #include "httpsession.h"
10 #include "httpresponse.h"
11 #include "httprequest.h"
12 #include "httpsettings.h"
13 #include "httpserver.h"
14 
15 using namespace HobrasoftHttpd;
16 
17 
18 HttpSessionStore::HttpSessionStore(const HttpSettings* settings, QObject *parent) : QObject(parent) {
19  m_settings = settings;
20  init();
21 }
22 
23 
24 HttpSessionStore::HttpSessionStore(HttpServer *server) : QObject(server) {
25  m_settings = server->settings();
26  init();
27 }
28 
29 
30 void HttpSessionStore::init() {
31  m_cleaner = new QTimer(this);
32  m_cleaner->setInterval(30000);
33  m_cleaner->setSingleShot(false);
34  m_cleaner->start();
35  connect(m_cleaner, SIGNAL(timeout()), this, SLOT(slotCleaner()));
36 }
37 
38 
39 const QString& HttpSessionStore::sessionCookieName() const {
40  if (m_sessionCookieName.isEmpty() || m_sessionCookieName == "") {
41  return m_settings->sessionCookieName();
42  }
43  return m_sessionCookieName;
44 }
45 
46 
47 QByteArray HttpSessionStore::sessionId(HttpRequest *request, HttpResponse *response) {
48  QByteArray sessionId = response->cookie(sessionCookieName()).value().toUtf8();
49  if (sessionId.isEmpty()) {
50  sessionId = request->cookie(sessionCookieName()).toUtf8();
51  }
52 
53  if (sessionId.isEmpty() || !m_sessions.contains(sessionId)) {
54  return QByteArray();
55  }
56 
57  return sessionId;
58 }
59 
60 
62  QByteArray id = sessionId(request, response);
63  if (!id.isEmpty()) {
64  HttpSession session(m_sessions.value(id));
66  return session;
67  }
68 
70  m_sessions.insert(session.id(), session);
71  response->setCookie(
72  HttpCookie(
74  session.id(),
76  "/"
77  )
78  );
79  return session;
80 }
81 
82 
83 HttpSession HttpSessionStore::session(const QByteArray& id) {
86  return session;
87 }
88 
89 
91  QDateTime now = QDateTime::currentDateTime();
92  QHash<QByteArray, HttpSession>::iterator i = m_sessions.begin() ;
93  while (i != m_sessions.end()) {
94  QHash<QByteArray, HttpSession>::iterator prev = i;
95  i++;
96  HttpSession session = prev.value();
97  if (!session.lastAccess().isValid() || session.lastAccess().addSecs(m_settings->sessionExpirationTime()) < now) {
98  emit aboutToRemove(session);
99  m_sessions.erase(prev);
100  }
101  }
102 }
103 
104 
106  emit aboutToRemove(session);
107  m_sessions.remove(session.id());
108 }
109 
110 
QByteArray id() const
Returns session ID.
Definition: httpsession.h:39
const HttpSettings * settings() const
Returs pointer to HttpSettings used in the HttpServer.
Definition: httpserver.h:120
const QString & sessionCookieName() const
Returns session cookie name.
Definition: httpsettings.h:92
QByteArray sessionId(HttpRequest *request, HttpResponse *response)
Returns session ID associated with the request or response.
HttpSessionStore(const HttpSettings *settings, QObject *parent)
Constructor.
QDateTime lastAccess()
Return time of last access to the session.
Definition: httpsession.h:94
Information about one session.
Definition: httpsession.h:33
void setLastAccess()
Sets time of last access to the session.
Definition: httpsession.h:102
QString m_sessionCookieName
The name of the session cookie.
General single-threaded, event-driven HTTP server.
Definition: httpserver.h:88
void aboutToRemove(const HttpSession &session) const
Signal is emited when a session is about to remove from storage.
HttpSession session(HttpRequest *request, HttpResponse *response)
Returns session associated with the request or response.
HttpCookie cookie(const QString &name)
Returns cookie.
Definition: httpresponse.h:79
One cookie of HTTP protocol.
Definition: httpcookie.h:20
QVariant value(const QString &key) const
Returns an item from the session.
Definition: httpsession.h:86
void remove(HttpSession session)
Removes session.
QHash< QByteArray, HttpSession > m_sessions
List of stored sessions.
Response to HTTP request - headers, cookies and body.
Definition: httpresponse.h:30
QString value() const
Returns the value of cookie.
Definition: httpcookie.h:55
QTimer * m_cleaner
Timer for store cleaning from old sessions.
Configuration of the http server instance.
Definition: httpsettings.h:39
void setCookie(const HttpCookie &cookie)
Sets a cookie.
const HttpSettings * m_settings
Pointer to http server settings.
QString cookie(const QString &name) const
Returns cookie.
Definition: httprequest.h:143
int sessionExpirationTime() const
Returns maximum expiration time for the session.
Definition: httpsettings.h:88
Processes HTTP request, parses headers, body and files sent by HTTP protocol.
Definition: httprequest.h:23
void slotCleaner()
Slot cleans the store from old sessions.
Namespace of HTTP server.
const QString & sessionCookieName() const
Returns the session cookie name.