-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusession.cpp
More file actions
408 lines (361 loc) · 9.97 KB
/
Copy pathusession.cpp
File metadata and controls
408 lines (361 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <QDir>
#include <QMutex>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include <QDateTime>
#include <QDomDocument>
#include <QSqlDatabase>
#include <QDesktopServices>
#include "uno.h"
#include "uutil.h"
#include "ucrypto.h"
#include "uconfig.h"
#include "usession.h"
#include "uresponse.h"
#include "uconnectionpool.h"
uSession::uSession() :
m_session(0), m_engine(), m_encrypt(false), m_secret()
{
qsrand(QDateTime::currentDateTime().toTime_t());
uConfig *config = &uConfig::getInstance();
m_engine = config->value("session/engine", "file").toString();
m_encrypt = config->value("session/encrypt", false).toBool();
m_secret = config->value("secret", "").toString();
uint lifetime = config->value("session/lifetime", (uint)uCookie::TwentyMins).toUInt();
if (m_engine == "cookie")
{
m_session = new uSessionCookie(lifetime);
}
else if (m_engine == "database")
{
m_session = new uSessionDatabase(lifetime);
}
else {
m_session = new uSessionFile(lifetime);
}
// run session garbage collection every around 100 requests.
if (0 == (qrand() % 100))
{
connect((&Uno::getInstance()), SIGNAL(shutdown()), m_session, SLOT(gc()));
}
m_session->open();
connect(m_session, SIGNAL(write()), this, SLOT(write()));
connect((&uResponse::getInstance()), SIGNAL(packingCookies(uCookieJar*)), m_session, SLOT(addSessionCookie(uCookieJar*)));
read();
}
uSession::~uSession()
{
delete m_session;
}
uSession &uSession::getInstance()
{
static QMutex mutex;
QMutexLocker lock(&mutex);
Q_UNUSED(lock);
static uSession instance;
return instance;
}
void uSession::insert(const QString &name, const QVariant &value)
{
m_params.insert(name, value);
}
QVariant uSession::value(const QString &name, const QVariant &value)
{
return m_params.value(name, value);
}
QVariant uSession::getOnce(const QString &name, const QVariant &value)
{
if (m_params.contains(name))
{
return m_params.take(name);
}
return value;
}
void uSession::remove(const QString &name)
{
m_params.remove(name);
}
bool uSession::contains(const QString &name)
{
return m_params.contains(name);
}
void uSession::read()
{
QString data = m_session->read();
if (data.isEmpty()) return;
if (! m_secret.isEmpty() && m_encrypt)
{
uCrypto crypto(m_secret);
data = crypto.decrypt(data);
}
QDomDocument doc;
doc.setContent(data);
QDomElement root = doc.documentElement();
QDomNodeList nodes = root.childNodes();
for (int i = 0; i < nodes.size(); i++)
{
QDomElement node = nodes.at(i).toElement();
QString key = node.nodeName();
QByteArray ba(QByteArray::fromBase64(node.attribute("value").toUtf8()));
QVariant value;
{
QDataStream out(&ba, QIODevice::ReadOnly);
out >> value;
}
m_params.insert(key, value);
}
}
void uSession::write()
{
QDomDocument doc;
QDomProcessingInstruction head = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
doc.appendChild(head);
QDomElement root = doc.createElement("unosession");
doc.appendChild(root);
QList<QString> keys = m_params.keys();
QByteArray ba;
for (int i = 0; i < keys.size(); i++)
{
ba.clear();
QString key = keys.at(i);
QDomElement node = doc.createElement(key);
{
QDataStream in(&ba, QIODevice::WriteOnly);
in << m_params.value(key);
}
node.setAttribute("value", QString(ba.toBase64()));
root.appendChild(node);
}
if (! m_secret.isEmpty() && m_encrypt)
{
uCrypto crypto(m_secret);
m_session->write(crypto.encrypt(doc.toString()));
}
else {
m_session->write(doc.toString());
}
m_session->close();
}
uSessionEngine::uSessionEngine(uint lifetime)
{
m_config = &uConfig::getInstance();
m_name = m_config->value("session/name", "UNOSESSID").toString();
uCookieJar *cjar = &uCookieJar::getInstance();
m_cookie = cjar->cookie(m_name);
// in case (re)set the name (could be a new cookie)
m_cookie.setName(m_name);
m_cookie.setMaxAge(lifetime);
}
void uSessionEngine::addSessionCookie(uCookieJar *cookies)
{
if (! m_cookie.name().isEmpty())
{
cookies->insert(m_cookie);
}
}
uSessionFile::uSessionFile(uint lifetime) :
uSessionEngine(lifetime), m_file(), m_basepath()
{
m_basepath.append(QDesktopServices::storageLocation(QDesktopServices::TempLocation));
if (m_basepath.isEmpty())
{
m_basepath.append(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
if (m_basepath.isEmpty())
{
m_basepath.append(m_config->value("session/location", "").toString());
if (m_basepath.isEmpty())
{
qDebug() << tr("Cannot find a valid location to store session files");
qDebug() << tr("please set a valid location in the configuration file.");
qDebug() << tr("Trying /tmp.....");
m_basepath.append("/tmp");
}
}
}
m_basepath.append(m_basepath.endsWith("/") ? "" : "/");
QString path(m_basepath);
path.append("uno_");
if (m_cookie.hasValue())
{
path.append(m_cookie.value());
QFileInfo sesfile(path);
if (sesfile.exists())
{
// if the file was NOT accessed within "lifetime"
// or is older than 24 hours its expired so delete it
uint now = QDateTime::currentDateTime().toTime_t();
// yesterday
QDateTime ieri = QDateTime::fromTime_t(now - 86400);
QDateTime then = QDateTime::fromTime_t(now - lifetime);
if ((sesfile.created() < ieri) || (sesfile.lastRead() < then))
{
QFile::remove(path);
m_cookie.setValue(uUtil::uuid());
path.clear();
path.append(m_basepath);
path.append("uno_");
path.append(m_cookie.value());
}
}
}
else {
m_cookie.setValue(uUtil::uuid());
path.append(m_cookie.value());
}
m_file.setFileName(path);
// here we let know when our write method should be called
connect((&Uno::getInstance()), SIGNAL(shutdown()), this, SIGNAL(write()));
}
void uSessionFile::open()
{
m_file.open(QFile::ReadWrite);
}
void uSessionFile::close()
{
m_file.close();
}
QString uSessionFile::read()
{
return m_file.readAll();
}
void uSessionFile::write(const QString &data)
{
m_file.seek(0);
if (m_file.write(data.toUtf8()) < 0)
{
qDebug() << Q_FUNC_INFO << m_file.errorString();
}
}
void uSessionFile::remove()
{
if (m_file.exists())
{
m_file.remove();
}
}
void uSessionFile::gc()
{
QDir dir(m_basepath);
QStringList filter("uno_*");
QFileInfoList files = dir.entryInfoList(filter, QDir::Files);
// time 24 hours from now
QDateTime ieri = QDateTime::fromTime_t(QDateTime::currentDateTime().toTime_t() - 86400);
foreach (QFileInfo file, files)
{
if (file.created() < ieri)
{
QFile::remove(file.absoluteFilePath());
}
}
}
uSessionCookie::uSessionCookie(uint lifetime) :
uSessionEngine(lifetime)
{
connect((&uResponse::getInstance()), SIGNAL(packingCookies(uCookieJar*)), this, SIGNAL(write()));
}
void uSessionCookie::open() {}
QString uSessionCookie::read()
{
return QByteArray::fromBase64(m_cookie.value().toUtf8());
}
void uSessionCookie::write(const QString &data)
{
QByteArray buf;
buf.append(data);
m_cookie.setValue(buf.toBase64());
}
void uSessionCookie::close() {}
void uSessionCookie::remove()
{
m_cookie.remove();
}
void uSessionCookie::gc() {}
/***
* Session ORM class
*/
uSessionORM::uSessionORM() :
uORM(setFields(), "sessions")
{
}
QHash<QString, QVariant> uSessionORM::setFields()
{
QHash<QString, QVariant> fields;
fields.insert("id", QVariant(QVariant::Invalid));
fields.insert("session_id", "");
fields.insert("access", 0);
fields.insert("data", "");
return fields;
}
bool uSessionORM::save()
{
m_fields.insert("access", uUtil::timeUTC());
return uORM::save();
}
/***
* Database Session
*/
uSessionDatabase::uSessionDatabase(uint lifetime) :
uSessionEngine(lifetime)
{
if (m_cookie.hasValue())
{
if ( ! m_ses.findWhere("session_id", m_cookie.value()))
{
m_cookie.setValue("");
qDebug() << m_ses.lastError();
return;
}
if (m_ses.isLoaded())
{
QDateTime then = QDateTime::fromTime_t(uUtil::timeUTC() - lifetime);
QDateTime access = QDateTime::fromTime_t(m_ses.field("access").toUInt());
if (access < then)
{
m_cookie.setValue("");
m_ses.remove();
}
}
}
else {
m_cookie.setValue(uUtil::uuid());
}
connect((&Uno::getInstance()), SIGNAL(shutdown()), this, SIGNAL(write()));
}
void uSessionDatabase::open() {}
QString uSessionDatabase::read()
{
return m_ses.isLoaded() ? m_ses.field("data", "").toString() : "";
}
void uSessionDatabase::write(const QString &data)
{
if (! data.isEmpty())
{
m_ses.setField("session_id", m_cookie.value());
m_ses.setField("data", data);
m_ses.save();
}
}
void uSessionDatabase::remove()
{
if (m_ses.isLoaded()) m_ses.remove();
}
void uSessionDatabase::close() {}
void uSessionDatabase::gc()
{
uConnectionPool *pool = &uConnectionPool::getInstance();
QSqlDatabase con;
if (! pool->connection(con))
{
qDebug() << Q_FUNC_INFO;
qDebug() << con.lastError().text();
return;
}
QSqlQuery query(con);
query.prepare("DELETE FROM sessions WHERE access <= ?");
query.bindValue(0, (uint)(uUtil::timeUTC() - uCookie::OneDay));
if (! query.exec())
{
qDebug() << query.lastError().text();
}
}