12#include <QNetworkCookie>
20 : QNetworkCookieJar (parent)
26 FilterTrackingCookies_ = filter;
36 MatchDomainExactly_ = enabled;
51 return Save (allCookies ());
57 for (
const auto& cookie : cookies)
59 result += cookie.toRawForm ();
67 bool IsExpired (
const QNetworkCookie& cookie,
const QDateTime& now)
69 return !cookie.isSessionCookie () && cookie.expirationDate () < now;
77 for (
const auto& ba : data.split (
'\n'))
78 cookies << QNetworkCookie::parseCookies (ba);
80 const auto& now = QDateTime::currentDateTime ();
81 for (
const auto& cookie : cookies)
83 if (FilterTrackingCookies_ &&
84 cookie.name ().startsWith (
"__utm"))
87 if (IsExpired (cookie, now))
90 filteredCookies << cookie;
93 setAllCookies (filteredCookies);
98 const auto& cookies = allCookies ();
101 result.reserve (cookies.size ());
103 const auto& now = QDateTime::currentDateTime ();
106 for (
const auto& cookie : cookies)
108 if (IsExpired (cookie, now))
114 if (result.contains (cookie))
119 qDebug () << Q_FUNC_INFO << cookies.size () << result.size ();
120 setAllCookies (result);
122 if (!removed.isEmpty ())
132 for (
const auto& cookie : QNetworkCookieJar::cookiesForUrl (url))
133 if (!filtered.contains (cookie))
140 bool MatchDomain (
const QString& rawDomain,
const QString& rawCookieDomain)
142 auto normalize = [] (QStringView s)
144 return s.startsWith (
'.') ? s.mid (1) : s;
146 const auto& domain = normalize (rawDomain);
147 const auto& cookieDomain = normalize (rawCookieDomain);
149 if (domain == cookieDomain)
152 const auto idx = domain.indexOf (cookieDomain);
153 return idx > 0 && domain.at (idx - 1) ==
'.';
158 return std::any_of (list.begin (), list.end (),
159 [&str] (
const auto& rx) { return str == rx.pattern () || rx.match (str).hasMatch (); });
164 QList<QNetworkCookie> Added_;
165 QList<QNetworkCookie> Removed_;
168 auto CookieToTuple (
const QNetworkCookie& c)
170 return std::make_tuple (c.isHttpOnly (),
172 c.isSessionCookie (),
177 c.expirationDate ());
182 bool operator() (
const QNetworkCookie& left,
const QNetworkCookie& right)
const
184 return CookieToTuple (left) < CookieToTuple (right);
188 CookiesDiff CheckDifferences (
const QList<QNetworkCookie>& previousList,
189 const QList<QNetworkCookie>& currentList)
191 using Set_t = std::set<QNetworkCookie, CookieLess>;
192 const Set_t previous { previousList.begin (), previousList.end () };
193 const Set_t current { currentList.begin (), currentList.end () };
196 std::set_difference (previous.begin (), previous.end (),
197 current.begin (), current.end (),
198 std::back_inserter (diff.Removed_),
200 std::set_difference (current.begin (), current.end (),
201 previous.begin (), previous.end (),
202 std::back_inserter (diff.Added_),
214 filtered.reserve (cookieList.size ());
215 for (
auto cookie : cookieList)
217 if (cookie.domain ().isEmpty ())
218 cookie.setDomain (url.host ());
220 bool checkWhitelist =
false;
223 if (checkWhitelist && Check (WL_, cookie.domain ()))
227 if (MatchDomainExactly_ && !MatchDomain (url.host (), cookie.domain ()))
229 checkWhitelist =
true;
233 if (FilterTrackingCookies_ &&
234 cookie.name ().startsWith (
"__utm"))
236 checkWhitelist =
true;
240 if (!Check (BL_, cookie.domain ()))
245 if (existing.isEmpty ())
249 const auto& diff = CheckDifferences (existing, filtered);
250 if (!diff.Removed_.isEmpty ())
252 if (!diff.Added_.isEmpty ())
256 return QNetworkCookieJar::setCookiesFromUrl (filtered, url);
void SetFilterTrackingCookies(bool filter)
void SetEnabled(bool enabled)
Enables or disables the cookies.
void SetExactDomainMatch(bool enabled)
Sets whether exact domain matching is enabled.
void SetWhitelist(const QList< QRegularExpression > &list)
Sets the cookies whitelist.
void cookiesRemoved(const QList< QNetworkCookie > &)
void SetBlacklist(const QList< QRegularExpression > &list)
Sets the cookies blacklist.
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.
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.