From 8b1aa554ddaa0a226fac064f37b4eb762fd20d92 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Thu, 28 Nov 2024 07:53:11 +1000 Subject: [PATCH] Fix case-sensitivity of http header checks Since qt 6.8 header keys are forced to lowercase, so we need to adapt all checks accordingly (cherry picked from commit 682aae0e0fcde16c51d70d16fc6989f2d3863c63) --- src/providers/wfs/qgsbasenetworkrequest.cpp | 2 +- .../src/core/testqgsnetworkaccessmanager.cpp | 30 +++++++++++++++++-- .../python/test_qgsblockingnetworkrequest.py | 9 +++--- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/providers/wfs/qgsbasenetworkrequest.cpp b/src/providers/wfs/qgsbasenetworkrequest.cpp index f33d74cc3eac..763b3e82c9dd 100644 --- a/src/providers/wfs/qgsbasenetworkrequest.cpp +++ b/src/providers/wfs/qgsbasenetworkrequest.cpp @@ -511,7 +511,7 @@ QStringList QgsBaseNetworkRequest::sendOPTIONS( const QUrl &url ) for ( const auto &headerKeyValue : mResponseHeaders ) { - if ( headerKeyValue.first == QByteArray( "Allow" ) ) + if ( headerKeyValue.first.compare( QByteArray( "Allow" ), Qt::CaseInsensitive ) == 0 ) { allowValue = headerKeyValue.second; break; diff --git a/tests/src/core/testqgsnetworkaccessmanager.cpp b/tests/src/core/testqgsnetworkaccessmanager.cpp index ee2be6dc264f..c9db5dc481dc 100644 --- a/tests/src/core/testqgsnetworkaccessmanager.cpp +++ b/tests/src/core/testqgsnetworkaccessmanager.cpp @@ -439,7 +439,15 @@ void TestQgsNetworkAccessManager::fetchEncodedContent() { QCOMPARE( reply.error(), QNetworkReply::NoError ); QCOMPARE( reply.requestId(), requestId ); - QVERIFY( reply.rawHeaderList().contains( "Content-Length" ) ); + + // newer qt versions force headers to lower case, older ones didn't + QStringList lowerCaseRawHeaders; + for ( const QByteArray &header : reply.rawHeaderList() ) + { + lowerCaseRawHeaders.append( header.toLower() ); + } + + QVERIFY( lowerCaseRawHeaders.contains( "content-length" ) ); QCOMPARE( reply.request().url(), u ); loaded = true; } ); @@ -524,7 +532,15 @@ void TestQgsNetworkAccessManager::fetchPost() { QCOMPARE( reply.error(), QNetworkReply::NoError ); QCOMPARE( reply.requestId(), requestId ); - QVERIFY( reply.rawHeaderList().contains( "Content-Type" ) ); + + // newer qt versions force headers to lower case, older ones didn't + QStringList lowerCaseRawHeaders; + for ( const QByteArray &header : reply.rawHeaderList() ) + { + lowerCaseRawHeaders.append( header.toLower() ); + } + + QVERIFY( lowerCaseRawHeaders.contains( "content-type" ) ); QCOMPARE( reply.request().url(), u ); loaded = true; } ); @@ -614,7 +630,15 @@ void TestQgsNetworkAccessManager::fetchPostMultiPart() el.exec(); QCOMPARE( reply->error(), QNetworkReply::NoError ); - QVERIFY( reply->rawHeaderList().contains( "Content-Type" ) ); + + // newer qt versions force headers to lower case, older ones didn't + QStringList lowerCaseRawHeaders; + for ( const QByteArray &header : reply->rawHeaderList() ) + { + lowerCaseRawHeaders.append( header.toLower() ); + } + QVERIFY( lowerCaseRawHeaders.contains( "content-type" ) ); + QCOMPARE( reply->request().url(), u ); } diff --git a/tests/src/python/test_qgsblockingnetworkrequest.py b/tests/src/python/test_qgsblockingnetworkrequest.py index efee29f05626..342545a4e0ef 100644 --- a/tests/src/python/test_qgsblockingnetworkrequest.py +++ b/tests/src/python/test_qgsblockingnetworkrequest.py @@ -83,10 +83,11 @@ def testGet(self): reply = request.reply() self.assertEqual(reply.error(), QNetworkReply.NoError) self.assertEqual(reply.content(), '\n') - self.assertEqual(reply.rawHeaderList(), [b'Server', - b'Date', - b'Content-type', - b'Content-Length']) + # newer qt versions FORCE lowercase header keys, older ones didn't + self.assertEqual( + [h.data().decode().lower() for h in reply.rawHeaderList()], + ["server", "date", "content-type", "content-length"], + ) self.assertEqual(reply.rawHeader(b'Content-type'), 'text/html') self.assertEqual(reply.rawHeader(b'xxxxxxxxx'), '') self.assertEqual(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), 200)