-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookiejar.cpp
More file actions
214 lines (199 loc) · 8.58 KB
/
Copy pathcookiejar.cpp
File metadata and controls
214 lines (199 loc) · 8.58 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
#include <QDir>
#include <QSqlDriver>
#include <QSqlQuery>
#include <QSqlError>
#include <QMultiMap>
#include <QUuid>
#include "settings.h"
#include "options.h"
#include "cookiejar.h"
#include "qmc2main.h"
#include "macros.h"
extern MainWindow *qmc2MainWindow;
extern Settings *qmc2Config;
CookieJar::CookieJar(QObject *parent) : QNetworkCookieJar(parent)
{
QString userScopePath = Options::configPath();
db = QSqlDatabase::addDatabase("QSQLITE", "cookie-db-connection-" + QUuid::createUuid().toString());
db.setDatabaseName(qmc2Config->value(QMC2_FRONTEND_PREFIX + "WebBrowser/CookieDatabase", userScopePath + "/qmc2-" + QMC2_EMU_NAME_VARIANT.toLower() + "-cookies.db").toString());
if ( !db.open() ) {
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to open cookie database: error = '%1'").arg(db.lastError().text()));
return;
}
QStringList tables = db.driver()->tables(QSql::Tables);
if ( tables.count() != 1 || !tables.contains("qmc2_cookies") )
recreateDatabase();
static QStringList dbSyncModes = QStringList() << "OFF" << "NORMAL" << "FULL";
QSqlQuery query(db);
query.exec("PRAGMA synchronous = OFF");
query.exec("PRAGMA journal_mode = MEMORY");
}
CookieJar::~CookieJar()
{
if ( db.isOpen() ) {
saveCookies();
db.close();
}
}
void CookieJar::recreateDatabase()
{
if ( !db.isOpen() )
return;
QSqlQuery query(db);
if ( !query.exec("DROP TABLE IF EXISTS qmc2_cookies") ) {
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to remove cookie database: query = '%1', error = '%2'").arg(query.lastQuery()).arg(query.lastError().text()));
return;
}
query.finish();
// vaccum'ing the database frees all disk-space previously used
query.exec("VACUUM");
query.finish();
if ( !query.exec("CREATE TABLE qmc2_cookies (id INTEGER PRIMARY KEY, domain TEXT, name TEXT, value TEXT, path TEXT, expiry INTEGER, secure INTEGER, http_only INTEGER, CONSTRAINT qmc2_uniqueid UNIQUE (name, domain, path))") )
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to create cookie database: query = '%1', error = '%2'").arg(query.lastQuery()).arg(query.lastError().text()));
cookieMap.clear();
setAllCookies(QList<QNetworkCookie>());
}
QList<QNetworkCookie> CookieJar::cookiesForUrl(const QUrl &url) const
{
QString domain = url.host();
QString path = url.path();
QString defaultPath = path.left(path.lastIndexOf(QLatin1Char('/')) + 1);
if ( defaultPath.isEmpty() )
defaultPath = QLatin1Char('/');
QList<QNetworkCookie> cookieList;
if ( loadCookies(cookieList, domain, defaultPath) )
return cookieList;
else
return QNetworkCookieJar::cookiesForUrl(url);
}
bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
{
QString domain = url.host();
QString path = url.path();
QString defaultPath = path.left(path.lastIndexOf(QLatin1Char('/')) + 1);
if ( defaultPath.isEmpty() )
defaultPath = QLatin1Char('/');
for (int i = 0; i < cookieList.count(); i++) {
QNetworkCookie cookie = cookieList[i];
cookie.setDomain(domain);
cookie.setPath(defaultPath);
cookieMap.insertMulti(domain + defaultPath, cookie);
}
return QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
}
void CookieJar::saveCookies()
{
if ( !db.isOpen() )
return;
QSqlQuery query(db);
QDateTime now = QDateTime::currentDateTime();
QMapIterator<QString, QNetworkCookie> it(cookieMap);
QStringList cookieKeysProcessed;
db.driver()->beginTransaction();
while ( it.hasNext() ) {
it.next();
QNetworkCookie cookie = it.value();
QString cookieKey = cookie.domain() + cookie.path() + cookie.name();
if ( cookieKeysProcessed.contains(cookieKey) )
continue;
query.prepare("SELECT domain, name, path FROM qmc2_cookies WHERE domain=:domain AND path=:path AND name=:name");
query.bindValue(":domain", cookie.domain());
query.bindValue(":path", cookie.path());
query.bindValue(":name", cookie.name());
if ( query.exec() ) {
if ( query.next() ) {
query.finish();
if ( cookie.value().isEmpty() ) {
query.prepare("DELETE FROM qmc2_cookies WHERE domain=:domain AND path=:path AND name=:name");
query.bindValue(":domain", cookie.domain());
query.bindValue(":path", cookie.path());
query.bindValue(":name", cookie.name());
if ( !query.exec() )
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to remove expired cookie from database: query = '%1', error = '%2'").arg(query.lastQuery()).arg(query.lastError().text()));
query.finish();
cookieKeysProcessed << cookieKey;
} else if ( cookie.expirationDate() < now ) {
query.prepare("DELETE FROM qmc2_cookies WHERE domain=:domain AND path=:path AND name=:name");
query.bindValue(":domain", cookie.domain());
query.bindValue(":path", cookie.path());
query.bindValue(":name", cookie.name());
if ( !query.exec() )
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to remove expired cookie from database: query = '%1', error = '%2'").arg(query.lastQuery()).arg(query.lastError().text()));
query.finish();
cookieKeysProcessed << cookieKey;
} else if ( !cookie.isSessionCookie() ) {
query.prepare("UPDATE qmc2_cookies SET value=:value, expiry=" + QString::number(cookie.expirationDate().toTime_t()) + " WHERE domain=:domain AND path=:path AND name=:name");
query.bindValue(":value", cookie.value());
query.bindValue(":domain", cookie.domain());
query.bindValue(":path", cookie.path());
query.bindValue(":name", cookie.name());
if ( !query.exec() )
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to update cookie in database: query = '%1', error = '%2'").arg(query.lastQuery()).arg(query.lastError().text()));
query.finish();
cookieKeysProcessed << cookieKey;
}
} else {
query.finish();
if ( cookie.expirationDate() > now && !cookie.isSessionCookie() ) {
query.prepare("INSERT INTO qmc2_cookies (domain, name, value, path, expiry, secure, http_only) VALUES (:domain, :name, :value, :path, " + QString::number(cookie.expirationDate().toTime_t()) + ", " + QString(cookie.isSecure() ? "1" : "0") + ", " + QString(cookie.isHttpOnly() ? "1" : "0") + ")");
query.bindValue(":value", cookie.value());
query.bindValue(":domain", cookie.domain());
query.bindValue(":path", cookie.path());
query.bindValue(":name", cookie.name());
if ( !query.exec() )
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to add cookie to database: query = '%1', error = '%2'").arg(query.lastQuery()).arg(query.lastError().text()));
query.finish();
cookieKeysProcessed << cookieKey;
}
}
} else
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to query cookie database: query = '%1', error = '%2'").arg(query.lastQuery()).arg(query.lastError().text()));
}
db.driver()->commitTransaction();
}
bool CookieJar::loadCookies(QList<QNetworkCookie> &cookieList, QString domain, QString path) const
{
cookieList.clear();
if ( cookieMap.contains(domain + path) ) {
cookieList = cookieMap.values(domain + path);
return !cookieList.isEmpty();
}
if ( !db.isOpen() )
return false;
QSqlQuery query(db);
query.prepare("SELECT domain, name, value, path, expiry, secure, http_only FROM qmc2_cookies WHERE domain=:domain AND path=:path");
query.bindValue(":domain", domain);
query.bindValue(":path", path);
if ( !query.exec() ) {
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to fetch cookies from database: query = '%1', error = '%2'").arg(query.lastQuery()).arg(query.lastError().text()));
return false;
}
QDateTime now = QDateTime::currentDateTime();
QDateTime dt;
db.driver()->beginTransaction();
while ( query.next() ) {
QNetworkCookie cookie;
cookie.setDomain(query.value(0).toString());
cookie.setName(query.value(1).toByteArray());
cookie.setValue(query.value(2).toByteArray());
cookie.setPath(query.value(3).toString());
dt.setTime_t((uint) query.value(4).toULongLong());
cookie.setExpirationDate(dt);
cookie.setSecure(query.value(5).toBool());
cookie.setHttpOnly(query.value(6).toBool());
if ( dt < now ) {
QSqlQuery delquery(db);
delquery.prepare("DELETE FROM qmc2_cookies WHERE domain=:domain AND path=:path AND name=:name");
delquery.bindValue(":domain", cookie.domain());
delquery.bindValue(":path", cookie.path());
delquery.bindValue(":name", cookie.name());
if ( !delquery.exec() )
qmc2MainWindow->log(QMC2_LOG_FRONTEND, tr("WARNING: failed to remove expired cookie from database: query = '%1', error = '%2'").arg(delquery.lastQuery()).arg(query.lastError().text()));
} else {
cookieList << cookie;
cookieMap.insertMulti(domain + path, cookie);
}
}
db.driver()->commitTransaction();
return !cookieList.isEmpty();
}