From a78e1848118b16cbf517d14926663d175e3fb927 Mon Sep 17 00:00:00 2001 From: shiroki Date: Wed, 25 May 2011 11:11:51 +0200 Subject: [PATCH] fix "Host" header of ipv6 URLs in QNAM Change-Id: I6bf3320e5ab285e3d1f4d72bd1ef0a0e42813e5b Reviewed-on: http://codereview.qt.nokia.com/115 Reviewed-by: Markus Goetz --- src/network/access/qhttpnetworkconnection.cpp | 12 +++++++++++- tests/auto/qnetworkreply/tst_qnetworkreply.cpp | 18 +++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/network/access/qhttpnetworkconnection.cpp b/src/network/access/qhttpnetworkconnection.cpp index 33124fd..d950af4 100644 --- a/src/network/access/qhttpnetworkconnection.cpp +++ b/src/network/access/qhttpnetworkconnection.cpp @@ -261,7 +261,17 @@ void QHttpNetworkConnectionPrivate::prepareRequest(HttpMessagePair &messagePair) // set the host value = request.headerField("host"); if (value.isEmpty()) { - QByteArray host = QUrl::toAce(hostName); + QHostAddress add; + QByteArray host; + if (add.setAddress(hostName)) { + if (add.protocol() == QAbstractSocket::IPv6Protocol) + host = "[" + hostName.toAscii() + "]";//format the ipv6 in the standard way + else + host = hostName.toAscii(); + + } else { + host = QUrl::toAce(hostName); + } int port = request.url().port(); if (port != -1) { diff --git a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp index 4f5bd19..67b6e5c 100644 --- a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp @@ -457,9 +457,9 @@ public: : client(0), dataToTransmit(data), doClose(true), doSsl(ssl), ipv6(useipv6), multiple(false), totalConnections(0) { - if( useipv6 ){ + if (useipv6) { listen(QHostAddress::AnyIPv6); - }else{ + } else { listen(); } if (thread) { @@ -2338,8 +2338,9 @@ void tst_QNetworkReply::connectToIPv6Address_data() QTest::addColumn("url"); QTest::addColumn("error"); QTest::addColumn("dataToSend"); - QTest::addColumn("serverVerifyData"); - QTest::newRow("localhost") << QUrl(QByteArray("http://[::1]")) << QNetworkReply::NoError<< QByteArray("localhost") << QByteArray("\r\nHost: [::1]\r\n"); + QTest::addColumn("hostfield"); + QTest::newRow("localhost") << QUrl(QByteArray("http://[::1]")) << QNetworkReply::NoError<< QByteArray("localhost") << QByteArray("[::1]"); + //QTest::newRow("ipv4localhost") << QUrl(QByteArray("http://127.0.0.1")) << QNetworkReply::NoError<< QByteArray("ipv4localhost") << QByteArray("127.0.0.1"); //to add more test data here } @@ -2348,7 +2349,7 @@ void tst_QNetworkReply::connectToIPv6Address() QFETCH(QUrl, url); QFETCH(QNetworkReply::NetworkError, error); QFETCH(QByteArray, dataToSend); - QFETCH(QByteArray, serverVerifyData); + QFETCH(QByteArray, hostfield); QByteArray httpResponse = QByteArray("HTTP/1.0 200 OK\r\nContent-Length: "); httpResponse += QByteArray::number(dataToSend.size()); @@ -2366,10 +2367,9 @@ void tst_QNetworkReply::connectToIPv6Address() QTestEventLoop::instance().enterLoop(10); QVERIFY(!QTestEventLoop::instance().timeout()); QByteArray content = reply->readAll(); - if( !serverVerifyData.isEmpty()){ - //qDebug() << server.receivedData; - //QVERIFY(server.receivedData.contains(serverVerifyData)); //got a bug here - } + //qDebug() << server.receivedData; + QByteArray hostinfo = "\r\nHost: " + hostfield + ":" + QByteArray::number(server.serverPort()) + "\r\n"; + QVERIFY(server.receivedData.contains(hostinfo)); QVERIFY(content == dataToSend); QCOMPARE(reply->url(), request.url()); QVERIFY(reply->error() == error); -- 2.7.4