33 #include <QNetworkCookie>
36 #include <QtConcurrentRun>
45 : QNetworkCookieJar (parent)
51 FilterTrackingCookies_ = filter;
61 MatchDomainExactly_ = enabled;
78 for (
const auto& cookie : cookies)
80 if (cookie.isSessionCookie ())
83 result += cookie.toRawForm ();
92 for (
const auto& ba : data.split (
'\n'))
93 cookies << QNetworkCookie::parseCookies (ba);
95 const auto& now = QDateTime::currentDateTime ();
96 for (
const auto& cookie : cookies)
98 if (FilterTrackingCookies_ &&
99 cookie.name ().startsWith (
"__utm"))
102 if (cookie.expirationDate () < now)
105 filteredCookies << cookie;
108 setAllCookies (filteredCookies);
113 const auto& cookies = allCookies ();
115 const auto& now = QDateTime::currentDateTime ();
116 for (
const auto& cookie : cookies)
118 if (!cookie.isSessionCookie () &&
119 cookie.expirationDate () < now)
122 if (result.contains (cookie))
127 qDebug () << Q_FUNC_INFO << cookies.size () << result.size ();
128 setAllCookies (result);
137 for (
const auto& cookie : QNetworkCookieJar::cookiesForUrl (url))
138 if (!filtered.contains (cookie))
145 bool MatchDomain (QString domain, QString cookieDomain)
147 auto normalize = [] (QString& s)
149 if (s.startsWith (
'.'))
153 normalize (cookieDomain);
155 if (domain == cookieDomain)
158 const auto idx = domain.indexOf (cookieDomain);
159 return idx > 0 && domain.at (idx - 1) ==
'.';
164 for (
auto& rx : list)
165 if (str == rx.pattern () || rx.exactMatch (str))
177 auto CookieToTuple (
const QNetworkCookie& c)
179 return std::make_tuple (c.isHttpOnly (),
181 c.isSessionCookie (),
186 c.expirationDate ());
191 bool operator() (
const QNetworkCookie& left,
const QNetworkCookie& right)
const
193 return CookieToTuple (left) < CookieToTuple (right);
200 using Set_t = std::set<QNetworkCookie, CookieLess>;
201 Set_t previous { previousList.begin (), previousList.end () };
202 Set_t current { currentList.begin (), currentList.end () };
205 std::set_difference (previous.begin (), previous.end (),
206 current.begin (), current.end (),
207 std::back_inserter (diff.Removed_),
209 std::set_difference (current.begin (), current.end (),
210 previous.begin (), previous.end (),
211 std::back_inserter (diff.Added_),
223 filtered.reserve (cookieList.size ());
224 for (
auto cookie : cookieList)
226 if (cookie.domain ().isEmpty ())
227 cookie.setDomain (url.host ());
229 bool checkWhitelist =
false;
232 if (checkWhitelist && Check (WL_, cookie.domain ()))
236 if (MatchDomainExactly_ && !MatchDomain (url.host (), cookie.domain ()))
238 checkWhitelist =
true;
242 if (FilterTrackingCookies_ &&
243 cookie.name ().startsWith (
"__utm"))
245 checkWhitelist =
true;
249 if (!Check (BL_, cookie.domain ()))
254 if (existing.isEmpty ())
257 Util::Sequence (
this, QtConcurrent::run (CheckDifferences, existing, filtered)) >>
258 [
this] (
const CookiesDiff& diff)
260 if (!diff.Removed_.isEmpty ())
262 if (!diff.Added_.isEmpty ())
266 return QNetworkCookieJar::setCookiesFromUrl (filtered, url);
QList< QNetworkCookie > cookiesForUrl(const QUrl &url) const
Returns cookies for the given url.
void cookiesAdded(const QList< QNetworkCookie > &)
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
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.
QList< QNetworkCookie > Removed_
bool setCookiesFromUrl(const QList< QNetworkCookie > &cookieList, const QUrl &url)
Adds the cookieList for the given url to the jar.
QList< QNetworkCookie > Added_
void cookiesRemoved(const QList< QNetworkCookie > &)
void SetWhitelist(const QList< QRegExp > &list)
Sets the cookies whitelist.
detail::SequenceProxy< detail::SequencerRetType_t< QFuture< T > >, QFuture< T >, detail::EmptyDestructionTag > Sequence(QObject *parent, const QFuture< T > &future)
Creates a sequencer that allows chaining multiple futures.
void SetExactDomainMatch(bool enabled)
Sets whether exact domain matching is enabled.
void Load(const QByteArray &data)
void SetFilterTrackingCookies(bool filter)