Skip to content

Commit

Permalink
network manager: fix authentication of private network manager instan…
Browse files Browse the repository at this point in the history
…ces and support system proxies with exclusions
  • Loading branch information
jef-n committed May 10, 2014
1 parent b1570fb commit afc2e05
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -860,6 +860,14 @@ void QgsOptions::setCurrentPage( QString pageWidgetName )
}
}

void QgsOptions::on_mProxyTypeComboBox_currentIndexChanged( int idx )
{
leProxyHost->setEnabled( idx != 0 );
leProxyPort->setEnabled( idx != 0 );
leProxyUser->setEnabled( idx != 0 );
leProxyPassword->setEnabled( idx != 0 );
}

void QgsOptions::on_cbxProjectDefaultNew_toggled( bool checked )
{
if ( checked )
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsoptions.h
Expand Up @@ -135,6 +135,8 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
*/
void on_mBoldGroupBoxTitleChkBx_clicked( bool chkd );

void on_mProxyTypeComboBox_currentIndexChanged( int idx );

/**Add a new URL to exclude from Proxy*/
void on_mAddUrlPushButton_clicked();

Expand Down
50 changes: 41 additions & 9 deletions src/core/qgsnetworkaccessmanager.cpp
Expand Up @@ -69,20 +69,32 @@ class QgsNetworkProxyFactory : public QNetworkProxyFactory
}
}

QgsDebugMsg( QString( "using user proxy for %1" ).arg( url ) );
if ( nam->useSystemProxy() )
{
QgsDebugMsg( QString( "requesting system proxy for query %1" ).arg( url ) );
QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery( query );
if ( !proxies.isEmpty() )
{
QgsDebugMsg( QString( "using system proxy %1:%2 for query" )
.arg( proxies.first().hostName() ).arg( proxies.first().port() ) );
return proxies;
}
}

QgsDebugMsg( QString( "using fallback proxy for %1" ).arg( url ) );
return QList<QNetworkProxy>() << nam->fallbackProxy();
}
};

QgsNetworkAccessManager *QgsNetworkAccessManager::instance()
{
static QgsNetworkAccessManager sInstance;

return &sInstance;
}

QgsNetworkAccessManager::QgsNetworkAccessManager( QObject *parent )
: QNetworkAccessManager( parent )
, mUseSystemProxy( false )
{
setProxyFactory( new QgsNetworkProxyFactory() );
}
Expand Down Expand Up @@ -230,7 +242,30 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()

QSettings settings;

//check if proxy is enabled
mUseSystemProxy = false;

if ( this != instance() )
{
connect( this, SIGNAL( authenticationRequired( QNetworkReply *, QAuthenticator * ) ),
instance(), SIGNAL( authenticationRequired( QNetworkReply *, QAuthenticator * ) ),
Qt::BlockingQueuedConnection );

connect( this, SIGNAL( proxyAuthenticationRequired( const QNetworkProxy &, QAuthenticator * ) ),
instance(), SIGNAL( proxyAuthenticationRequired( const QNetworkProxy &, QAuthenticator * ) ),
Qt::BlockingQueuedConnection );

connect( this, SIGNAL( requestTimedOut( QNetworkReply* ) ),
instance(), SIGNAL( requestTimedOut( QNetworkReply* ) ),
Qt::BlockingQueuedConnection );

#ifndef QT_NO_OPENSSL
connect( this, SIGNAL( sslErrors( QNetworkReply *, const QList<QSslError> & ) ),
instance(), SIGNAL( sslErrors( QNetworkReply *, const QList<QSslError> & ) ),
Qt::BlockingQueuedConnection );
#endif
}

// check if proxy is enabled
bool proxyEnabled = settings.value( "proxy/proxyEnabled", false ).toBool();
if ( proxyEnabled )
{
Expand All @@ -243,24 +278,21 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
QString proxyPassword = settings.value( "proxy/proxyPassword", "" ).toString();

QString proxyTypeString = settings.value( "proxy/proxyType", "" ).toString();
QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;

if ( proxyTypeString == "DefaultProxy" )
{
proxyType = QNetworkProxy::DefaultProxy;

#if defined(Q_OS_WIN)
mUseSystemProxy = true;
QNetworkProxyFactory::setUseSystemConfiguration( true );
QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
if ( !proxies.isEmpty() )
{
proxy = proxies.first();
}
#endif

QgsDebugMsg( "setting default proxy" );
}
else
{
QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
if ( proxyTypeString == "Socks5Proxy" )
{
proxyType = QNetworkProxy::Socks5Proxy;
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsnetworkaccessmanager.h
Expand Up @@ -82,6 +82,8 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
//! Setup the NAM according to the user's settings
void setupDefaultProxyAndCache();

bool useSystemProxy() { return mUseSystemProxy; }

signals:
void requestAboutToBeCreated( QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice * );
void requestCreated( QNetworkReply * );
Expand All @@ -97,6 +99,7 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
QList<QNetworkProxyFactory*> mProxyFactories;
QNetworkProxy mFallbackProxy;
QStringList mExcludedURLs;
bool mUseSystemProxy;
};

#endif // QGSNETWORKACCESSMANAGER_H
Expand Down

0 comments on commit afc2e05

Please sign in to comment.