From d216c0fd62a879fd1eb84e087f70c7f542d75d58 Mon Sep 17 00:00:00 2001 From: Peter Hartmann Date: Thu, 14 May 2009 11:21:25 +0200 Subject: [PATCH] HTTP authentication: return error if authentication cannot be handled return error upon receiving an unknown authentication method (e.g. WSSE); before we were just silently returning. Reviewed-by: Thiago Macieira --- src/network/access/qhttpnetworkconnection.cpp | 20 +++++++--- tests/auto/qnetworkreply/tst_qnetworkreply.cpp | 52 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/network/access/qhttpnetworkconnection.cpp b/src/network/access/qhttpnetworkconnection.cpp index f558f2bff5..ae518df09f 100644 --- a/src/network/access/qhttpnetworkconnection.cpp +++ b/src/network/access/qhttpnetworkconnection.cpp @@ -643,10 +643,21 @@ void QHttpNetworkConnectionPrivate::handleStatus(QAbstractSocket *socket, QHttpN switch (statusCode) { case 401: case 407: - handleAuthenticateChallenge(socket, reply, (statusCode == 407), resend); - if (resend) { - eraseData(reply); - sendRequest(socket); + if (handleAuthenticateChallenge(socket, reply, (statusCode == 407), resend)) { + if (resend) { + eraseData(reply); + sendRequest(socket); + } + } else { + int i = indexOf(socket); + emit channels[i].reply->headerChanged(); + emit channels[i].reply->readyRead(); + QNetworkReply::NetworkError errorCode = (statusCode == 407) + ? QNetworkReply::ProxyAuthenticationRequiredError + : QNetworkReply::AuthenticationRequiredError; + reply->d_func()->errorString = errorDetail(errorCode, socket); + emit q->error(errorCode, reply->d_func()->errorString); + emit channels[i].reply->finished(); } break; default: @@ -749,7 +760,6 @@ bool QHttpNetworkConnectionPrivate::handleAuthenticateChallenge(QAbstractSocket // authentication is cancelled, send the current contents to the user. emit channels[i].reply->headerChanged(); emit channels[i].reply->readyRead(); - emit channels[i].reply->finished(); QNetworkReply::NetworkError errorCode = isProxy ? QNetworkReply::ProxyAuthenticationRequiredError diff --git a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp index 4be0863eec..bb199b9074 100644 --- a/tests/auto/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/qnetworkreply/tst_qnetworkreply.cpp @@ -217,6 +217,8 @@ private Q_SLOTS: void httpProxyCommands_data(); void httpProxyCommands(); void proxyChange(); + void authorizationError_data(); + void authorizationError(); }; QT_BEGIN_NAMESPACE @@ -3012,5 +3014,55 @@ void tst_QNetworkReply::proxyChange() QVERIFY(int(reply3->error()) > 0); } +void tst_QNetworkReply::authorizationError_data() +{ + + QTest::addColumn("url"); + QTest::addColumn("errorSignalCount"); + QTest::addColumn("finishedSignalCount"); + QTest::addColumn("error"); + QTest::addColumn("httpStatusCode"); + QTest::addColumn("httpBody"); + + QTest::newRow("unknown-authorization-method") << "http://" + QtNetworkSettings::serverName() + + "/cgi-bin/http-unknown-authentication-method.cgi?401-authorization-required" << 1 << 1 + << int(QNetworkReply::AuthenticationRequiredError) << 401 << "authorization required"; + QTest::newRow("unknown-proxy-authorization-method") << "http://" + QtNetworkSettings::serverName() + + "/cgi-bin/http-unknown-authentication-method.cgi?407-proxy-authorization-required" << 1 << 1 + << int(QNetworkReply::ProxyAuthenticationRequiredError) << 407 + << "authorization required"; +} + +void tst_QNetworkReply::authorizationError() +{ + QFETCH(QString, url); + QNetworkRequest request(url); + QNetworkReplyPtr reply = manager.get(request); + + QCOMPARE(reply->error(), QNetworkReply::NoError); + + qRegisterMetaType("QNetworkReply::NetworkError"); + QSignalSpy errorSpy(reply, SIGNAL(error(QNetworkReply::NetworkError))); + QSignalSpy finishedSpy(reply, SIGNAL(finished())); + // now run the request: + connect(reply, SIGNAL(finished()), + &QTestEventLoop::instance(), SLOT(exitLoop())); + QTestEventLoop::instance().enterLoop(10); + QVERIFY(!QTestEventLoop::instance().timeout()); + + QFETCH(int, errorSignalCount); + QCOMPARE(errorSpy.count(), errorSignalCount); + QFETCH(int, finishedSignalCount); + QCOMPARE(finishedSpy.count(), finishedSignalCount); + QFETCH(int, error); + QCOMPARE(reply->error(), QNetworkReply::NetworkError(error)); + + QFETCH(int, httpStatusCode); + QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), httpStatusCode); + + QFETCH(QString, httpBody); + QCOMPARE(QString(reply->readAll()), httpBody); +} + QTEST_MAIN(tst_QNetworkReply) #include "tst_qnetworkreply.moc" -- 2.11.4.GIT