Skip to content

Commit afc2e05

Browse files
committed
network manager: fix authentication of private network manager instances and support system proxies with exclusions
1 parent b1570fb commit afc2e05

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

src/app/qgsoptions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,14 @@ void QgsOptions::setCurrentPage( QString pageWidgetName )
860860
}
861861
}
862862

863+
void QgsOptions::on_mProxyTypeComboBox_currentIndexChanged( int idx )
864+
{
865+
leProxyHost->setEnabled( idx != 0 );
866+
leProxyPort->setEnabled( idx != 0 );
867+
leProxyUser->setEnabled( idx != 0 );
868+
leProxyPassword->setEnabled( idx != 0 );
869+
}
870+
863871
void QgsOptions::on_cbxProjectDefaultNew_toggled( bool checked )
864872
{
865873
if ( checked )

src/app/qgsoptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
135135
*/
136136
void on_mBoldGroupBoxTitleChkBx_clicked( bool chkd );
137137

138+
void on_mProxyTypeComboBox_currentIndexChanged( int idx );
139+
138140
/**Add a new URL to exclude from Proxy*/
139141
void on_mAddUrlPushButton_clicked();
140142

src/core/qgsnetworkaccessmanager.cpp

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,32 @@ class QgsNetworkProxyFactory : public QNetworkProxyFactory
6969
}
7070
}
7171

72-
QgsDebugMsg( QString( "using user proxy for %1" ).arg( url ) );
72+
if ( nam->useSystemProxy() )
73+
{
74+
QgsDebugMsg( QString( "requesting system proxy for query %1" ).arg( url ) );
75+
QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery( query );
76+
if ( !proxies.isEmpty() )
77+
{
78+
QgsDebugMsg( QString( "using system proxy %1:%2 for query" )
79+
.arg( proxies.first().hostName() ).arg( proxies.first().port() ) );
80+
return proxies;
81+
}
82+
}
83+
84+
QgsDebugMsg( QString( "using fallback proxy for %1" ).arg( url ) );
7385
return QList<QNetworkProxy>() << nam->fallbackProxy();
7486
}
7587
};
7688

7789
QgsNetworkAccessManager *QgsNetworkAccessManager::instance()
7890
{
7991
static QgsNetworkAccessManager sInstance;
80-
8192
return &sInstance;
8293
}
8394

8495
QgsNetworkAccessManager::QgsNetworkAccessManager( QObject *parent )
8596
: QNetworkAccessManager( parent )
97+
, mUseSystemProxy( false )
8698
{
8799
setProxyFactory( new QgsNetworkProxyFactory() );
88100
}
@@ -230,7 +242,30 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
230242

231243
QSettings settings;
232244

233-
//check if proxy is enabled
245+
mUseSystemProxy = false;
246+
247+
if ( this != instance() )
248+
{
249+
connect( this, SIGNAL( authenticationRequired( QNetworkReply *, QAuthenticator * ) ),
250+
instance(), SIGNAL( authenticationRequired( QNetworkReply *, QAuthenticator * ) ),
251+
Qt::BlockingQueuedConnection );
252+
253+
connect( this, SIGNAL( proxyAuthenticationRequired( const QNetworkProxy &, QAuthenticator * ) ),
254+
instance(), SIGNAL( proxyAuthenticationRequired( const QNetworkProxy &, QAuthenticator * ) ),
255+
Qt::BlockingQueuedConnection );
256+
257+
connect( this, SIGNAL( requestTimedOut( QNetworkReply* ) ),
258+
instance(), SIGNAL( requestTimedOut( QNetworkReply* ) ),
259+
Qt::BlockingQueuedConnection );
260+
261+
#ifndef QT_NO_OPENSSL
262+
connect( this, SIGNAL( sslErrors( QNetworkReply *, const QList<QSslError> & ) ),
263+
instance(), SIGNAL( sslErrors( QNetworkReply *, const QList<QSslError> & ) ),
264+
Qt::BlockingQueuedConnection );
265+
#endif
266+
}
267+
268+
// check if proxy is enabled
234269
bool proxyEnabled = settings.value( "proxy/proxyEnabled", false ).toBool();
235270
if ( proxyEnabled )
236271
{
@@ -243,24 +278,21 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
243278
QString proxyPassword = settings.value( "proxy/proxyPassword", "" ).toString();
244279

245280
QString proxyTypeString = settings.value( "proxy/proxyType", "" ).toString();
246-
QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;
281+
247282
if ( proxyTypeString == "DefaultProxy" )
248283
{
249-
proxyType = QNetworkProxy::DefaultProxy;
250-
251-
#if defined(Q_OS_WIN)
284+
mUseSystemProxy = true;
252285
QNetworkProxyFactory::setUseSystemConfiguration( true );
253286
QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
254287
if ( !proxies.isEmpty() )
255288
{
256289
proxy = proxies.first();
257290
}
258-
#endif
259-
260291
QgsDebugMsg( "setting default proxy" );
261292
}
262293
else
263294
{
295+
QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
264296
if ( proxyTypeString == "Socks5Proxy" )
265297
{
266298
proxyType = QNetworkProxy::Socks5Proxy;

src/core/qgsnetworkaccessmanager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
8282
//! Setup the NAM according to the user's settings
8383
void setupDefaultProxyAndCache();
8484

85+
bool useSystemProxy() { return mUseSystemProxy; }
86+
8587
signals:
8688
void requestAboutToBeCreated( QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice * );
8789
void requestCreated( QNetworkReply * );
@@ -97,6 +99,7 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
9799
QList<QNetworkProxyFactory*> mProxyFactories;
98100
QNetworkProxy mFallbackProxy;
99101
QStringList mExcludedURLs;
102+
bool mUseSystemProxy;
100103
};
101104

102105
#endif // QGSNETWORKACCESSMANAGER_H

0 commit comments

Comments
 (0)