QtWebApp
httpsession.cpp
Go to the documentation of this file.
1 
6 #include "httpsession.h"
7 #include <QDateTime>
8 #include <QUuid>
9 
10 using namespace stefanfrings;
11 
13 {
14  if (canStore)
15  {
16  dataPtr=new HttpSessionData();
17  dataPtr->refCount=1;
18  dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
19  dataPtr->id=QUuid::createUuid().toString().toLocal8Bit();
20 #ifdef SUPERVERBOSE
21  qDebug("HttpSession: (constructor) new session %s with refCount=1",dataPtr->id.constData());
22 #endif
23  }
24  else
25  {
26  dataPtr=nullptr;
27  }
28 }
29 
31 {
32  dataPtr=other.dataPtr;
33  if (dataPtr)
34  {
35  dataPtr->lock.lockForWrite();
36  dataPtr->refCount++;
37 #ifdef SUPERVERBOSE
38  qDebug("HttpSession: (constructor) copy session %s refCount=%i",dataPtr->id.constData(),dataPtr->refCount);
39 #endif
40  dataPtr->lock.unlock();
41  }
42 }
43 
45 {
46  HttpSessionData* oldPtr=dataPtr;
47  dataPtr=other.dataPtr;
48  if (dataPtr)
49  {
50  dataPtr->lock.lockForWrite();
51  dataPtr->refCount++;
52 #ifdef SUPERVERBOSE
53  qDebug("HttpSession: (operator=) session %s refCount=%i",dataPtr->id.constData(),dataPtr->refCount);
54 #endif
55  dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
56  dataPtr->lock.unlock();
57  }
58  if (oldPtr)
59  {
60  int refCount;
61  oldPtr->lock.lockForWrite();
62  refCount=--oldPtr->refCount;
63 #ifdef SUPERVERBOSE
64  qDebug("HttpSession: (operator=) session %s refCount=%i",oldPtr->id.constData(),oldPtr->refCount);
65 #endif
66  oldPtr->lock.unlock();
67  if (refCount==0)
68  {
69  qDebug("HttpSession: deleting old data");
70  delete oldPtr;
71  }
72  }
73  return *this;
74 }
75 
77 {
78  if (dataPtr) {
79  int refCount;
80  dataPtr->lock.lockForWrite();
81  refCount=--dataPtr->refCount;
82 #ifdef SUPERVERBOSE
83  qDebug("HttpSession: (destructor) session %s refCount=%i",dataPtr->id.constData(),dataPtr->refCount);
84 #endif
85  dataPtr->lock.unlock();
86  if (refCount==0)
87  {
88  qDebug("HttpSession: deleting data");
89  delete dataPtr;
90  }
91  }
92 }
93 
94 
95 QByteArray HttpSession::getId() const
96 {
97  if (dataPtr)
98  {
99  return dataPtr->id;
100  }
101  else
102  {
103  return QByteArray();
104  }
105 }
106 
107 bool HttpSession::isNull() const {
108  return dataPtr==nullptr;
109 }
110 
111 void HttpSession::set(const QByteArray& key, const QVariant& value)
112 {
113  if (dataPtr)
114  {
115  dataPtr->lock.lockForWrite();
116  dataPtr->values.insert(key,value);
117  dataPtr->lock.unlock();
118  }
119 }
120 
121 void HttpSession::remove(const QByteArray& key)
122 {
123  if (dataPtr)
124  {
125  dataPtr->lock.lockForWrite();
126  dataPtr->values.remove(key);
127  dataPtr->lock.unlock();
128  }
129 }
130 
131 QVariant HttpSession::get(const QByteArray& key) const
132 {
133  QVariant value;
134  if (dataPtr)
135  {
136  dataPtr->lock.lockForRead();
137  value=dataPtr->values.value(key);
138  dataPtr->lock.unlock();
139  }
140  return value;
141 }
142 
143 bool HttpSession::contains(const QByteArray& key) const
144 {
145  bool found=false;
146  if (dataPtr)
147  {
148  dataPtr->lock.lockForRead();
149  found=dataPtr->values.contains(key);
150  dataPtr->lock.unlock();
151  }
152  return found;
153 }
154 
155 QMap<QByteArray,QVariant> HttpSession::getAll() const
156 {
157  QMap<QByteArray,QVariant> values;
158  if (dataPtr)
159  {
160  dataPtr->lock.lockForRead();
161  values=dataPtr->values;
162  dataPtr->lock.unlock();
163  }
164  return values;
165 }
166 
168 {
169  qint64 value=0;
170  if (dataPtr)
171  {
172  dataPtr->lock.lockForRead();
173  value=dataPtr->lastAccess;
174  dataPtr->lock.unlock();
175  }
176  return value;
177 }
178 
179 
181 {
182  if (dataPtr)
183  {
184  dataPtr->lock.lockForWrite();
185  dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
186  dataPtr->lock.unlock();
187  }
188 }
This class stores data for a single HTTP session.
Definition: httpsession.h:23
bool isNull() const
Null sessions cannot store data.
HttpSession(const bool canStore=false)
Constructor.
Definition: httpsession.cpp:12
void setLastAccess()
Set the timestamp of last access, to renew the timeout period.
QByteArray getId() const
Get the unique ID of this session.
Definition: httpsession.cpp:95
void remove(const QByteArray &key)
Remove a value.
bool contains(const QByteArray &key) const
Check if a key exists.
QVariant get(const QByteArray &key) const
Get a value.
void set(const QByteArray &key, const QVariant &value)
Set a value.
HttpSession & operator=(const HttpSession &other)
Copy operator.
Definition: httpsession.cpp:44
qint64 getLastAccess() const
Get the timestamp of last access.
QMap< QByteArray, QVariant > getAll() const
Get a copy of all data stored in this session.
virtual ~HttpSession()
Destructor.
Definition: httpsession.cpp:76