32 #include <QNetworkCookie> 41 : QNetworkCookieJar (parent)
42 , FilterTrackingCookies_ (false)
44 , MatchDomainExactly_ (false)
54 FilterTrackingCookies_ = filter;
64 MatchDomainExactly_ = enabled;
81 for (
const auto& cookie : cookies)
83 if (cookie.isSessionCookie ())
86 result += cookie.toRawForm ();
95 for (
const auto& ba : data.split (
'\n'))
96 cookies << QNetworkCookie::parseCookies (ba);
98 const auto& now = QDateTime::currentDateTime ();
99 for (
const auto& cookie : cookies)
101 if (FilterTrackingCookies_ &&
102 cookie.name ().startsWith (
"__utm"))
105 if (cookie.expirationDate () < now)
108 filteredCookies << cookie;
110 setAllCookies (filteredCookies);
115 const auto& cookies = allCookies ();
117 const auto& now = QDateTime::currentDateTime ();
118 for (
const auto& cookie : cookies)
120 if (!cookie.isSessionCookie () &&
121 cookie.expirationDate () < now)
124 if (result.contains (cookie))
129 qDebug () << Q_FUNC_INFO << cookies.size () << result.size ();
130 setAllCookies (result);
139 for (
const auto& cookie : QNetworkCookieJar::cookiesForUrl (url))
140 if (!filtered.contains (cookie))
147 bool MatchDomain (QString domain, QString cookieDomain)
149 auto normalize = [] (QString& s)
151 if (s.startsWith (
'.'))
155 normalize (cookieDomain);
157 if (domain == cookieDomain)
160 const auto idx = domain.indexOf (cookieDomain);
161 return idx > 0 && domain.at (idx - 1) ==
'.';
166 for (
auto& rx : list)
167 if (str == rx.pattern () || rx.exactMatch (str))
180 filtered.reserve (cookieList.size ());
181 for (
auto cookie : cookieList)
183 if (cookie.domain ().isEmpty ())
184 cookie.setDomain (url.host ());
186 bool checkWhitelist =
false;
187 std::shared_ptr<void> wlGuard (
nullptr, [&] (
void*)
189 if (checkWhitelist && Check (WL_, cookie.domain ()))
193 if (MatchDomainExactly_ && !MatchDomain (url.host (), cookie.domain ()))
195 checkWhitelist =
true;
199 if (FilterTrackingCookies_ &&
200 cookie.name ().startsWith (
"__utm"))
202 checkWhitelist =
true;
206 if (!Check (BL_, cookie.domain ()))
210 return QNetworkCookieJar::setCookiesFromUrl (filtered, url);
QList< QNetworkCookie > cookiesForUrl(const QUrl &url) const
Returns cookies for the given url.
CustomCookieJar(QObject *parent=0)
Constructs the cookie jar.
void SetBlacklist(const QList< QRegExp > &list)
Sets the cookies blacklist.
void SetEnabled(bool enabled)
Enables or disables the cookies.
virtual ~CustomCookieJar()
bool setCookiesFromUrl(const QList< QNetworkCookie > &cookieList, const QUrl &url)
Adds the cookieList for the given url to the jar.
void SetWhitelist(const QList< QRegExp > &list)
Sets the cookies whitelist.
void SetExactDomainMatch(bool enabled)
Sets whether exact domain matching is enabled.
void Load(const QByteArray &data)
void SetFilterTrackingCookies(bool filter)