Skip to content

Commit

Permalink
wms: fix redirected requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jun 22, 2014
1 parent e8d80a3 commit ec1bfad
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 55 deletions.
100 changes: 52 additions & 48 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9775,41 +9775,43 @@ void QgisApp::namAuthenticationRequired( QNetworkReply *reply, QAuthenticator *a
QString username = auth->user();
QString password = auth->password();

QMutexLocker lock( QgsCredentials::instance()->mutex() );

do
{
bool ok = QgsCredentials::instance()->get(
QString( "%1 at %2" ).arg( auth->realm() ).arg( reply->url().host() ),
username, password,
tr( "Authentication required" ) );
if ( !ok )
return;

if ( reply->isFinished() )
return;
QMutexLocker lock( QgsCredentials::instance()->mutex() );

if ( auth->user() == username && password == auth->password() )
do
{
if ( !password.isNull() )
bool ok = QgsCredentials::instance()->get(
QString( "%1 at %2" ).arg( auth->realm() ).arg( reply->url().host() ),
username, password,
tr( "Authentication required" ) );
if ( !ok )
return;

if ( reply->isFinished() )
return;

if ( auth->user() == username && password == auth->password() )
{
// credentials didn't change - stored ones probably wrong? clear password and retry
if ( !password.isNull() )
{
// credentials didn't change - stored ones probably wrong? clear password and retry
QgsCredentials::instance()->put(
QString( "%1 at %2" ).arg( auth->realm() ).arg( reply->url().host() ),
username, QString::null );
continue;
}
}
else
{
// save credentials
QgsCredentials::instance()->put(
QString( "%1 at %2" ).arg( auth->realm() ).arg( reply->url().host() ),
username, QString::null );
continue;
username, password
);
}
}
else
{
// save credentials
QgsCredentials::instance()->put(
QString( "%1 at %2" ).arg( auth->realm() ).arg( reply->url().host() ),
username, password
);
}
while ( 0 );
}
while ( 0 );

auth->setUser( username );
auth->setPassword( password );
Expand All @@ -9828,37 +9830,39 @@ void QgisApp::namProxyAuthenticationRequired( const QNetworkProxy &proxy, QAuthe
QString username = auth->user();
QString password = auth->password();

QMutexLocker lock( QgsCredentials::instance()->mutex() );

do
{
bool ok = QgsCredentials::instance()->get(
QString( "proxy %1:%2 [%3]" ).arg( proxy.hostName() ).arg( proxy.port() ).arg( auth->realm() ),
username, password,
tr( "Proxy authentication required" ) );
if ( !ok )
return;
QMutexLocker lock( QgsCredentials::instance()->mutex() );

if ( auth->user() == username && password == auth->password() )
do
{
if ( !password.isNull() )
bool ok = QgsCredentials::instance()->get(
QString( "proxy %1:%2 [%3]" ).arg( proxy.hostName() ).arg( proxy.port() ).arg( auth->realm() ),
username, password,
tr( "Proxy authentication required" ) );
if ( !ok )
return;

if ( auth->user() == username && password == auth->password() )
{
if ( !password.isNull() )
{
// credentials didn't change - stored ones probably wrong? clear password and retry
QgsCredentials::instance()->put(
QString( "proxy %1:%2 [%3]" ).arg( proxy.hostName() ).arg( proxy.port() ).arg( auth->realm() ),
username, QString::null );
continue;
}
}
else
{
// credentials didn't change - stored ones probably wrong? clear password and retry
QgsCredentials::instance()->put(
QString( "proxy %1:%2 [%3]" ).arg( proxy.hostName() ).arg( proxy.port() ).arg( auth->realm() ),
username, QString::null );
continue;
username, password
);
}
}
else
{
QgsCredentials::instance()->put(
QString( "proxy %1:%2 [%3]" ).arg( proxy.hostName() ).arg( proxy.port() ).arg( auth->realm() ),
username, password
);
}
while ( 0 );
}
while ( 0 );

auth->setUser( username );
auth->setPassword( password );
Expand Down
22 changes: 15 additions & 7 deletions src/providers/wms/qgswmsprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2367,7 +2367,7 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPoint & thePoint, Qgs
connect( mIdentifyReply, SIGNAL( finished() ), this, SLOT( identifyReplyFinished() ) );

QEventLoop loop;
connect( mIdentifyReply, SIGNAL( finished() ), &loop, SLOT( quit() ) );
mIdentifyReply->setProperty( "eventLoop", QVariant::fromValue( qobject_cast<QObject *>( &loop ) ) );
loop.exec( QEventLoop::ExcludeUserInputEvents );

