Add support for HTTP status 308 Permanent Redirect

308 Permanent Redirect was introduced after redirection support was
initially added to Qt.

[ChangeLog][QtNetwork][QNetworkAccessManager] Added support for HTTP status 308.

Task-number: QTBUG-63075
Change-Id: I1c6cda331d776237113ef8854de9abfe7e41ed3e
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Mårten Nordheim 2017-10-04 12:31:38 +02:00
parent d20aa0d50d
commit a402b3a02e
5 changed files with 45 additions and 3 deletions

View File

@ -553,7 +553,8 @@ void QHttpNetworkConnectionChannel::handleStatus()
case 302:
case 303:
case 305:
case 307: {
case 307:
case 308: {
// Parse the response headers and get the "location" url
QUrl redirectUrl = connection->d_func()->parseRedirectResponse(socket, reply);
if (redirectUrl.isValid())

View File

@ -96,7 +96,7 @@ void QHttpNetworkReply::setRedirectUrl(const QUrl &url)
bool QHttpNetworkReply::isHttpRedirect(int statusCode)
{
return (statusCode == 301 || statusCode == 302 || statusCode == 303
|| statusCode == 305 || statusCode == 307);
|| statusCode == 305 || statusCode == 307 || statusCode == 308);
}
qint64 QHttpNetworkReply::contentLength() const

View File

@ -297,7 +297,7 @@ QNetworkReplyPrivate::QNetworkReplyPrivate()
This signal is emitted if the QNetworkRequest::FollowRedirectsAttribute was
set in the request and the server responded with a 3xx status (specifically
301, 302, 303, 305 or 307 status code) with a valid url in the location
301, 302, 303, 305, 307 or 308 status code) with a valid url in the location
header, indicating a HTTP redirect. The \a url parameter contains the new
redirect url as returned by the server in the location header.

View File

@ -1200,6 +1200,7 @@ void QNetworkReplyHttpImplPrivate::checkForRedirect(const int statusCode)
case 302: // Found
case 303: // See Other
case 307: // Temporary Redirect
case 308: // Permanent Redirect
// What do we do about the caching of the HTML note?
// The response to a 303 MUST NOT be cached, while the response to
// all of the others is cacheable if the headers indicate it to be

View File

@ -488,6 +488,8 @@ private Q_SLOTS:
void ioHttpUserVerifiedRedirect_data();
void ioHttpUserVerifiedRedirect();
void ioHttpCookiesDuringRedirect();
void ioHttpRedirect_data();
void ioHttpRedirect();
#ifndef QT_NO_SSL
void putWithServerClosingConnectionImmediately();
#endif
@ -8438,6 +8440,44 @@ void tst_QNetworkReply::ioHttpCookiesDuringRedirect()
QVERIFY(target.receivedData.contains("\r\nCookie: hello=world\r\n"));
}
void tst_QNetworkReply::ioHttpRedirect_data()
{
QTest::addColumn<QString>("status");
QTest::addRow("301") << "301 Moved Permanently";
QTest::addRow("302") << "302 Found";
QTest::addRow("303") << "303 See Other";
QTest::addRow("305") << "305 Use Proxy";
QTest::addRow("307") << "307 Temporary Redirect";
QTest::addRow("308") << "308 Permanent Redirect";
}
void tst_QNetworkReply::ioHttpRedirect()
{
QFETCH(QString, status);
MiniHttpServer target(httpEmpty200Response, false);
QUrl targetUrl("http://localhost/");
targetUrl.setPort(target.serverPort());
QString redirectReply = QStringLiteral("HTTP/1.1 %1\r\n"
"Content-Type: text/plain\r\n"
"location: %2\r\n"
"\r\n").arg(status, targetUrl.toString());
MiniHttpServer redirectServer(redirectReply.toLatin1(), false);
QUrl url("http://localhost/");
url.setPort(redirectServer.serverPort());
QNetworkRequest request(url);
auto oldRedirectPolicy = manager.redirectPolicy();
manager.setRedirectPolicy(QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy);
QNetworkReplyPtr reply(manager.get(request));
// Set policy back to what it was
manager.setRedirectPolicy(oldRedirectPolicy);
QCOMPARE(waitForFinish(reply), int(Success));
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
}
#ifndef QT_NO_SSL
class PutWithServerClosingConnectionImmediatelyHandler: public QObject