forked from Cutehacks/duperagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookiejar.cpp
141 lines (114 loc) · 3.05 KB
/
cookiejar.cpp
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
// Copyright 2016 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QTextStream>
#include <QtNetwork/QNetworkCookie>
#include "cookiejar.h"
namespace com { namespace cutehacks { namespace duperagent {
CookieJar::CookieJar(const QString &path, QObject *parent) :
QNetworkCookieJar(parent),
m_savePath(path),
m_persistSessions(false)
{
load();
}
CookieJar::~CookieJar()
{
save();
}
bool CookieJar::insertCookie(const QNetworkCookie &cookie)
{
if (QNetworkCookieJar::insertCookie(cookie)) {
save(); // too aggressive to save on each insert?
return true;
}
return false;
}
bool CookieJar::deleteCookie(const QNetworkCookie &cookie)
{
if (QNetworkCookieJar::deleteCookie(cookie)) {
save();
return true;
}
return false;
}
void CookieJar::save() const
{
QFile file(m_savePath);
QDir dir = QFileInfo(file).dir();
if (!dir.mkpath(dir.absolutePath())) {
qWarning("Could not create path for writing: %s", qUtf8Printable(dir.path()));
return;
}
if (!file.open(QIODevice::WriteOnly)) {
qWarning("Could not open file for writing: %s", qUtf8Printable(m_savePath));
return;
}
QTextStream out(&file);
out.setCodec("UTF-8");
QList<QNetworkCookie> cookies = allCookies();
foreach (QNetworkCookie c, cookies) {
if (m_persistSessions || !c.isSessionCookie())
out << c.toRawForm() << endl;
}
file.close();
}
void CookieJar::load()
{
QFile file(m_savePath);
if (!file.open(QIODevice::ReadOnly)) {
return;
}
QTextStream in(&file);
in.setCodec("UTF-8");
QList<QNetworkCookie> cookies;
while (!in.atEnd()) {
QByteArray line = in.readLine().toUtf8();
cookies << QNetworkCookie::parseCookies(line);
}
file.close();
setAllCookies(cookies);
}
void CookieJar::addCookie(const QString &cookieString)
{
QList<QNetworkCookie> newCookies = QNetworkCookie::parseCookies(cookieString.toUtf8());
if (newCookies.length() == 0)
return;
QList<QNetworkCookie> cookies = allCookies();
foreach (QNetworkCookie cookie, newCookies) {
bool found = false;
foreach (QNetworkCookie existing, cookies) {
if (cookie.hasSameIdentifier(existing)) {
found = true;
if (!existing.isHttpOnly())
insertCookie(cookie);
break;
}
}
if (!found)
insertCookie(cookie);
}
}
void CookieJar::setPersistSessions(bool persist)
{
m_persistSessions = persist;
}
void CookieJar::clearAll()
{
setAllCookies(QList<QNetworkCookie>());
save();
}
QString CookieJar::cookies() const
{
QStringList cookieString;
QList<QNetworkCookie> cookies = allCookies();
foreach (QNetworkCookie c, cookies) {
if (!c.isHttpOnly()) {
cookieString << c.toRawForm();
}
}
return cookieString.join(";");
}
} } }