diff --git a/webenginepart/autotests/webenginepartcookiejar_test.cpp b/webenginepart/autotests/webenginepartcookiejar_test.cpp --- a/webenginepart/autotests/webenginepartcookiejar_test.cpp +++ b/webenginepart/autotests/webenginepartcookiejar_test.cpp @@ -325,10 +325,6 @@ QNetworkCookie c = d.cookie(); QDBusError e = addCookieToKCookieServer(c, d.host); QVERIFY2(!e.isValid(), qPrintable(e.message())); - //In case of an empty domain, WebEnginePartCookieJar will use QNetworkCookie::normalize on the cookie - if (c.domain().isEmpty()) { - c.setDomain(d.host); - } expected << c; } m_jar = new WebEnginePartCookieJar(m_profile, this); diff --git a/webenginepart/src/webenginepartcookiejar.h b/webenginepart/src/webenginepartcookiejar.h --- a/webenginepart/src/webenginepartcookiejar.h +++ b/webenginepart/src/webenginepartcookiejar.h @@ -124,8 +124,14 @@ * @return the ID of the window determined as described above or 0 if no such a window can be found */ static qlonglong findWinID(); - - using CookieList = QList; + + struct CookieWithUrl { + QNetworkCookie cookie; + QUrl url; + }; + + using CookieUrlList = QVector; + using CookieList = QVector; /** * @brief An identifier for a cookie @@ -220,7 +226,7 @@ * @brief Finds all cookies stored in `KCookieJar` * @return a list of the cookies in `KCookieJar` */ - CookieList findKIOCookies(); + CookieUrlList findKIOCookies(); /** * @brief Enum describing the possible fields to pas to `KCookieServer::findCookies` using DBus. @@ -239,7 +245,7 @@ * @param start: the position in the list where the data for the cookie starts. * @return The cookie described by the data and its host */ - static QNetworkCookie parseKIOCookie(const QStringList &data, int start); + static CookieWithUrl parseKIOCookie(const QStringList &data, int start); #if QTWEBENGINE_VERSION >= QT_VERSION_CHECK(5,11,0) /** diff --git a/webenginepart/src/webenginepartcookiejar.cpp b/webenginepart/src/webenginepartcookiejar.cpp --- a/webenginepart/src/webenginepartcookiejar.cpp +++ b/webenginepart/src/webenginepartcookiejar.cpp @@ -144,7 +144,7 @@ //the cookie back to KCookieServer; instead, remove it from the list. if (m_cookiesLoadedFromKCookieServer.removeOne(_cookie)) { return; - } + } #ifdef BUILD_TESTING m_testCookies.clear(); @@ -285,24 +285,27 @@ void WebEnginePartCookieJar::loadKIOCookies() { - CookieList cookies = findKIOCookies(); - foreach(const QNetworkCookie& cookie, cookies){ + const CookieUrlList cookies = findKIOCookies(); + for (const CookieWithUrl& cookieWithUrl : cookies){ + QNetworkCookie cookie = cookieWithUrl.cookie; QDateTime currentTime = QDateTime::currentDateTime(); //Don't attempt to add expired cookies if (cookie.expirationDate().isValid() && cookie.expirationDate() < currentTime) { continue; } + QNetworkCookie normalizedCookie(cookie); + normalizedCookie.normalize(cookieWithUrl.url); m_cookiesLoadedFromKCookieServer << cookie; #ifdef BUILD_TESTING m_testCookies << cookie; #endif - m_cookieStore->setCookie(cookie); + m_cookieStore->setCookie(cookie, cookieWithUrl.url); } } -WebEnginePartCookieJar::CookieList WebEnginePartCookieJar::findKIOCookies() +WebEnginePartCookieJar::CookieUrlList WebEnginePartCookieJar::findKIOCookies() { - CookieList res; + CookieUrlList res; if (!m_cookieServer.isValid()) { return res; } @@ -327,24 +330,28 @@ return res; } -QNetworkCookie WebEnginePartCookieJar::parseKIOCookie(const QStringList& data, int start) +//This function used to return a normalized cookie. However, doing so doesn't work correctly because QWebEngineCookieStore::setCookie +//in turns normalizes the cookie. One could think that calling normalize twice wouldn't be a problem, but that would be wrong. If the cookie +//domain is originally empty, after a call to normalize it will contain the cookie origin host. The second call to normalize will see a cookie +//whose domain is not empty and doesn't start with a dot and will add a dot to it. +WebEnginePartCookieJar::CookieWithUrl WebEnginePartCookieJar::parseKIOCookie(const QStringList& data, int start) { QNetworkCookie c; auto extractField = [data, start](CookieDetails field){return data.at(start + static_cast(field));}; - c.setDomain(extractField(CookieDetails::domain).toUtf8()); + c.setDomain(extractField(CookieDetails::domain)); c.setExpirationDate(QDateTime::fromSecsSinceEpoch(extractField(CookieDetails::expirationDate).toInt())); c.setName(extractField(CookieDetails::name).toUtf8()); - c.setPath(extractField(CookieDetails::path).toUtf8()); + QString path = extractField(CookieDetails::path); + c.setPath(path); c.setSecure(extractField(CookieDetails::secure).toInt()); //1 for true, 0 for false c.setValue(extractField(CookieDetails::value).toUtf8()); - if (c.domain().isEmpty()) { - QString host = extractField(CookieDetails::host); - QUrl url; - url.setScheme(c.isSecure() ? "https" : "http"); - url.setHost(host); - c.normalize(url); - } - return c; + + QString host = extractField(CookieDetails::host); + QUrl url; + url.setScheme(c.isSecure() ? "https" : "http"); + url.setHost(host); + url.setPath(path); + return CookieWithUrl{c, url}; } QDebug operator<<(QDebug deb, const WebEnginePartCookieJar::CookieIdentifier& id)