diff options
-rw-r--r-- | src/network/access/qhttpnetworkheader.cpp | 12 | ||||
-rw-r--r-- | src/network/access/qnetworkrequest.cpp | 10 | ||||
-rw-r--r-- | tests/auto/qnetworkreply/tst_qnetworkreply.cpp | 22 |
3 files changed, 41 insertions, 3 deletions
diff --git a/src/network/access/qhttpnetworkheader.cpp b/src/network/access/qhttpnetworkheader.cpp index 669f9cf..3eb2f3b 100644 --- a/src/network/access/qhttpnetworkheader.cpp +++ b/src/network/access/qhttpnetworkheader.cpp @@ -60,7 +60,17 @@ QHttpNetworkHeaderPrivate::QHttpNetworkHeaderPrivate(const QHttpNetworkHeaderPri qint64 QHttpNetworkHeaderPrivate::contentLength() const { bool ok = false; - QByteArray value = headerField("content-length"); + // We are not using the headerField() method here because servers might send us multiple content-length + // headers which is crap (see QTBUG-15311). Therefore just take the first content-length header field. + QByteArray value; + QList<QPair<QByteArray, QByteArray> >::ConstIterator it = fields.constBegin(), + end = fields.constEnd(); + for ( ; it != end; ++it) + if (qstricmp("content-length", it->first) == 0) { + value = it->second; + break; + } + qint64 length = value.toULongLong(&ok); if (ok) return length; diff --git a/src/network/access/qnetworkrequest.cpp b/src/network/access/qnetworkrequest.cpp index b761af5..162392d 100644 --- a/src/network/access/qnetworkrequest.cpp +++ b/src/network/access/qnetworkrequest.cpp @@ -899,10 +899,16 @@ void QNetworkHeadersPrivate::parseAndSetHeader(const QByteArray &key, const QByt // is it a known header? QNetworkRequest::KnownHeaders parsedKey = parseHeaderName(key); if (parsedKey != QNetworkRequest::KnownHeaders(-1)) { - if (value.isNull()) + if (value.isNull()) { cookedHeaders.remove(parsedKey); - else + } else if (parsedKey == QNetworkRequest::ContentLengthHeader + && cookedHeaders.contains(QNetworkRequest::ContentLengthHeader)) { + // Only set the cooked header "Content-Length" once. + // See bug QTBUG-15311 + } else { cookedHeaders.insert(parsedKey, parseHeaderValue(parsedKey, value)); + } + } } diff --git a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp index a986438..41b3e0a 100644 --- a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp @@ -301,6 +301,8 @@ private Q_SLOTS: void httpWithNoCredentialUsage(); + void qtbug15311doubleContentLength(); + // NOTE: This test must be last! void parentingRepliesToTheApp(); }; @@ -4724,6 +4726,26 @@ void tst_QNetworkReply::httpWithNoCredentialUsage() QCOMPARE(reply->error(), QNetworkReply::AuthenticationRequiredError); } +void tst_QNetworkReply::qtbug15311doubleContentLength() +{ + QByteArray response("HTTP/1.0 200 OK\r\nContent-Length: 3\r\nServer: bogus\r\nContent-Length: 3\r\n\r\nABC"); + MiniHttpServer server(response); + server.doClose = true; + + QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort()))); + QNetworkReplyPtr reply = manager.get(request); + + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + QTestEventLoop::instance().enterLoop(10); + QVERIFY(!QTestEventLoop::instance().timeout()); + QVERIFY(reply->isFinished()); + QCOMPARE(reply->error(), QNetworkReply::NoError); + QCOMPARE(reply->size(), qint64(3)); + QCOMPARE(reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(), qint64(3)); + QCOMPARE(reply->rawHeader("Content-length"), QByteArray("3, 3")); + QCOMPARE(reply->readAll(), QByteArray("ABC")); +} + // NOTE: This test must be last testcase in tst_qnetworkreply! |