32 #include <QNetworkReply> 33 #include <QThreadStorage> 36 #include <QSslConfiguration> 48 QgsNetworkProxyFactory() {}
49 virtual ~QgsNetworkProxyFactory() {}
68 if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
71 QString url = query.url().toString();
77 QgsDebugMsg(
QString(
"using default proxy for %1 [exclude %2]" ).arg( url, exclude ) );
89 .arg( proxies.
first().hostName() ).arg( proxies.
first().port() ) );
111 if ( !nam->mInitialized )
119 , mUseSystemProxy( false )
120 , mInitialized( false )
131 mProxyFactories.
insert( 0, factory );
141 return mProxyFactories;
146 return mExcludedURLs;
151 return mFallbackProxy;
157 .arg( proxy.
type() == QNetworkProxy::DefaultProxy ?
"DefaultProxy" :
158 proxy.
type() == QNetworkProxy::Socks5Proxy ?
"Socks5Proxy" :
159 proxy.
type() == QNetworkProxy::NoProxy ?
"NoProxy" :
160 proxy.
type() == QNetworkProxy::HttpProxy ?
"HttpProxy" :
161 proxy.
type() == QNetworkProxy::HttpCachingProxy ?
"HttpCachingProxy" :
162 proxy.
type() == QNetworkProxy::FtpCachingProxy ?
"FtpCachingProxy" :
169 mFallbackProxy =
proxy;
170 mExcludedURLs = excludes;
185 #ifndef QT_NO_OPENSSL 189 QgsDebugMsg(
"Adding trusted CA certs to request" );
198 if ( !servconfig.
isNull() )
200 QgsDebugMsg(
QString(
"Adding SSL custom config to request for %1" ).arg( hostport ) );
220 connect( timer, SIGNAL( timeout() ),
this, SLOT( abortRequest() ) );
222 timer->
start( s.
value(
"/qgis/networkAndProxy/networkTimeout",
"60000" ).
toInt() );
224 connect( reply, SIGNAL( downloadProgress( qint64, qint64 ) ), timer, SLOT( start() ) );
225 connect( reply, SIGNAL( uploadProgress( qint64, qint64 ) ), timer, SLOT( start() ) );
232 void QgsNetworkAccessManager::abortRequest()
241 QgsDebugMsgLevel(
QString(
"Abort [reply:%1] %2" ).arg(( qint64 ) reply, 0, 16 ).arg( reply->url().toString() ), 3 );
251 switch ( theControl )
253 case QNetworkRequest::AlwaysNetwork:
254 return "AlwaysNetwork";
255 case QNetworkRequest::PreferNetwork:
256 return "PreferNetwork";
257 case QNetworkRequest::PreferCache:
258 return "PreferCache";
259 case QNetworkRequest::AlwaysCache:
260 return "AlwaysCache";
264 return "PreferNetwork";
269 if ( theName ==
"AlwaysNetwork" )
271 return QNetworkRequest::AlwaysNetwork;
273 else if ( theName ==
"PreferNetwork" )
275 return QNetworkRequest::PreferNetwork;
277 else if ( theName ==
"PreferCache" )
279 return QNetworkRequest::PreferCache;
281 else if ( theName ==
"AlwaysCache" )
283 return QNetworkRequest::AlwaysCache;
285 return QNetworkRequest::PreferNetwork;
291 mUseSystemProxy =
false;
293 Q_ASSERT( smMainNAM );
295 if ( smMainNAM !=
this )
299 Qt::BlockingQueuedConnection );
303 Qt::BlockingQueuedConnection );
308 #ifndef QT_NO_OPENSSL 311 Qt::BlockingQueuedConnection );
320 bool proxyEnabled = settings.
value(
"proxy/proxyEnabled",
false ).
toBool();
323 excludes = settings.
value(
"proxy/proxyExcludedUrls",
"" ).
toString().
split(
'|', QString::SkipEmptyParts );
333 if ( proxyTypeString ==
"DefaultProxy" )
335 mUseSystemProxy =
true;
340 proxy = proxies.
first();
346 QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
347 if ( proxyTypeString ==
"Socks5Proxy" )
349 proxyType = QNetworkProxy::Socks5Proxy;
351 else if ( proxyTypeString ==
"HttpProxy" )
353 proxyType = QNetworkProxy::HttpProxy;
355 else if ( proxyTypeString ==
"HttpCachingProxy" )
357 proxyType = QNetworkProxy::HttpCachingProxy;
359 else if ( proxyTypeString ==
"FtpCachingProxy" )
361 proxyType = QNetworkProxy::FtpCachingProxy;
365 .arg( proxyHost ).arg( proxyPort )
366 .arg( proxyUser, proxyPassword )
368 proxy =
QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
379 if ( cacheDirectory.
isEmpty() )
381 qint64 cacheSize = settings.
value(
"cache/size", 50 * 1024 * 1024 ).
toULongLong();
387 if (
cache() != newcache )
bool isNull() const
Whether configuration is null (missing components)
void requestCreated(QNetworkReply *)
virtual QNetworkReply * createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData)
static QString cacheLoadControlName(QNetworkRequest::CacheLoadControl theControl)
Get name for QNetworkRequest::CacheLoadControl.
Q_DECL_DEPRECATED void sendGet(const QNetworkRequest &request)
Send GET request, calls get().
void sslErrors(QNetworkReply *reply, const QList< QSslError > &errors)
static QgsAuthManager * instance()
Enforce singleton pattern.
int sslPeerVerifyDepth() const
Number or SSL client's peer to verify in connections.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
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.
void setCacheDirectory(const QString &cacheDir)
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.
qulonglong toULongLong(bool *ok) const
QSsl::SslProtocol sslProtocol() const
SSL server protocol to use in connections.
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual QNetworkReply * createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData=nullptr) override
QgsNetworkAccessManager(QObject *parent=nullptr)
QAbstractNetworkCache * cache() const
QSslSocket::PeerVerifyMode sslPeerVerifyMode() const
SSL client's peer verify mode to use in connections.
void setProxyFactory(QNetworkProxyFactory *factory)
QNetworkProxy proxy() const
~QgsNetworkAccessManager()
destructor
void setCaCertificates(const QList< QSslCertificate > &certificates)
virtual QList< QNetworkProxy > queryProxy(const QNetworkProxyQuery &query)=0
Q_DECL_DEPRECATED void deleteReply(QNetworkReply *reply)
Abort and delete reply.
int toInt(bool *ok) const
#define QgsDebugMsgLevel(str, level)
QNetworkProxy::ProxyType type() const
void requestTimedOut(QNetworkReply *)
int toInt(bool *ok, int base) const
void setObjectName(const QString &name)
int removeAll(const T &value)
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)
QString cacheDirectory() const
void removeProxyFactory(QNetworkProxyFactory *factory)
remove a factory from the proxy factories list
static QString QGIS_VERSION
void insertProxyFactory(QNetworkProxyFactory *factory)
insert a factory into the proxy factories list
const QStringList & excludeList() const
retrieve exclude list (urls shouldn't use the fallback proxy)
QVariant value(const QString &key, const QVariant &defaultValue) const
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
qint64 maximumCacheSize() const
void insert(int i, const T &value)
QList< QNetworkProxy > systemProxyForQuery(const QNetworkProxyQuery &query)
const QNetworkProxy & fallbackProxy() const
retrieve fall back proxy (for urls that no factory returned proxies for)
const QgsAuthConfigSslServer getSslCertCustomConfigByHost(const QString &hostport)
Get an SSL certificate custom config by host:port.
void finished(QNetworkReply *reply)
void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
void setUseSystemConfiguration(bool enable)
bool useSystemProxy() const
return whether the system proxy should be used
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void requestAboutToBeCreated(QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice *)
void setCache(QAbstractNetworkCache *cache)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
network access manager for QGIS
void setFallbackProxyAndExcludes(const QNetworkProxy &proxy, const QStringList &excludes)
set fallback proxy and URL that shouldn't use it.
void setMaximumCacheSize(qint64 size)
void setSslConfiguration(const QSslConfiguration &config)
Wrapper implementation of QNetworkDiskCache with all methods guarded by a mutex soly for internal use...
void setSingleShot(bool singleShot)
const QList< QNetworkProxyFactory * > proxyFactories() const
retrieve proxy factory list
QByteArray toUtf8() const