From ae293c1cb220847194fba6dcebdbb9194837bb56 Mon Sep 17 00:00:00 2001 From: Peter Hartmann Date: Tue, 26 Nov 2013 13:58:47 -0500 Subject: QNetworkCookie: allow cookies for IPv6 domains For IPv6 addresses don't call toAce as it returns the empty string. We should reflect the behavior of browsers here, which all accept cookies from IPv6 addresses. Original-patch-by: David Tapuska Task-number: QTBUG-35022 Change-Id: Ic00369e923d044ec459822b2405865c13e4185b6 Reviewed-by: Thiago Macieira Reviewed-by: Richard J. Moore --- src/network/access/qnetworkcookie.cpp | 16 ++++++++++++---- .../access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp | 6 ++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/network/access/qnetworkcookie.cpp b/src/network/access/qnetworkcookie.cpp index 55049280aa..664bc8282c 100644 --- a/src/network/access/qnetworkcookie.cpp +++ b/src/network/access/qnetworkcookie.cpp @@ -467,12 +467,19 @@ QByteArray QNetworkCookie::toRawForm(RawForm form) const } if (!d->domain.isEmpty()) { result += "; domain="; - QString domainNoDot = d->domain; - if (domainNoDot.startsWith(QLatin1Char('.'))) { + if (d->domain.startsWith(QLatin1Char('.'))) { result += '.'; - domainNoDot = domainNoDot.mid(1); + result += QUrl::toAce(d->domain.mid(1)); + } else { + QHostAddress hostAddr(d->domain); + if (hostAddr.protocol() == QAbstractSocket::IPv6Protocol) { + result += '['; + result += d->domain.toUtf8(); + result += ']'; + } else { + result += QUrl::toAce(d->domain); + } } - result += QUrl::toAce(domainNoDot); } if (!d->path.isEmpty()) { result += "; path="; @@ -1021,6 +1028,7 @@ void QNetworkCookie::normalize(const QUrl &url) } else { QHostAddress hostAddress(d->domain); if (hostAddress.protocol() != QAbstractSocket::IPv4Protocol + && hostAddress.protocol() != QAbstractSocket::IPv6Protocol && !d->domain.startsWith(QLatin1Char('.'))) { // Ensure the domain starts with a dot if its field was not empty // in the HTTP header. There are some servers that forget the diff --git a/tests/auto/network/access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp b/tests/auto/network/access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp index 25d91d63f5..bcfe9f090f 100644 --- a/tests/auto/network/access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp +++ b/tests/auto/network/access/qnetworkcookiejar/tst_qnetworkcookiejar.cpp @@ -217,6 +217,12 @@ void tst_QNetworkCookieJar::setCookiesFromUrl_data() result += cookie; QTest::newRow("IPv4-address-as-domain") << preset << cookie << "http://127.0.0.1/" << result << true; + result.clear(); + preset.clear(); + cookie.setDomain("fe80::250:56ff:fec0:1"); + result += cookie; + QTest::newRow("IPv6-address-as-domain") << preset << cookie << "http://[fe80::250:56ff:fec0:1]/" << result << true; + // setting the defaults: finalCookie = cookie; finalCookie.setPath("/something/"); -- cgit v1.2.3