Skip to content
Permalink
Browse files
Use preprocessed URLs for QgsTileCache tile keys and add unit test fo…
…r QgsNetworkAccessManager::setRequestPreprocessor
  • Loading branch information
mhugent committed Jun 21, 2021
1 parent 307bce8 commit 6221754e6cfc48920462dbe36c2af79e0e7b7bea
@@ -405,6 +405,7 @@ Abort any outstanding external browser login request.



void preprocessRequest( QNetworkRequest *req ) const;

signals:

@@ -831,6 +831,14 @@ bool QgsNetworkAccessManager::removeRequestPreprocessor( const QString &id )
return prevCount != sCustomPreprocessors.size();
}

void QgsNetworkAccessManager::preprocessRequest( QNetworkRequest *req ) const
{
for ( const auto &preprocessor : sCustomPreprocessors )
{
preprocessor.second( req );
}
}


//
// QgsNetworkRequestParameters
@@ -601,6 +601,7 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
static const inline QgsSettingsEntryInteger settingsNetworkTimeout = QgsSettingsEntryInteger( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), QgsSettings::NoSection, 60000, QObject::tr( "Network timeout" ) );
#endif

void preprocessRequest( QNetworkRequest *req ) const;

signals:

@@ -33,16 +33,20 @@ void QgsTileCache::insertTile( const QUrl &url, const QImage &image )

bool QgsTileCache::tile( const QUrl &url, QImage &image )
{
QNetworkRequest req( url );
QgsNetworkAccessManager::instance()->preprocessRequest( &req );
QUrl adjUrl = req.url();

QMutexLocker locker( &sTileCacheMutex );
bool success = false;
if ( QImage *i = sTileCache.object( url ) )
if ( QImage *i = sTileCache.object( adjUrl ) )
{
image = *i;
success = true;
}
else if ( QgsNetworkAccessManager::instance()->cache()->metaData( url ).isValid() )
else if ( QgsNetworkAccessManager::instance()->cache()->metaData( adjUrl ).isValid() )
{
if ( QIODevice *data = QgsNetworkAccessManager::instance()->cache()->data( url ) )
if ( QIODevice *data = QgsNetworkAccessManager::instance()->cache()->data( adjUrl ) )
{
QByteArray imageData = data->readAll();
delete data;
@@ -53,7 +57,7 @@ bool QgsTileCache::tile( const QUrl &url, QImage &image )
// Check for null because it could be a redirect (see: https://github.com/qgis/QGIS/issues/24336 )
if ( ! image.isNull( ) )
{
sTileCache.insert( url, new QImage( image ) );
sTileCache.insert( adjUrl, new QImage( image ) );
success = true;
}
}
@@ -150,6 +150,7 @@ class TestQgsNetworkAccessManager : public QObject
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void testRequestPreprocessor();
void testProxyExcludeList();
void fetchEmptyUrl(); //test fetching blank url
void fetchBadUrl(); //test fetching bad url
@@ -1128,6 +1129,14 @@ void TestQgsNetworkAccessManager::testCookieManagement()
evLoop.exec();
QVERIFY( thread2.getResult() );

void TestQgsNetworkAccessManager::testRequestPreprocessor()
{
QString processorId = QgsNetworkAccessManager::instance()->setRequestPreprocessor( []( QNetworkRequest * request ) { request->setHeader( QNetworkRequest::UserAgentHeader, QStringLiteral( "QGIS" ) );} );
QNetworkRequest request;
QgsNetworkAccessManager::instance()->preprocessRequest( &request );
QString userAgent = request.header( QNetworkRequest::UserAgentHeader ).toString();
QCOMPARE( userAgent, "QGIS" );
QgsNetworkAccessManager::instance()->removeRequestPreprocessor( processorId );
}

QGSTEST_MAIN( TestQgsNetworkAccessManager )

0 comments on commit 6221754

Please sign in to comment.