Hobrasoft httpd server
Embedded HTTP server for Qt and C++
httpsession.h
Go to the documentation of this file.
1 
8 #ifndef _HttpSession_H_
9 #define _HttpSession_H_
10 
11 #include <QDateTime>
12 #include <QByteArray>
13 #include <QVariant>
14 #include <QHash>
15 #include <QMutex>
16 #include <QMutexLocker>
17 #include <QStringList>
18 #include <QSharedPointer>
19 
20 namespace HobrasoftHttpd {
21 
22 class HttpSessionStore;
23 
33 class HttpSession {
34  public:
35 
39  QByteArray id() const {
40  QMutexLocker locker(&m_mutex);
41  return (m_data == NULL) ? QByteArray() : m_data->id;
42  }
43 
47  bool isNull() const {
48  return m_data != NULL;
49  }
50 
51 
55  bool contains(const QString& key) const {
56  QMutexLocker locker(&m_mutex);
57  if (m_data != NULL) {
58  return m_data->values.contains(key);
59  }
60  return false;
61  }
62 
66  void add(const QString& key, const QVariant& value) {
67  QMutexLocker locker(&m_mutex);
68  if (m_data != NULL) {
69  m_data->values[key] = value;
70  }
71  }
72 
76  void remove(const QString& key) {
77  QMutexLocker locker(&m_mutex);
78  if (m_data != NULL) {
79  m_data->values.remove(key);
80  }
81  }
82 
86  QVariant value(const QString& key) const {
87  QMutexLocker locker(&m_mutex);
88  return (m_data == NULL) ? QVariant() : m_data->values.value(key);
89  }
90 
94  QDateTime lastAccess() {
95  QMutexLocker locker(&m_mutex);
96  return (m_data == NULL) ? QDateTime() : m_data->lastAccess;
97  }
98 
102  void setLastAccess() {
103  QMutexLocker locker(&m_mutex);
104  if (m_data != NULL) {
105  m_data->lastAccess = QDateTime::currentDateTime();
106  }
107  }
108 
109  QStringList keys() const {
110  QStringList list;
111  if (m_data != NULL) {
112  QHashIterator<QString, QVariant> iterator(m_data->values);
113  while (iterator.hasNext()) {
114  iterator.next();
115  list << iterator.key();
116  }
117  }
118  return list;
119  }
120 
121 
122  QVariantHash variantHash() const {
123  if (m_data == NULL) { return QVariantHash(); }
124  return m_data->values;
125  }
126 
127  private:
132  QByteArray id;
133  QDateTime lastAccess;
134  QHash<QString, QVariant> values;
135  };
136 
137  #ifndef DOXYGEN_SHOULD_SKIP_THIS
138  QSharedPointer<HttpSessionData> m_data;
139  mutable QMutex m_mutex;
140  #endif
141 
142 
143  public:
147  HttpSession();
148 
152  HttpSession(const HttpSession& other);
153 
157  HttpSession& operator= (const HttpSession& other);
158 
159 };
160 
161 }
162 
163 #endif
QByteArray id() const
Returns session ID.
Definition: httpsession.h:39
QDateTime lastAccess
Time of last access.
Definition: httpsession.h:133
HttpSession()
Constructor, should be private. Do not use in your code.
Definition: httpsession.cpp:16
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
bool isNull() const
Returns true if the session is valid (not null)
Definition: httpsession.h:47
QHash< QString, QVariant > values
List of values.
Definition: httpsession.h:134
QVariant value(const QString &key) const
Returns an item from the session.
Definition: httpsession.h:86
void add(const QString &key, const QVariant &value)
Adds an item to the session.
Definition: httpsession.h:66
bool contains(const QString &key) const
Returns true if the session contains key.
Definition: httpsession.h:55
HttpSession & operator=(const HttpSession &other)
Operator=(), should be private. Do not use in your code.
Definition: httpsession.cpp:32
Namespace of HTTP server.