12#include <QNetworkCookie>
16#include <QtConcurrentRun>
23 : QNetworkCookieJar (parent)
29 FilterTrackingCookies_ = filter;
39 MatchDomainExactly_ = enabled;
54 auto cookies = allCookies ();
56 for (
const auto& cookie : cookies)
58 result += cookie.toRawForm ();
66 bool IsExpired (
const QNetworkCookie& cookie,
const QDateTime& now)
68 return !cookie.isSessionCookie () && cookie.expirationDate () < now;
75 for (
const auto& ba : data.split (
'\n'))
76 cookies << QNetworkCookie::parseCookies (ba);
78 const auto& now = QDateTime::currentDateTime ();
79 for (
const auto& cookie : cookies)
81 if (FilterTrackingCookies_ &&
82 cookie.name ().startsWith (
"__utm"))
85 if (IsExpired (cookie, now))
88 filteredCookies << cookie;
91 setAllCookies (filteredCookies);
96 const auto& cookies = allCookies ();
98 const auto& now = QDateTime::currentDateTime ();
99 for (
const auto& cookie : cookies)
101 if (IsExpired (cookie, now))
104 if (result.contains (cookie))
109 qDebug () << Q_FUNC_INFO << cookies.size () << result.size ();
110 setAllCookies (result);
119 for (
const auto& cookie : QNetworkCookieJar::cookiesForUrl (url))
120 if (!filtered.contains (cookie))
127 bool MatchDomain (
const QString& rawDomain,
const QString& rawCookieDomain)
129 auto normalize = [] (
const QString& s)
131 return s.startsWith (
'.') ? s.midRef (1) : QStringRef { &s };
133 const auto& domain = normalize (rawDomain);
134 const auto& cookieDomain = normalize (rawCookieDomain);
136 if (domain == cookieDomain)
139 const auto idx = domain.indexOf (cookieDomain);
140 return idx > 0 && domain.at (idx - 1) ==
'.';
145 return std::any_of (list.begin (), list.end (),
146 [&str] (
const auto& rx) { return str == rx.pattern () || rx.exactMatch (str); });
155 auto CookieToTuple (
const QNetworkCookie& c)
157 return std::make_tuple (c.isHttpOnly (),
159 c.isSessionCookie (),
164 c.expirationDate ());
169 bool operator() (
const QNetworkCookie& left,
const QNetworkCookie& right)
const
171 return CookieToTuple (left) < CookieToTuple (right);
178 using Set_t = std::set<QNetworkCookie, CookieLess>;
179 Set_t previous { previousList.begin (), previousList.end () };
180 Set_t current { currentList.begin (), currentList.end () };
183 std::set_difference (previous.begin (), previous.end (),
184 current.begin (), current.end (),
185 std::back_inserter (diff.Removed_),
187 std::set_difference (current.begin (), current.end (),
188 previous.begin (), previous.end (),
189 std::back_inserter (diff.Added_),
201 filtered.reserve (cookieList.size ());
202 for (
auto cookie : cookieList)
204 if (cookie.domain ().isEmpty ())
205 cookie.setDomain (url.host ());
207 bool checkWhitelist =
false;
210 if (checkWhitelist && Check (WL_, cookie.domain ()))
214 if (MatchDomainExactly_ && !MatchDomain (url.host (), cookie.domain ()))
216 checkWhitelist =
true;
220 if (FilterTrackingCookies_ &&
221 cookie.name ().startsWith (
"__utm"))
223 checkWhitelist =
true;
227 if (!Check (BL_, cookie.domain ()))
232 if (existing.isEmpty ())
235 Util::Sequence (
this, QtConcurrent::run (CheckDifferences, existing, filtered)) >>
236 [
this] (
const CookiesDiff& diff)
238 if (!diff.Removed_.isEmpty ())
240 if (!diff.Added_.isEmpty ())
244 return QNetworkCookieJar::setCookiesFromUrl (filtered, url);
void SetBlacklist(const QList< QRegExp > &list)
Sets the cookies blacklist.
void SetFilterTrackingCookies(bool filter)
void SetWhitelist(const QList< QRegExp > &list)
Sets the cookies whitelist.
void SetEnabled(bool enabled)
Enables or disables the cookies.
void SetExactDomainMatch(bool enabled)
Sets whether exact domain matching is enabled.
void cookiesRemoved(const QList< QNetworkCookie > &)
bool setCookiesFromUrl(const QList< QNetworkCookie > &cookieList, const QUrl &url) override
Adds the cookieList for the given url to the jar.
void Load(const QByteArray &data)
QList< QNetworkCookie > cookiesForUrl(const QUrl &url) const override
Returns cookies for the given url.
void cookiesAdded(const QList< QNetworkCookie > &)
CustomCookieJar(QObject *parent=nullptr)
Constructs the cookie jar.
QList< QNetworkCookie > Added_
QList< QNetworkCookie > Removed_
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.