QGIS API Documentation  2.13.0-Master
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 
29 #include <QUrl>
30 #include <QSettings>
31 #include <QTimer>
32 #include <QNetworkReply>
33 #include <QNetworkDiskCache>
34 
35 #ifndef QT_NO_OPENSSL
36 #include <QSslConfiguration>
37 #endif
38 
39 #include "qgsauthmanager.h"
40 
42 class QgsNetworkProxyFactory : public QNetworkProxyFactory
43 {
44  public:
45  QgsNetworkProxyFactory() {}
46  virtual ~QgsNetworkProxyFactory() {}
47 
48  virtual QList<QNetworkProxy> queryProxy( const QNetworkProxyQuery & query = QNetworkProxyQuery() ) override
49  {
51 
52  // iterate proxies factories and take first non empty list
53  Q_FOREACH ( QNetworkProxyFactory *f, nam->proxyFactories() )
54  {
55  QList<QNetworkProxy> systemproxies = f->systemProxyForQuery( query );
56  if ( !systemproxies.isEmpty() )
57  return systemproxies;
58 
59  QList<QNetworkProxy> proxies = f->queryProxy( query );
60  if ( !proxies.isEmpty() )
61  return proxies;
62  }
63 
64  // no proxies from the proxy factor list check for excludes
65  if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
66  return QList<QNetworkProxy>() << nam->fallbackProxy();
67 
68  QString url = query.url().toString();
69 
70  Q_FOREACH ( const QString& exclude, nam->excludeList() )
71  {
72  if ( url.startsWith( exclude ) )
73  {
74  QgsDebugMsg( QString( "using default proxy for %1 [exclude %2]" ).arg( url, exclude ) );
75  return QList<QNetworkProxy>() << QNetworkProxy();
76  }
77  }
78 
79  if ( nam->useSystemProxy() )
80  {
81  QgsDebugMsg( QString( "requesting system proxy for query %1" ).arg( url ) );
83  if ( !proxies.isEmpty() )
84  {
85  QgsDebugMsg( QString( "using system proxy %1:%2 for query" )
86  .arg( proxies.first().hostName() ).arg( proxies.first().port() ) );
87  return proxies;
88  }
89  }
90 
91  QgsDebugMsg( QString( "using fallback proxy for %1" ).arg( url ) );
92  return QList<QNetworkProxy>() << nam->fallbackProxy();
93  }
94 };
96 
97 //
98 // Static calls to enforce singleton behaviour
99 //
101 {
103  return sInstance;
104 }
105 
107  : QNetworkAccessManager( parent )
108  , mUseSystemProxy( false )
109 {
110  setProxyFactory( new QgsNetworkProxyFactory() );
111 }
112 
114 {
115 }
116 
118 {
119  mProxyFactories.insert( 0, factory );
120 }
121 
123 {
124  mProxyFactories.removeAll( factory );
125 }
126 
128 {
129  return mProxyFactories;
130 }
131 
133 {
134  return mExcludedURLs;
135 }
136 
138 {
139  return mFallbackProxy;
140 }
141 
143 {
144  QgsDebugMsg( QString( "proxy settings: (type:%1 host: %2:%3, user:%4, password:%5" )
145  .arg( proxy.type() == QNetworkProxy::DefaultProxy ? "DefaultProxy" :
146  proxy.type() == QNetworkProxy::Socks5Proxy ? "Socks5Proxy" :
147  proxy.type() == QNetworkProxy::NoProxy ? "NoProxy" :
148  proxy.type() == QNetworkProxy::HttpProxy ? "HttpProxy" :
149  proxy.type() == QNetworkProxy::HttpCachingProxy ? "HttpCachingProxy" :
150  proxy.type() == QNetworkProxy::FtpCachingProxy ? "FtpCachingProxy" :
151  "Undefined",
152  proxy.hostName() )
153  .arg( proxy.port() )
154  .arg( proxy.user(),
155  proxy.password().isEmpty() ? "not set" : "set" ) );
156 
157  mFallbackProxy = proxy;
158  mExcludedURLs = excludes;
159 }
160 
161 QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData )
162 {
163  QSettings s;
164 
165  QNetworkRequest *pReq( const_cast< QNetworkRequest * >( &req ) ); // hack user agent
166 
167  QString userAgent = s.value( "/qgis/networkAndProxy/userAgent", "Mozilla/5.0" ).toString();
168  if ( !userAgent.isEmpty() )
169  userAgent += ' ';
170  userAgent += QString( "QGIS/%1" ).arg( QGis::QGIS_VERSION );
171  pReq->setRawHeader( "User-Agent", userAgent.toUtf8() );
172 
173 #ifndef QT_NO_OPENSSL
174  bool ishttps = pReq->url().scheme().toLower() == "https";
175  if ( ishttps && !QgsAuthManager::instance()->isDisabled() )
176  {
177  QgsDebugMsg( "Adding trusted CA certs to request" );
178  QSslConfiguration sslconfig( pReq->sslConfiguration() );
179  sslconfig.setCaCertificates( QgsAuthManager::instance()->getTrustedCaCertsCache() );
180 
181  // check for SSL cert custom config
182  QString hostport( QString( "%1:%2" )
183  .arg( pReq->url().host().trimmed() )
184  .arg( pReq->url().port() != -1 ? pReq->url().port() : 443 ) );
186  if ( !servconfig.isNull() )
187  {
188  QgsDebugMsg( QString( "Adding SSL custom config to request for %1" ).arg( hostport ) );
189  sslconfig.setProtocol( servconfig.sslProtocol() );
190  sslconfig.setPeerVerifyMode( servconfig.sslPeerVerifyMode() );
191  sslconfig.setPeerVerifyDepth( servconfig.sslPeerVerifyDepth() );
192  }
193 
194  pReq->setSslConfiguration( sslconfig );
195  }
196 #endif
197 
198  emit requestAboutToBeCreated( op, req, outgoingData );
199  QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
200 
201  emit requestCreated( reply );
202 
203  // abort request, when network timeout happens
204  QTimer *timer = new QTimer( reply );
205  timer->setObjectName( "timeoutTimer" );
206  connect( timer, SIGNAL( timeout() ), this, SLOT( abortRequest() ) );
207  timer->setSingleShot( true );
208  timer->start( s.value( "/qgis/networkAndProxy/networkTimeout", "20000" ).toInt() );
209 
210  connect( reply, SIGNAL( downloadProgress( qint64, qint64 ) ), timer, SLOT( start() ) );
211  connect( reply, SIGNAL( uploadProgress( qint64, qint64 ) ), timer, SLOT( start() ) );
212 
213  return reply;
214 }
215 
216 void QgsNetworkAccessManager::abortRequest()
217 {
218  QTimer *timer = qobject_cast<QTimer *>( sender() );
219  Q_ASSERT( timer );
220 
221  QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent() );
222  Q_ASSERT( reply );
223 
224  QgsMessageLog::logMessage( tr( "Network request %1 timed out" ).arg( reply->url().toString() ), tr( "Network" ) );
225 
226  if ( reply->isRunning() )
227  reply->close();
228 
229  emit requestTimedOut( reply );
230 }
231 
232 QString QgsNetworkAccessManager::cacheLoadControlName( QNetworkRequest::CacheLoadControl theControl )
233 {
234  switch ( theControl )
235  {
236  case QNetworkRequest::AlwaysNetwork:
237  return "AlwaysNetwork";
238  case QNetworkRequest::PreferNetwork:
239  return "PreferNetwork";
240  case QNetworkRequest::PreferCache:
241  return "PreferCache";
242  case QNetworkRequest::AlwaysCache:
243  return "AlwaysCache";
244  default:
245  break;
246  }
247  return "PreferNetwork";
248 }
249 
250 QNetworkRequest::CacheLoadControl QgsNetworkAccessManager::cacheLoadControlFromName( const QString &theName )
251 {
252  if ( theName == "AlwaysNetwork" )
253  {
254  return QNetworkRequest::AlwaysNetwork;
255  }
256  else if ( theName == "PreferNetwork" )
257  {
258  return QNetworkRequest::PreferNetwork;
259  }
260  else if ( theName == "PreferCache" )
261  {
262  return QNetworkRequest::PreferCache;
263  }
264  else if ( theName == "AlwaysCache" )
265  {
266  return QNetworkRequest::AlwaysCache;
267  }
268  return QNetworkRequest::PreferNetwork;
269 }
270 
272 {
274  QStringList excludes;
275 
276  QSettings settings;
277 
278  mUseSystemProxy = false;
279 
280  if ( this != instance() )
281  {
282  Qt::ConnectionType connectionType = thread() == instance()->thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection;
283 
286  connectionType );
287 
288  connect( this, SIGNAL( proxyAuthenticationRequired( const QNetworkProxy &, QAuthenticator * ) ),
290  connectionType );
291 
292  connect( this, SIGNAL( requestTimedOut( QNetworkReply* ) ),
293  instance(), SIGNAL( requestTimedOut( QNetworkReply* ) ) );
294 
295 #ifndef QT_NO_OPENSSL
296  connect( this, SIGNAL( sslErrors( QNetworkReply *, const QList<QSslError> & ) ),
297  instance(), SIGNAL( sslErrors( QNetworkReply *, const QList<QSslError> & ) ),
298  connectionType );
299 #endif
300  }
301 
302  // check if proxy is enabled
303  bool proxyEnabled = settings.value( "proxy/proxyEnabled", false ).toBool();
304  if ( proxyEnabled )
305  {
306  excludes = settings.value( "proxy/proxyExcludedUrls", "" ).toString().split( '|', QString::SkipEmptyParts );
307 
308  //read type, host, port, user, passw from settings
309  QString proxyHost = settings.value( "proxy/proxyHost", "" ).toString();
310  int proxyPort = settings.value( "proxy/proxyPort", "" ).toString().toInt();
311  QString proxyUser = settings.value( "proxy/proxyUser", "" ).toString();
312  QString proxyPassword = settings.value( "proxy/proxyPassword", "" ).toString();
313 
314  QString proxyTypeString = settings.value( "proxy/proxyType", "" ).toString();
315 
316  if ( proxyTypeString == "DefaultProxy" )
317  {
318  mUseSystemProxy = true;
321  if ( !proxies.isEmpty() )
322  {
323  proxy = proxies.first();
324  }
325  QgsDebugMsg( "setting default proxy" );
326  }
327  else
328  {
329  QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
330  if ( proxyTypeString == "Socks5Proxy" )
331  {
332  proxyType = QNetworkProxy::Socks5Proxy;
333  }
334  else if ( proxyTypeString == "HttpProxy" )
335  {
336  proxyType = QNetworkProxy::HttpProxy;
337  }
338  else if ( proxyTypeString == "HttpCachingProxy" )
339  {
340  proxyType = QNetworkProxy::HttpCachingProxy;
341  }
342  else if ( proxyTypeString == "FtpCachingProxy" )
343  {
344  proxyType = QNetworkProxy::FtpCachingProxy;
345  }
346  QgsDebugMsg( QString( "setting proxy %1 %2:%3 %4/%5" )
347  .arg( proxyType )
348  .arg( proxyHost ).arg( proxyPort )
349  .arg( proxyUser, proxyPassword )
350  );
351  proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
352  }
353  }
354 
355  setFallbackProxyAndExcludes( proxy, excludes );
356 
357  QNetworkDiskCache *newcache = qobject_cast<QNetworkDiskCache*>( cache() );
358  if ( !newcache )
359  newcache = new QNetworkDiskCache( this );
360 
361  QString cacheDirectory = settings.value( "cache/directory", QgsApplication::qgisSettingsDirPath() + "cache" ).toString();
362  qint64 cacheSize = settings.value( "cache/size", 50 * 1024 * 1024 ).toULongLong();
363  QgsDebugMsg( QString( "setCacheDirectory: %1" ).arg( cacheDirectory ) );
364  QgsDebugMsg( QString( "setMaximumCacheSize: %1" ).arg( cacheSize ) );
365  newcache->setCacheDirectory( cacheDirectory );
366  newcache->setMaximumCacheSize( cacheSize );
367  QgsDebugMsg( QString( "cacheDirectory: %1" ).arg( newcache->cacheDirectory() ) );
368  QgsDebugMsg( QString( "maximumCacheSize: %1" ).arg( newcache->maximumCacheSize() ) );
369 
370  if ( cache() != newcache )
371  setCache( newcache );
372 }
373 
375 {
376  QgsDebugMsg( "Entered" );
377  QNetworkReply * reply = get( request );
378  emit requestSent( reply, QObject::sender() );
379 }
380 
382 {
383  QgsDebugMsg( "Entered" );
384  if ( !reply )
385  {
386  return;
387  }
388  reply->abort();
389  reply->deleteLater();
390 }
static const char * QGIS_VERSION
Definition: qgis.h:42
const QStringList & excludeList() const
retrieve exclude list (urls shouldn't use the fallback proxy)
void requestCreated(QNetworkReply *)
virtual QNetworkReply * createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData)
QString password() const
static QString cacheLoadControlName(QNetworkRequest::CacheLoadControl theControl)
Get name for QNetworkRequest::CacheLoadControl.
void sendGet(const QNetworkRequest &request)
Send GET request, calls get().
void sslErrors(QNetworkReply *reply, const QList< QSslError > &errors)
static QgsAuthManager * instance()
Enforce singleton pattern.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
QString user() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
quint16 port() const
QObject * sender() const
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QSslConfiguration sslConfiguration() const
static QNetworkRequest::CacheLoadControl cacheLoadControlFromName(const QString &theName)
Get QNetworkRequest::CacheLoadControl from name.
bool isRunning() const
QString host() const
bool isNull() const
Whether configuration is null (missing components)
const QNetworkProxy & fallbackProxy() const
retrieve fall back proxy (for urls that no factory returned proxies for)
Configuration container for SSL server connection exceptions or overrides.
void authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
void setupDefaultProxyAndCache()
Setup the NAM according to the user's settings.
int port() const
QThread * thread() const
QString toString(QFlags< QUrl::FormattingOption > options) const
qulonglong toULongLong(bool *ok) const
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual QNetworkReply * createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData=nullptr) override
const QList< QNetworkProxyFactory * > proxyFactories() const
retrieve proxy factory list
QgsNetworkAccessManager(QObject *parent=nullptr)
QAbstractNetworkCache * cache() const
void setCacheDirectory(const QString &cacheDir)
void setProxyFactory(QNetworkProxyFactory *factory)
QNetworkProxy proxy() const
QString hostName() const
void setCaCertificates(const QList< QSslCertificate > &certificates)
virtual QList< QNetworkProxy > queryProxy(const QNetworkProxyQuery &query)=0
void deleteReply(QNetworkReply *reply)
Abort and delete reply.
int toInt(bool *ok) const
QSslSocket::PeerVerifyMode sslPeerVerifyMode() const
SSL client's peer verify mode to use in connections.
QNetworkProxy::ProxyType type() const
void requestTimedOut(QNetworkReply *)
QSsl::SslProtocol sslProtocol() const
SSL server protocol to use in connections.
int toInt(bool *ok, int base) const
bool isEmpty() const
void setObjectName(const QString &name)
bool isEmpty() const
int removeAll(const T &value)
QString trimmed() const
void requestSent(QNetworkReply *reply, QObject *sender)
Emitted when request was sent by request()
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
static void logMessage(const QString &message, const QString &tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
void setMaximumCacheSize(qint64 size)
void removeProxyFactory(QNetworkProxyFactory *factory)
remove a factory from the proxy factories list
QCoreApplication * instance()
void deleteLater()
T & first()
QString scheme() const
QString toLower() const
virtual void abort()=0
void insertProxyFactory(QNetworkProxyFactory *factory)
insert a factory into the proxy factories list
QUrl url() const
QString cacheDirectory() const
QVariant value(const QString &key, const QVariant &defaultValue) const
int sslPeerVerifyDepth() const
Number or SSL client's peer to verify in connections.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
void insert(int i, const T &value)
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
QList< QNetworkProxy > systemProxyForQuery(const QNetworkProxyQuery &query)
QUrl url() const
const QgsAuthConfigSslServer getSslCertCustomConfigByHost(const QString &hostport)
Get an SSL certificate custom config by host:port.
bool toBool() const
void start(int msec)
void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
void setUseSystemConfiguration(bool enable)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
virtual void close()
void requestAboutToBeCreated(QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice *)
void setCache(QAbstractNetworkCache *cache)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
void setFallbackProxyAndExcludes(const QNetworkProxy &proxy, const QStringList &excludes)
set fallback proxy and URL that shouldn't use it.
void setSslConfiguration(const QSslConfiguration &config)
qint64 maximumCacheSize() const
void setSingleShot(bool singleShot)
QByteArray toUtf8() const