QGIS API Documentation  3.0.2-Girona (307d082)
qgsnetworkaccessmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnetworkaccessmanager.cpp
3  This class implements a QNetworkManager with the ability to chain in
4  own proxy factories.
5 
6  -------------------
7  begin : 2010-05-08
8  copyright : (C) 2010 by Juergen E. Fischer
9  email : jef at norbit dot de
10 
11 ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 
23 
24 #include "qgsapplication.h"
25 #include "qgsmessagelog.h"
26 #include "qgslogger.h"
27 #include "qgis.h"
28 #include "qgssettings.h"
29 #include "qgsnetworkdiskcache.h"
30 #include "qgsauthmanager.h"
31 
32 #include <QUrl>
33 #include <QTimer>
34 #include <QNetworkReply>
35 #include <QThreadStorage>
36 #include <QAuthenticator>
37 
38 #ifndef QT_NO_SSL
39 #include <QSslConfiguration>
40 #endif
41 
42 #include "qgsnetworkdiskcache.h"
43 #include "qgsauthmanager.h"
44 
45 QgsNetworkAccessManager *QgsNetworkAccessManager::sMainNAM = nullptr;
46 
48 class QgsNetworkProxyFactory : public QNetworkProxyFactory
49 {
50  public:
51  QgsNetworkProxyFactory() = default;
52 
53  QList<QNetworkProxy> queryProxy( const QNetworkProxyQuery &query = QNetworkProxyQuery() ) override
54  {
56 
57  // iterate proxies factories and take first non empty list
58  Q_FOREACH ( QNetworkProxyFactory *f, nam->proxyFactories() )
59  {
60  QList<QNetworkProxy> systemproxies = f->systemProxyForQuery( query );
61  if ( !systemproxies.isEmpty() )
62  return systemproxies;
63 
64  QList<QNetworkProxy> proxies = f->queryProxy( query );
65  if ( !proxies.isEmpty() )
66  return proxies;
67  }
68 
69  // no proxies from the proxy factory list check for excludes
70  if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
71  return QList<QNetworkProxy>() << nam->fallbackProxy();
72 
73  QString url = query.url().toString();
74 
75  Q_FOREACH ( const QString &exclude, nam->excludeList() )
76  {
77  if ( url.startsWith( exclude ) )
78  {
79  QgsDebugMsgLevel( QString( "using default proxy for %1 [exclude %2]" ).arg( url, exclude ), 4 );
80  return QList<QNetworkProxy>() << QNetworkProxy();
81  }
82  }
83 
84  if ( nam->useSystemProxy() )
85  {
86  QgsDebugMsgLevel( QString( "requesting system proxy for query %1" ).arg( url ), 4 );
87  QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery( query );
88  if ( !proxies.isEmpty() )
89  {
90  QgsDebugMsgLevel( QString( "using system proxy %1:%2 for query" )
91  .arg( proxies.first().hostName() ).arg( proxies.first().port() ), 4 );
92  return proxies;
93  }
94  }
95 
96  QgsDebugMsgLevel( QString( "using fallback proxy for %1" ).arg( url ), 4 );
97  return QList<QNetworkProxy>() << nam->fallbackProxy();
98  }
99 };
101 
102 //
103 // Static calls to enforce singleton behavior
104 //
106 {
107  static QThreadStorage<QgsNetworkAccessManager> sInstances;
108  QgsNetworkAccessManager *nam = &sInstances.localData();
109 
110  if ( nam->thread() == qApp->thread() )
111  sMainNAM = nam;
112 
113  if ( !nam->mInitialized )
115 
116  return nam;
117 }
118 
120  : QNetworkAccessManager( parent )
121 {
122  setProxyFactory( new QgsNetworkProxyFactory() );
123 }
124 
125 void QgsNetworkAccessManager::insertProxyFactory( QNetworkProxyFactory *factory )
126 {
127  mProxyFactories.insert( 0, factory );
128 }
129 
130 void QgsNetworkAccessManager::removeProxyFactory( QNetworkProxyFactory *factory )
131 {
132  mProxyFactories.removeAll( factory );
133 }
134 
135 const QList<QNetworkProxyFactory *> QgsNetworkAccessManager::proxyFactories() const
136 {
137  return mProxyFactories;
138 }
139 
141 {
142  return mExcludedURLs;
143 }
144 
145 const QNetworkProxy &QgsNetworkAccessManager::fallbackProxy() const
146 {
147  return mFallbackProxy;
148 }
149 
150 void QgsNetworkAccessManager::setFallbackProxyAndExcludes( const QNetworkProxy &proxy, const QStringList &excludes )
151 {
152  QgsDebugMsg( QString( "proxy settings: (type:%1 host: %2:%3, user:%4, password:%5" )
153  .arg( proxy.type() == QNetworkProxy::DefaultProxy ? "DefaultProxy" :
154  proxy.type() == QNetworkProxy::Socks5Proxy ? "Socks5Proxy" :
155  proxy.type() == QNetworkProxy::NoProxy ? "NoProxy" :
156  proxy.type() == QNetworkProxy::HttpProxy ? "HttpProxy" :
157  proxy.type() == QNetworkProxy::HttpCachingProxy ? "HttpCachingProxy" :
158  proxy.type() == QNetworkProxy::FtpCachingProxy ? "FtpCachingProxy" :
159  "Undefined",
160  proxy.hostName() )
161  .arg( proxy.port() )
162  .arg( proxy.user(),
163  proxy.password().isEmpty() ? "not set" : "set" ) );
164 
165  mFallbackProxy = proxy;
166  mExcludedURLs = excludes;
167 }
168 
169 QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData )
170 {
171  QgsSettings s;
172 
173  QNetworkRequest *pReq( const_cast< QNetworkRequest * >( &req ) ); // hack user agent
174 
175  QString userAgent = s.value( QStringLiteral( "/qgis/networkAndProxy/userAgent" ), "Mozilla/5.0" ).toString();
176  if ( !userAgent.isEmpty() )
177  userAgent += ' ';
178  userAgent += QStringLiteral( "QGIS/%1" ).arg( Qgis::QGIS_VERSION );
179  pReq->setRawHeader( "User-Agent", userAgent.toUtf8() );
180 
181 #ifndef QT_NO_SSL
182  bool ishttps = pReq->url().scheme().toLower() == QLatin1String( "https" );
183  if ( ishttps && !QgsApplication::authManager()->isDisabled() )
184  {
185  QgsDebugMsgLevel( "Adding trusted CA certs to request", 3 );
186  QSslConfiguration sslconfig( pReq->sslConfiguration() );
187  // Merge trusted CAs with any additional CAs added by the authentication methods
188  sslconfig.setCaCertificates( QgsAuthCertUtils::casMerge( QgsApplication::authManager()->trustedCaCertsCache(), sslconfig.caCertificates( ) ) );
189  // check for SSL cert custom config
190  QString hostport( QStringLiteral( "%1:%2" )
191  .arg( pReq->url().host().trimmed() )
192  .arg( pReq->url().port() != -1 ? pReq->url().port() : 443 ) );
194  if ( !servconfig.isNull() )
195  {
196  QgsDebugMsg( QString( "Adding SSL custom config to request for %1" ).arg( hostport ) );
197  sslconfig.setProtocol( servconfig.sslProtocol() );
198  sslconfig.setPeerVerifyMode( servconfig.sslPeerVerifyMode() );
199  sslconfig.setPeerVerifyDepth( servconfig.sslPeerVerifyDepth() );
200  }
201 
202  pReq->setSslConfiguration( sslconfig );
203  }
204 #endif
205 
206  emit requestAboutToBeCreated( op, req, outgoingData );
207  QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
208 
209  emit requestCreated( reply );
210 
211  // The timer will call abortRequest slot to abort the connection if needed.
212  // The timer is stopped by the finished signal and is restarted on downloadProgress and
213  // uploadProgress.
214  QTimer *timer = new QTimer( reply );
215  timer->setObjectName( QStringLiteral( "timeoutTimer" ) );
216  connect( timer, &QTimer::timeout, this, &QgsNetworkAccessManager::abortRequest );
217  timer->setSingleShot( true );
218  timer->start( s.value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), "60000" ).toInt() );
219 
220  connect( reply, &QNetworkReply::downloadProgress, timer, [timer] { timer->start(); } );
221  connect( reply, &QNetworkReply::uploadProgress, timer, [timer] { timer->start(); } );
222  connect( reply, &QNetworkReply::finished, timer, &QTimer::stop );
223  QgsDebugMsgLevel( QString( "Created [reply:%1]" ).arg( ( qint64 ) reply, 0, 16 ), 3 );
224 
225  return reply;
226 }
227 
228 void QgsNetworkAccessManager::abortRequest()
229 {
230  QTimer *timer = qobject_cast<QTimer *>( sender() );
231  Q_ASSERT( timer );
232 
233  QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent() );
234  Q_ASSERT( reply );
235 
236  reply->abort();
237  QgsDebugMsgLevel( QString( "Abort [reply:%1] %2" ).arg( ( qint64 ) reply, 0, 16 ).arg( reply->url().toString() ), 3 );
238  QgsMessageLog::logMessage( tr( "Network request %1 timed out" ).arg( reply->url().toString() ), tr( "Network" ) );
239  // Notify the application
240  emit requestTimedOut( reply );
241 
242 }
243 
244 
245 QString QgsNetworkAccessManager::cacheLoadControlName( QNetworkRequest::CacheLoadControl control )
246 {
247  switch ( control )
248  {
249  case QNetworkRequest::AlwaysNetwork:
250  return QStringLiteral( "AlwaysNetwork" );
251  case QNetworkRequest::PreferNetwork:
252  return QStringLiteral( "PreferNetwork" );
253  case QNetworkRequest::PreferCache:
254  return QStringLiteral( "PreferCache" );
255  case QNetworkRequest::AlwaysCache:
256  return QStringLiteral( "AlwaysCache" );
257  default:
258  break;
259  }
260  return QStringLiteral( "PreferNetwork" );
261 }
262 
263 QNetworkRequest::CacheLoadControl QgsNetworkAccessManager::cacheLoadControlFromName( const QString &name )
264 {
265  if ( name == QLatin1String( "AlwaysNetwork" ) )
266  {
267  return QNetworkRequest::AlwaysNetwork;
268  }
269  else if ( name == QLatin1String( "PreferNetwork" ) )
270  {
271  return QNetworkRequest::PreferNetwork;
272  }
273  else if ( name == QLatin1String( "PreferCache" ) )
274  {
275  return QNetworkRequest::PreferCache;
276  }
277  else if ( name == QLatin1String( "AlwaysCache" ) )
278  {
279  return QNetworkRequest::AlwaysCache;
280  }
281  return QNetworkRequest::PreferNetwork;
282 }
283 
285 {
286  mInitialized = true;
287  mUseSystemProxy = false;
288 
289  Q_ASSERT( sMainNAM );
290 
291  if ( sMainNAM != this )
292  {
293  connect( this, &QNetworkAccessManager::authenticationRequired,
294  sMainNAM, &QNetworkAccessManager::authenticationRequired,
295  Qt::BlockingQueuedConnection );
296 
297  connect( this, &QNetworkAccessManager::proxyAuthenticationRequired,
298  sMainNAM, &QNetworkAccessManager::proxyAuthenticationRequired,
299  Qt::BlockingQueuedConnection );
300 
303 
304 #ifndef QT_NO_SSL
305  connect( this, &QNetworkAccessManager::sslErrors,
306  sMainNAM, &QNetworkAccessManager::sslErrors,
307  Qt::BlockingQueuedConnection );
308 #endif
309  }
310 
311  // check if proxy is enabled
312  QgsSettings settings;
313  QNetworkProxy proxy;
314  QStringList excludes;
315 
316  bool proxyEnabled = settings.value( QStringLiteral( "proxy/proxyEnabled" ), false ).toBool();
317  if ( proxyEnabled )
318  {
319  excludes = settings.value( QStringLiteral( "proxy/proxyExcludedUrls" ), "" ).toString().split( '|', QString::SkipEmptyParts );
320 
321  //read type, host, port, user, passw from settings
322  QString proxyHost = settings.value( QStringLiteral( "proxy/proxyHost" ), "" ).toString();
323  int proxyPort = settings.value( QStringLiteral( "proxy/proxyPort" ), "" ).toString().toInt();
324 
325  QString proxyUser = settings.value( QStringLiteral( "proxy/proxyUser" ), "" ).toString();
326  QString proxyPassword = settings.value( QStringLiteral( "proxy/proxyPassword" ), "" ).toString();
327 
328  QString proxyTypeString = settings.value( QStringLiteral( "proxy/proxyType" ), "" ).toString();
329 
330  if ( proxyTypeString == QLatin1String( "DefaultProxy" ) )
331  {
332  mUseSystemProxy = true;
333  QNetworkProxyFactory::setUseSystemConfiguration( true );
334  QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
335  if ( !proxies.isEmpty() )
336  {
337  proxy = proxies.first();
338  }
339  QgsDebugMsg( "setting default proxy" );
340  }
341  else
342  {
343  QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
344  if ( proxyTypeString == QLatin1String( "Socks5Proxy" ) )
345  {
346  proxyType = QNetworkProxy::Socks5Proxy;
347  }
348  else if ( proxyTypeString == QLatin1String( "HttpProxy" ) )
349  {
350  proxyType = QNetworkProxy::HttpProxy;
351  }
352  else if ( proxyTypeString == QLatin1String( "HttpCachingProxy" ) )
353  {
354  proxyType = QNetworkProxy::HttpCachingProxy;
355  }
356  else if ( proxyTypeString == QLatin1String( "FtpCachingProxy" ) )
357  {
358  proxyType = QNetworkProxy::FtpCachingProxy;
359  }
360  QgsDebugMsg( QStringLiteral( "setting proxy %1 %2:%3 %4/%5" )
361  .arg( proxyType )
362  .arg( proxyHost ).arg( proxyPort )
363  .arg( proxyUser, proxyPassword )
364  );
365  proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
366  }
367  // Setup network proxy authentication configuration
368  QString authcfg = settings.value( QStringLiteral( "proxy/authcfg" ), "" ).toString();
369  if ( !authcfg.isEmpty( ) )
370  {
371  QgsDebugMsg( QStringLiteral( "setting proxy from stored authentication configuration %1" ).arg( authcfg ) );
372  // Never crash! Never.
374  QgsApplication::authManager()->updateNetworkProxy( proxy, authcfg );
375  }
376  }
377 
378  setFallbackProxyAndExcludes( proxy, excludes );
379 
380  QgsNetworkDiskCache *newcache = qobject_cast<QgsNetworkDiskCache *>( cache() );
381  if ( !newcache )
382  newcache = new QgsNetworkDiskCache( this );
383 
384  QString cacheDirectory = settings.value( QStringLiteral( "cache/directory" ) ).toString();
385  if ( cacheDirectory.isEmpty() )
386  cacheDirectory = QgsApplication::qgisSettingsDirPath() + "cache";
387  qint64 cacheSize = settings.value( QStringLiteral( "cache/size" ), 50 * 1024 * 1024 ).toULongLong();
388  newcache->setCacheDirectory( cacheDirectory );
389  newcache->setMaximumCacheSize( cacheSize );
390  QgsDebugMsg( QString( "cacheDirectory: %1" ).arg( newcache->cacheDirectory() ) );
391  QgsDebugMsg( QString( "maximumCacheSize: %1" ).arg( newcache->maximumCacheSize() ) );
392 
393  if ( cache() != newcache )
394  setCache( newcache );
395 }
396 
bool isNull() const
Whether configuration is null (missing components)
void requestCreated(QNetworkReply *)
static QList< QSslCertificate > casMerge(const QList< QSslCertificate > &bundle1, const QList< QSslCertificate > &bundle2)
casMerge merges two certificate bundles in a single one removing duplicates, the certificates from th...
static const QString QGIS_VERSION
Version string.
Definition: qgis.h:63
static QString cacheLoadControlName(QNetworkRequest::CacheLoadControl control)
Get name for QNetworkRequest::CacheLoadControl.
int sslPeerVerifyDepth() const
Number or SSL client&#39;s peer to verify in connections.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user&#39;s home dir.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
bool updateNetworkProxy(QNetworkProxy &proxy, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QNetworkProxy with an authentication config.
void setCacheDirectory(const QString &cacheDir)
Configuration container for SSL server connection exceptions or overrides.
void setupDefaultProxyAndCache()
Setup the NAM according to the user&#39;s settings.
QStringList excludeList() const
retrieve exclude list (urls shouldn&#39;t use the fallback proxy)
QSsl::SslProtocol sslProtocol() const
SSL server protocol to use in connections.
QNetworkReply * createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData=nullptr) override
static QNetworkRequest::CacheLoadControl cacheLoadControlFromName(const QString &name)
Get QNetworkRequest::CacheLoadControl from name.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
QgsNetworkAccessManager(QObject *parent=nullptr)
QSslSocket::PeerVerifyMode sslPeerVerifyMode() const
SSL client&#39;s peer verify mode to use in connections.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
void requestTimedOut(QNetworkReply *)
QString cacheDirectory() const
void removeProxyFactory(QNetworkProxyFactory *factory)
remove a factory from the proxy factories list
const QgsAuthConfigSslServer sslCertCustomConfigByHost(const QString &hostport)
sslCertCustomConfigByHost get an SSL certificate custom config by hostport (host:port) ...
void insertProxyFactory(QNetworkProxyFactory *factory)
insert a factory into the proxy factories list
static QgsAuthManager * authManager()
Returns the application&#39;s authentication manager instance.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
qint64 maximumCacheSize() const
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
const QNetworkProxy & fallbackProxy() const
retrieve fall back proxy (for urls that no factory returned proxies for)
bool useSystemProxy() const
return whether the system proxy should be used
void requestAboutToBeCreated(QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice *)
network access manager for QGIS
void setFallbackProxyAndExcludes(const QNetworkProxy &proxy, const QStringList &excludes)
set fallback proxy and URL that shouldn&#39;t use it.
void setMaximumCacheSize(qint64 size)
Wrapper implementation of QNetworkDiskCache with all methods guarded by a mutex soly for internal use...
const QList< QNetworkProxyFactory * > proxyFactories() const
retrieve proxy factory list