if ( mIdentifyResultBodies.size() == 0 ) // no result
Expand Down Expand Up @@ -2767,9 +2767,12 @@ QgsRasterIdentifyResult QgsWmsProvider::identify( const QgsPoint & thePoint, Qgs

void QgsWmsProvider::identifyReplyFinished()
{
QgsDebugMsg( "Entered." );
mIdentifyResultHeaders.clear();
mIdentifyResultBodies.clear();

QEventLoop *loop = qobject_cast< QEventLoop *>( sender()->property( "eventLoop" ).value< QObject *>() );

if ( mIdentifyReply->error() == QNetworkReply::NoError )
{
QVariant redirect = mIdentifyReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
Expand All @@ -2782,8 +2785,8 @@ void QgsWmsProvider::identifyReplyFinished()

QgsDebugMsg( QString( "redirected getfeatureinfo: %1" ).arg( redirect.toString() ) );
mIdentifyReply = QgsNetworkAccessManager::instance()->get( QNetworkRequest( redirect.toUrl() ) );
mIdentifyReply->setProperty( "eventLoop", QVariant::fromValue( qobject_cast<QObject *>( loop ) ) );
connect( mIdentifyReply, SIGNAL( finished() ), this, SLOT( identifyReplyFinished() ) );

return;
}

Expand All @@ -2794,8 +2797,6 @@ void QgsWmsProvider::identifyReplyFinished()
mErrorFormat = "text/plain";
mError = tr( "Map getfeatureinfo error %1: %2" ).arg( status.toInt() ).arg( phrase.toString() );
emit statusChanged( mError );

//mIdentifyResult = "";
}

QgsNetworkReplyParser parser( mIdentifyReply );
Expand All @@ -2805,7 +2806,6 @@ void QgsWmsProvider::identifyReplyFinished()
mErrorFormat = "text/plain";
mError = tr( "Cannot parse getfeatureinfo: %1" ).arg( parser.error() );
emit statusChanged( mError );
//mIdentifyResult = "";
}
else
{
Expand All @@ -2824,6 +2824,9 @@ void QgsWmsProvider::identifyReplyFinished()
QgsMessageLog::logMessage( mError, tr( "WMS" ) );
}

if ( loop )
QMetaObject::invokeMethod( loop, "quit", Qt::QueuedConnection );

mIdentifyReply->deleteLater();
mIdentifyReply = 0;
}
Expand Down Expand Up @@ -3016,12 +3019,11 @@ QImage QgsWmsProvider::getLegendGraphic( double scale, bool forceRefresh )

QgsDebugMsg( QString( "getlegendgraphics: %1" ).arg( url.toString() ) );
mGetLegendGraphicReply = QgsNetworkAccessManager::instance()->get( request );

connect( mGetLegendGraphicReply, SIGNAL( finished() ), this, SLOT( getLegendGraphicReplyFinished() ) );
connect( mGetLegendGraphicReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( getLegendGraphicReplyProgress( qint64, qint64 ) ) );

QEventLoop loop;
connect( mGetLegendGraphicReply, SIGNAL( finished() ), &loop, SLOT( quit() ) );
mGetLegendGraphicReply->setProperty( "eventLoop", QVariant::fromValue( qobject_cast<QObject *>( &loop ) ) );
loop.exec( QEventLoop::ExcludeUserInputEvents );

QgsDebugMsg( "exiting." );
Expand All @@ -3033,6 +3035,8 @@ void QgsWmsProvider::getLegendGraphicReplyFinished()
{
QgsDebugMsg( "entering." );

QEventLoop *loop = qobject_cast< QEventLoop *>( sender()->property( "eventLoop" ).value< QObject *>() );

if ( mGetLegendGraphicReply->error() == QNetworkReply::NoError )
{
QgsDebugMsg( "reply ok" );
Expand Down Expand Up @@ -3060,6 +3064,7 @@ void QgsWmsProvider::getLegendGraphicReplyFinished()
mGetLegendGraphicReply->deleteLater();
QgsDebugMsg( QString( "redirected GetLegendGraphic: %1" ).arg( redirect.toString() ) );
mGetLegendGraphicReply = QgsNetworkAccessManager::instance()->get( request );
mIdentifyReply->setProperty( "eventLoop", QVariant::fromValue( qobject_cast<QObject *>( loop ) ) );

connect( mGetLegendGraphicReply, SIGNAL( finished() ), this, SLOT( getLegendGraphicReplyFinished() ) );
connect( mGetLegendGraphicReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( getLegendGraphicReplyProgress( qint64, qint64 ) ) );
Expand Down Expand Up @@ -3099,6 +3104,9 @@ void QgsWmsProvider::getLegendGraphicReplyFinished()
mHttpGetLegendGraphicResponse.clear();
}

if ( loop )
QMetaObject::invokeMethod( loop, "quit", Qt::QueuedConnection );

mGetLegendGraphicReply->deleteLater();
mGetLegendGraphicReply = 0;
}
Expand Down

0 comments on commit ec1bfad

Please sign in to comment.