diff options
author | Anton Kudryavtsev <anton.kudryavtsev@vk.team> | 2023-10-30 18:28:30 +0300 |
---|---|---|
committer | Anton Kudryavtsev <anton.kudryavtsev@vk.team> | 2023-10-30 22:32:28 +0300 |
commit | 08fff11232ca6ca5165ba76bd268881a8a0060a1 (patch) | |
tree | bfe0eb97e41cc798427e1710abf18e6b71ec0792 | |
parent | d8d5922f16f1710b66caf718c302b633d2f78b0b (diff) |
qnetworkcookiejar: optimize cookiesForUrl
Check cheap conditions first.
Use string view types more
Don't insert in the middle of list, just append and then sort
Change-Id: I1ad7c3459eab45b2289bfe044314eec7130d8153
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
-rw-r--r-- | src/network/access/qnetworkcookiejar.cpp | 43 |
1 files changed, 16 insertions, 27 deletions
diff --git a/src/network/access/qnetworkcookiejar.cpp b/src/network/access/qnetworkcookiejar.cpp index c83fb0574e..270408a4bb 100644 --- a/src/network/access/qnetworkcookiejar.cpp +++ b/src/network/access/qnetworkcookiejar.cpp @@ -200,49 +200,38 @@ QList<QNetworkCookie> QNetworkCookieJar::cookiesForUrl(const QUrl &url) const Q_D(const QNetworkCookieJar); const QDateTime now = QDateTime::currentDateTimeUtc(); QList<QNetworkCookie> result; - bool isEncrypted = url.scheme() == "https"_L1; + const bool isEncrypted = url.scheme() == "https"_L1; // scan our cookies for something that matches - QList<QNetworkCookie>::ConstIterator it = d->allCookies.constBegin(), - end = d->allCookies.constEnd(); - for ( ; it != end; ++it) { - if (!isParentDomain(url.host(), it->domain())) + for (const auto &cookie : std::as_const(d->allCookies)) { + if (!isEncrypted && cookie.isSecure()) continue; - if (!isParentPath(url.path(), it->path())) + if (!cookie.isSessionCookie() && cookie.expirationDate() < now) continue; - if (!(*it).isSessionCookie() && (*it).expirationDate() < now) + const QString urlHost = url.host(); + const QString cookieDomain = cookie.domain(); + if (!isParentDomain(urlHost, cookieDomain)) continue; - if ((*it).isSecure() && !isEncrypted) + if (!isParentPath(url.path(), cookie.path())) continue; - QString domain = it->domain(); + QStringView domain = cookieDomain; if (domain.startsWith(u'.')) /// Qt6?: remove when compliant with RFC6265 - domain = domain.mid(1); + domain = domain.sliced(1); #if QT_CONFIG(topleveldomain) - if (qIsEffectiveTLD(domain) && url.host() != domain) + if (urlHost != domain && qIsEffectiveTLD(domain)) continue; #else - if (!domain.contains(u'.') && url.host() != domain) + if (!domain.contains(u'.') && urlHost != domain) continue; #endif // topleveldomain - // insert this cookie into result, sorted by path - QList<QNetworkCookie>::Iterator insertIt = result.begin(); - while (insertIt != result.end()) { - if (insertIt->path().size() < it->path().size()) { - // insert here - insertIt = result.insert(insertIt, *it); - break; - } else { - ++insertIt; - } - } - - // this is the shortest path yet, just append - if (insertIt == result.end()) - result += *it; + result += cookie; } + auto longerPath = [](const auto &c1, const auto &c2) + { return c1.path().size() > c2.path().size(); }; + std::sort(result.begin(), result.end(), longerPath); return result; } |