From c4d78cd61cb4a71bbb24bf9be7d452cc66910b93 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 --- src/providers/wfs/qgsbasenetworkrequest.cpp | 2 +- .../src/core/testqgsnetworkaccessmanager.cpp | 30 +++++++++++++++++-- .../python/test_qgsblockingnetworkrequest.py | 10 ++++--- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/providers/wfs/qgsbasenetworkrequest.cpp b/src/providers/wfs/qgsbasenetworkrequest.cpp index 5041b2d2a0aa9..1e9295a775a0b 100644 --- a/src/providers/wfs/qgsbasenetworkrequest.cpp +++ b/src/providers/wfs/qgsbasenetworkrequest.cpp @@ -513,7 +513,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 ee2be6dc264f3..c9db5dc481dce 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 1b039b23224a8..13a9d32c4f28d 100644 --- a/tests/src/python/test_qgsblockingnetworkrequest.py +++ b/tests/src/python/test_qgsblockingnetworkrequest.py @@ -82,10 +82,12 @@ def testGet(self): reply = request.reply() self.assertEqual(reply.error(), QNetworkReply.NetworkError.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.Attribute.HttpStatusCodeAttribute), 200)