From f9b6666e1d21225d02684cc8e7135d3313033008 Mon Sep 17 00:00:00 2001 From: Mate Barany Date: Wed, 19 Feb 2025 15:49:45 +0100 Subject: [PATCH] Do not keep the headers and message body in case of temporary redirect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The implementation of GET requests with body (QTBUG-112871) keeps the body after it has been redirected. However, in case of temporary redirect (status code 307) this seems to be an incorrect behavior. Reset the headers and the body in case of temporary redirect. Change-Id: I10be702b017a42cca27a37dfe2249da2f59c0328 Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit f6a5c7e011d24db22afa5a3bf92749b9bb9e9354) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 5581c629221948058da043179a2be2e6442bc597) --- src/network/access/qnetworkaccessmanager.cpp | 4 ++-- src/network/access/qnetworkreplyhttpimpl.cpp | 6 +++--- .../auto/network/access/qnetworkreply/tst_qnetworkreply.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/network/access/qnetworkaccessmanager.cpp b/src/network/access/qnetworkaccessmanager.cpp index b371730c3c..39a5c5c14b 100644 --- a/src/network/access/qnetworkaccessmanager.cpp +++ b/src/network/access/qnetworkaccessmanager.cpp @@ -788,7 +788,7 @@ QNetworkReply *QNetworkAccessManager::get(const QNetworkRequest &request) \note A GET request with a message body is not cached. \note If the request is redirected, the message body will be kept only if the status code is - 307 or 308. + 308. */ QNetworkReply *QNetworkAccessManager::get(const QNetworkRequest &request, QIODevice *data) @@ -806,7 +806,7 @@ QNetworkReply *QNetworkAccessManager::get(const QNetworkRequest &request, QIODev \note A GET request with a message body is not cached. \note If the request is redirected, the message body will be kept only if the status code is - 307 or 308. + 308. */ QNetworkReply *QNetworkAccessManager::get(const QNetworkRequest &request, const QByteArray &data) diff --git a/src/network/access/qnetworkreplyhttpimpl.cpp b/src/network/access/qnetworkreplyhttpimpl.cpp index 758729d1e3..481a7b72f0 100644 --- a/src/network/access/qnetworkreplyhttpimpl.cpp +++ b/src/network/access/qnetworkreplyhttpimpl.cpp @@ -1259,10 +1259,10 @@ void QNetworkReplyHttpImplPrivate::onRedirected(const QUrl &redirectUrl, int htt return; } - // If the original operation was a GET with a body and the status code is either - // 307 or 308 then keep the message body + // If the original operation was a GET with a body and the status code is + // 308 then keep the message body const bool getOperationKeepsBody = (operation == QNetworkAccessManager::GetOperation) - && (httpStatus == 307 || httpStatus == 308); + && httpStatus == 308; redirectRequest = createRedirectRequest(originalRequest, url, maxRedirectsRemaining); operation = getRedirectOperation(operation, httpStatus); diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp index c390655aea..023fe89512 100644 --- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp +++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp @@ -2310,7 +2310,7 @@ void tst_QNetworkReply::getWithBodyRedirected() QVERIFY(validateRedirectedResponseHeaders(reply)); // Verify that the message body has arrived to the server - if (status > 302) { + if (status > 307) { QVERIFY(server2.contentLength != 0); QCOMPARE(server2.contentLength, dataFromClientToServer.size()); QCOMPARE(server2.receivedData.right(dataFromClientToServer.size()), dataFromClientToServer);