116static inline bool isParentPath(QStringView path, QStringView reference)
118 if ((path.isEmpty() && reference ==
"/"_L1) || path.startsWith(reference)) {
120 if (path.size() == reference.size())
124 if (reference.endsWith(u'/'))
129 if (path.at(reference.size()) == u'/')
164bool QNetworkCookieJar::setCookiesFromUrl(
const QList<QNetworkCookie> &cookieList,
168 for (QNetworkCookie cookie : cookieList) {
169 cookie.normalize(url);
170 if (validateCookie(cookie, url) && insertCookie(cookie))
194QList<QNetworkCookie> QNetworkCookieJar::cookiesForUrl(
const QUrl &url)
const
201 Q_D(
const QNetworkCookieJar);
202 const QDateTime now = QDateTime::currentDateTimeUtc();
203 QList<QNetworkCookie> result;
204 const bool isEncrypted = url.scheme() ==
"https"_L1;
207 for (
const auto &cookie : std::as_const(d->allCookies)) {
208 if (!isEncrypted && cookie.isSecure())
210 if (!cookie.isSessionCookie() && cookie.expirationDate() < now)
212 const QString urlHost = url.host();
213 const QString cookieDomain = cookie.domain();
214 if (!isParentDomain(urlHost, cookieDomain))
216 if (!isParentPath(url.path(), cookie.path()))
219 QStringView domain = cookieDomain;
220 if (domain.startsWith(u'.'))
221 domain = domain.sliced(1);
222#if QT_CONFIG(topleveldomain)
223 if (urlHost != domain && qIsEffectiveTLD(domain))
226 if (!domain.contains(u'.') && urlHost != domain)
233 auto longerPath = [](
const auto &c1,
const auto &c2)
234 {
return c1.path().size() > c2.path().size(); };
235 std::sort(result.begin(), result.end(), longerPath);
289bool QNetworkCookieJar::deleteCookie(
const QNetworkCookie &cookie)
291 Q_D(QNetworkCookieJar);
292 const auto it = std::find_if(d->allCookies.cbegin(), d->allCookies.cend(),
293 [&cookie](
const auto &c) {
return c.hasSameIdentifier(cookie); });
294 if (it != d->allCookies.cend()) {
295 d->allCookies.erase(it);
307bool QNetworkCookieJar::validateCookie(
const QNetworkCookie &cookie,
const QUrl &url)
const
309 const QString cookieDomain = cookie.domain();
310 QStringView domain = cookieDomain;
311 const QString host = url.host();
312 if (!isParentDomain(domain, host) && !isParentDomain(host, domain))
315 if (domain.startsWith(u'.'))
316 domain = domain.sliced(1);
328 return !qIsEffectiveTLD(domain);