void QgsWFSRequest::replyFinished() { if ( !mIsAborted && mReply ) { if ( mReply->error() == QNetworkReply::NoError ) { QgsDebugMsg( "reply ok" ); QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute ); if ( !redirect.isNull() ) { QgsDebugMsg( "Request redirected." ); const QUrl& toUrl = redirect.toUrl(); mReply->request(); if ( toUrl == mReply->url() ) { mErrorMessage = tr( "Redirect loop detected: %1" ).arg( toUrl.toString() ); QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) ); mResponse.clear(); } else { QNetworkRequest request( toUrl ); if ( !mUri.auth().setAuthorization( request ) ) { mResponse.clear(); mErrorMessage = errorMessageFailedAuth(); mErrorCode = QgsWFSRequest::NetworkError; QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) ); emit downloadFinished(); return; } request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, mForceRefresh ? QNetworkRequest::AlwaysNetwork : QNetworkRequest::PreferCache ); request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true ); mReply->deleteLater(); mReply = nullptr; QgsDebugMsg( QString( "redirected: %1 forceRefresh=%2" ).arg( redirect.toString() ).arg( mForceRefresh ) ); mReply = QgsNetworkAccessManager::instance()->get( request ); connect( mReply, SIGNAL( finished() ), this, SLOT( replyFinished() ) ); connect( mReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( replyProgress( qint64, qint64 ) ) ); return; } } else { const QgsNetworkAccessManager *nam = QgsNetworkAccessManager::instance(); if ( nam->cache() ) { QNetworkCacheMetaData cmd = nam->cache()->metaData( mReply->request().url() ); QNetworkCacheMetaData::RawHeaderList hl; Q_FOREACH ( const QNetworkCacheMetaData::RawHeader &h, cmd.rawHeaders() ) { if ( h.first != "Cache-Control" ) hl.append( h ); } cmd.setRawHeaders( hl ); QgsDebugMsg( QString( "expirationDate:%1" ).arg( cmd.expirationDate().toString() ) ); if ( cmd.expirationDate().isNull() ) { cmd.setExpirationDate( QDateTime::currentDateTime().addSecs( defaultExpirationInSec() ) ); } nam->cache()->updateMetaData( cmd ); } else { QgsDebugMsg( "No cache!" ); } #ifdef QGISDEBUG bool fromCache = mReply->attribute( QNetworkRequest::SourceIsFromCacheAttribute ).toBool(); QgsDebugMsg( QString( "Reply was cached: %1" ).arg( fromCache ) ); #endif mResponse = mReply->readAll(); if ( mResponse.isEmpty() && !mGotNonEmptyResponse ) { mErrorMessage = tr( "empty response: %1" ).arg( mReply->errorString() ); mErrorCode = QgsWFSRequest::ServerExceptionError; QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) ); } }
void QgsWfsRequest::replyFinished() { if ( !mIsAborted && mReply ) { if ( mReply->error() == QNetworkReply::NoError ) { QgsDebugMsgLevel( QStringLiteral( "reply OK" ), 4 ); QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute ); if ( !redirect.isNull() ) { QgsDebugMsgLevel( QStringLiteral( "Request redirected." ), 4 ); const QUrl &toUrl = redirect.toUrl(); mReply->request(); if ( toUrl == mReply->url() ) { mErrorMessage = tr( "Redirect loop detected: %1" ).arg( toUrl.toString() ); QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) ); mResponse.clear(); } else { QNetworkRequest request( toUrl ); QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsWfsRequest" ) ); if ( !mUri.auth().setAuthorization( request ) ) { mResponse.clear(); mErrorMessage = errorMessageFailedAuth(); mErrorCode = QgsWfsRequest::NetworkError; QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) ); emit downloadFinished(); return; } request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, mForceRefresh ? QNetworkRequest::AlwaysNetwork : QNetworkRequest::PreferCache ); request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true ); mReply->deleteLater(); mReply = nullptr; QgsDebugMsgLevel( QStringLiteral( "redirected: %1 forceRefresh=%2" ).arg( redirect.toString() ).arg( mForceRefresh ), 4 ); mReply = QgsNetworkAccessManager::instance()->get( request ); mReply->setReadBufferSize( READ_BUFFER_SIZE_HINT ); if ( !mUri.auth().setAuthorizationReply( mReply ) ) { mResponse.clear(); mErrorMessage = errorMessageFailedAuth(); mErrorCode = QgsWfsRequest::NetworkError; QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) ); emit downloadFinished(); return; } connect( mReply, &QNetworkReply::finished, this, &QgsWfsRequest::replyFinished, Qt::DirectConnection ); connect( mReply, &QNetworkReply::downloadProgress, this, &QgsWfsRequest::replyProgress, Qt::DirectConnection ); return; } } else { const QgsNetworkAccessManager *nam = QgsNetworkAccessManager::instance(); if ( nam->cache() ) { QNetworkCacheMetaData cmd = nam->cache()->metaData( mReply->request().url() ); QNetworkCacheMetaData::RawHeaderList hl; const auto constRawHeaders = cmd.rawHeaders(); for ( const QNetworkCacheMetaData::RawHeader &h : constRawHeaders ) { if ( h.first != "Cache-Control" ) hl.append( h ); } cmd.setRawHeaders( hl ); QgsDebugMsgLevel( QStringLiteral( "expirationDate:%1" ).arg( cmd.expirationDate().toString() ), 4 ); if ( cmd.expirationDate().isNull() ) { cmd.setExpirationDate( QDateTime::currentDateTime().addSecs( defaultExpirationInSec() ) ); } nam->cache()->updateMetaData( cmd ); } else { QgsDebugMsgLevel( QStringLiteral( "No cache!" ), 4 ); } #ifdef QGISDEBUG bool fromCache = mReply->attribute( QNetworkRequest::SourceIsFromCacheAttribute ).toBool(); QgsDebugMsgLevel( QStringLiteral( "Reply was cached: %1" ).arg( fromCache ), 4 ); #endif mResponse = mReply->readAll(); if ( mResponse.isEmpty() && !mGotNonEmptyResponse ) { mErrorMessage = tr( "empty response: %1" ).arg( mReply->errorString() ); mErrorCode = QgsWfsRequest::ServerExceptionError; QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) ); } } } else { mErrorMessage = errorMessageWithReason( mReply->errorString() ); mErrorCode = QgsWfsRequest::ServerExceptionError; QgsMessageLog::logMessage( mErrorMessage, tr( "WFS" ) ); mResponse.clear(); } } if ( mTimedout ) mErrorCode = QgsWfsRequest::TimeoutError; if ( mReply ) { mReply->deleteLater(); mReply = nullptr; } emit downloadFinished(); }