23#include "moc_qgsnetworkaccessmanager.cpp"
41#include <QNetworkReply>
42#include <QRecursiveMutex>
43#include <QThreadStorage>
44#include <QAuthenticator>
45#include <QStandardPaths>
51#include <QSslConfiguration>
59static std::vector< std::pair< QString, std::function< void( QNetworkRequest * ) > > > sCustomPreprocessors;
60static std::vector< std::pair< QString, std::function< void(
const QNetworkRequest &, QNetworkReply * ) > > > sCustomReplyPreprocessors;
63class QgsNetworkProxyFactory :
public QNetworkProxyFactory
66 QgsNetworkProxyFactory() =
default;
68 QList<QNetworkProxy> queryProxy(
const QNetworkProxyQuery &query = QNetworkProxyQuery() )
override
74 for ( QNetworkProxyFactory *f : constProxyFactories )
76 QList<QNetworkProxy> systemproxies = QNetworkProxyFactory::systemProxyForQuery( query );
77 if ( !systemproxies.isEmpty() )
80 QList<QNetworkProxy> proxies = f->queryProxy( query );
81 if ( !proxies.isEmpty() )
86 if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
89 const QString url = query.url().toString();
92 for (
const QString &noProxy : constNoProxyList )
94 if ( !noProxy.trimmed().isEmpty() && url.startsWith( noProxy ) )
96 QgsDebugMsgLevel( QStringLiteral(
"don't using any proxy for %1 [exclude %2]" ).arg( url, noProxy ), 4 );
97 return QList<QNetworkProxy>() << QNetworkProxy( QNetworkProxy::NoProxy );
102 for (
const QString &exclude : constExcludeList )
104 if ( !exclude.trimmed().isEmpty() && url.startsWith( exclude ) )
106 QgsDebugMsgLevel( QStringLiteral(
"using default proxy for %1 [exclude %2]" ).arg( url, exclude ), 4 );
107 return QList<QNetworkProxy>() << QNetworkProxy( QNetworkProxy::DefaultProxy );
113 QgsDebugMsgLevel( QStringLiteral(
"requesting system proxy for query %1" ).arg( url ), 4 );
114 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery( query );
115 if ( !proxies.isEmpty() )
118 .arg( proxies.first().hostName() ).arg( proxies.first().port() ), 4 );
123 QgsDebugMsgLevel( QStringLiteral(
"using fallback proxy for %1" ).arg( url ), 4 );
130class QgsNetworkCookieJar :
public QNetworkCookieJar
136 : QNetworkCookieJar( parent )
140 bool deleteCookie(
const QNetworkCookie &cookie )
override
142 const QMutexLocker locker( &mMutex );
143 if ( QNetworkCookieJar::deleteCookie( cookie ) )
145 emit mNam->cookiesChanged( allCookies() );
150 bool insertCookie(
const QNetworkCookie &cookie )
override
152 const QMutexLocker locker( &mMutex );
153 if ( QNetworkCookieJar::insertCookie( cookie ) )
155 emit mNam->cookiesChanged( allCookies() );
160 bool setCookiesFromUrl(
const QList<QNetworkCookie> &cookieList,
const QUrl &url )
override
162 const QMutexLocker locker( &mMutex );
163 return QNetworkCookieJar::setCookiesFromUrl( cookieList, url );
165 bool updateCookie(
const QNetworkCookie &cookie )
override
167 const QMutexLocker locker( &mMutex );
168 if ( QNetworkCookieJar::updateCookie( cookie ) )
170 emit mNam->cookiesChanged( allCookies() );
177 QList<QNetworkCookie> allCookies()
const
179 const QMutexLocker locker( &mMutex );
180 return QNetworkCookieJar::allCookies();
182 void setAllCookies(
const QList<QNetworkCookie> &cookieList )
184 const QMutexLocker locker( &mMutex );
185 QNetworkCookieJar::setAllCookies( cookieList );
189 mutable QRecursiveMutex mMutex;
199 static QThreadStorage<QgsNetworkAccessManager> sInstances;
202 if ( nam->thread() == qApp->thread() )
205 if ( !nam->mInitialized )
215 : QNetworkAccessManager( parent )
216 , mSslErrorHandlerSemaphore( 1 )
217 , mAuthRequestHandlerSemaphore( 1 )
219 setProxyFactory(
new QgsNetworkProxyFactory() );
220 setCookieJar(
new QgsNetworkCookieJar(
this ) );
221 enableStrictTransportSecurityStore(
true );
222 setStrictTransportSecurityEnabled(
true );
227 Q_ASSERT( sMainNAM ==
this );
228 mSslErrorHandler = std::move( handler );
233 Q_ASSERT( sMainNAM ==
this );
234 mAuthHandler = std::move( handler );
239 mProxyFactories.insert( 0, factory );
244 mProxyFactories.removeAll( factory );
249 return mProxyFactories;
254 return mExcludedURLs;
264 return mFallbackProxy;
269 QgsDebugMsgLevel( QStringLiteral(
"proxy settings: (type:%1 host: %2:%3, user:%4, password:%5" )
270 .arg( proxy.type() == QNetworkProxy::DefaultProxy ? QStringLiteral(
"DefaultProxy" ) :
271 proxy.type() == QNetworkProxy::Socks5Proxy ? QStringLiteral(
"Socks5Proxy" ) :
272 proxy.type() == QNetworkProxy::NoProxy ? QStringLiteral(
"NoProxy" ) :
273 proxy.type() == QNetworkProxy::HttpProxy ? QStringLiteral(
"HttpProxy" ) :
274 proxy.type() == QNetworkProxy::HttpCachingProxy ? QStringLiteral(
"HttpCachingProxy" ) :
275 proxy.type() == QNetworkProxy::FtpCachingProxy ? QStringLiteral(
"FtpCachingProxy" ) :
276 QStringLiteral(
"Undefined" ),
280 proxy.password().isEmpty() ? QStringLiteral(
"not set" ) : QStringLiteral(
"set" ) ), 4 );
282 mFallbackProxy = proxy;
283 mExcludedURLs = excludes;
285 mExcludedURLs.erase( std::remove_if( mExcludedURLs.begin(), mExcludedURLs.end(),
286 [](
const QString & url )
288 return url.trimmed().isEmpty();
289 } ), mExcludedURLs.end() );
291 mNoProxyURLs = noProxyURLs;
292 mNoProxyURLs.erase( std::remove_if( mNoProxyURLs.begin(), mNoProxyURLs.end(),
293 [](
const QString & url )
295 return url.trimmed().isEmpty();
296 } ), mNoProxyURLs.end() );
303 QNetworkRequest *pReq(
const_cast< QNetworkRequest *
>( &req ) );
305 QString userAgent = s.
value( QStringLiteral(
"/qgis/networkAndProxy/userAgent" ),
"Mozilla/5.0" ).toString();
306 if ( !userAgent.isEmpty() )
308 userAgent += QStringLiteral(
"QGIS/%1/%2" ).arg(
Qgis::versionInt() ).arg( QSysInfo::prettyProductName() );
309 pReq->setRawHeader(
"User-Agent", userAgent.toLatin1() );
312 const bool ishttps = pReq->url().scheme().compare( QLatin1String(
"https" ), Qt::CaseInsensitive ) == 0;
315 QgsDebugMsgLevel( QStringLiteral(
"Adding trusted CA certs to request" ), 3 );
316 QSslConfiguration sslconfig( pReq->sslConfiguration() );
320 const QString hostport( QStringLiteral(
"%1:%2" )
321 .arg( pReq->url().host().trimmed() )
322 .arg( pReq->url().port() != -1 ? pReq->url().port() : 443 ) );
324 if ( !servconfig.
isNull() )
326 QgsDebugMsgLevel( QStringLiteral(
"Adding SSL custom config to request for %1" ).arg( hostport ), 2 );
332 pReq->setSslConfiguration( sslconfig );
336 if ( sMainNAM->mCacheDisabled )
339 pReq->setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
340 pReq->setAttribute( QNetworkRequest::CacheSaveControlAttribute,
false );
343 for (
const auto &preprocessor : sCustomPreprocessors )
345 preprocessor.second( pReq );
348 static QAtomicInt sRequestId = 0;
349 const int requestId = ++sRequestId;
351 if ( QBuffer *buffer = qobject_cast<QBuffer *>( outgoingData ) )
353 content = buffer->buffer();
360 QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
361 reply->setProperty(
"requestId", requestId );
368 connect( reply, &QNetworkReply::downloadProgress,
this, &QgsNetworkAccessManager::onReplyDownloadProgress );
370 connect( reply, &QNetworkReply::sslErrors,
this, &QgsNetworkAccessManager::onReplySslErrors );
373 for (
const auto &replyPreprocessor : sCustomReplyPreprocessors )
375 replyPreprocessor.second( req, reply );
383 QTimer *timer =
new QTimer( reply );
384 timer->setObjectName( QStringLiteral(
"timeoutTimer" ) );
385 connect( timer, &QTimer::timeout,
this, &QgsNetworkAccessManager::abortRequest );
386 timer->setSingleShot(
true );
389 connect( reply, &QNetworkReply::downloadProgress, timer, [timer] { timer->start(); } );
390 connect( reply, &QNetworkReply::uploadProgress, timer, [timer] { timer->start(); } );
391 connect( reply, &QNetworkReply::finished, timer, &QTimer::stop );
393 QgsDebugMsgLevel( QStringLiteral(
"Created [reply:%1]" ).arg(
reinterpret_cast< qint64
>( reply ), 0, 16 ), 3 );
398void QgsNetworkAccessManager::abortRequest()
400 QTimer *timer = qobject_cast<QTimer *>( sender() );
403 QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent() );
407 QgsDebugMsgLevel( QStringLiteral(
"Abort [reply:%1] %2" ).arg(
reinterpret_cast< qint64
>( reply ), 0, 16 ).arg( reply->url().toString() ), 3 );
414void QgsNetworkAccessManager::onReplyFinished( QNetworkReply *reply )
419void QgsNetworkAccessManager::onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
421 if ( QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() ) )
428void QgsNetworkAccessManager::onReplySslErrors(
const QList<QSslError> &errors )
430 QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() );
432 Q_ASSERT( reply->manager() ==
this );
434 QgsDebugMsgLevel( QStringLiteral(
"Stopping network reply timeout whilst SSL error is handled" ), 2 );
435 pauseTimeout( reply );
440 mSslErrorHandlerSemaphore.acquire();
444 emit sslErrorsOccurred( reply, errors );
445 if (
this != sMainNAM )
449 mSslErrorHandlerSemaphore.acquire();
450 mSslErrorHandlerSemaphore.release();
451 afterSslErrorHandled( reply );
455void QgsNetworkAccessManager::afterSslErrorHandled( QNetworkReply *reply )
457 if ( reply->manager() ==
this )
459 restartTimeout( reply );
460 emit sslErrorsHandled( reply );
464void QgsNetworkAccessManager::afterAuthRequestHandled( QNetworkReply *reply )
466 if ( reply->manager() ==
this )
468 restartTimeout( reply );
469 emit authRequestHandled( reply );
473void QgsNetworkAccessManager::pauseTimeout( QNetworkReply *reply )
475 Q_ASSERT( reply->manager() ==
this );
477 QTimer *timer = reply->findChild<QTimer *>( QStringLiteral(
"timeoutTimer" ) );
478 if ( timer && timer->isActive() )
484void QgsNetworkAccessManager::restartTimeout( QNetworkReply *reply )
486 Q_ASSERT( reply->manager() ==
this );
488 QTimer *timer = reply->findChild<QTimer *>( QStringLiteral(
"timeoutTimer" ) );
491 Q_ASSERT( !timer->isActive() );
492 QgsDebugMsgLevel( QStringLiteral(
"Restarting network reply timeout" ), 2 );
493 timer->setSingleShot(
true );
498int QgsNetworkAccessManager::getRequestId( QNetworkReply *reply )
500 return reply->property(
"requestId" ).toInt();
503void QgsNetworkAccessManager::handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
505 mSslErrorHandler->handleSslErrors( reply, errors );
506 afterSslErrorHandled( reply );
507 qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mSslErrorHandlerSemaphore.release();
512void QgsNetworkAccessManager::onAuthRequired( QNetworkReply *reply, QAuthenticator *auth )
515 Q_ASSERT( reply->manager() ==
this );
517 QgsDebugMsgLevel( QStringLiteral(
"Stopping network reply timeout whilst auth request is handled" ), 2 );
518 pauseTimeout( reply );
523 mAuthRequestHandlerSemaphore.acquire();
527 emit authRequestOccurred( reply, auth );
529 if (
this != sMainNAM )
533 mAuthRequestHandlerSemaphore.acquire();
534 mAuthRequestHandlerSemaphore.release();
535 afterAuthRequestHandled( reply );
541 if (
this != sMainNAM )
547 mAuthHandler->handleAuthRequestOpenBrowser( url );
552 if (
this != sMainNAM )
558 mAuthHandler->handleAuthRequestCloseBrowser();
563 if (
this != sMainNAM )
570void QgsNetworkAccessManager::handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth )
572 mAuthHandler->handleAuthRequest( reply, auth );
576 afterAuthRequestHandled( reply );
577 qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mAuthRequestHandlerSemaphore.release();
584 case QNetworkRequest::AlwaysNetwork:
585 return QStringLiteral(
"AlwaysNetwork" );
586 case QNetworkRequest::PreferNetwork:
587 return QStringLiteral(
"PreferNetwork" );
588 case QNetworkRequest::PreferCache:
589 return QStringLiteral(
"PreferCache" );
590 case QNetworkRequest::AlwaysCache:
591 return QStringLiteral(
"AlwaysCache" );
593 return QStringLiteral(
"PreferNetwork" );
598 if ( name == QLatin1String(
"AlwaysNetwork" ) )
600 return QNetworkRequest::AlwaysNetwork;
602 else if ( name == QLatin1String(
"PreferNetwork" ) )
604 return QNetworkRequest::PreferNetwork;
606 else if ( name == QLatin1String(
"PreferCache" ) )
608 return QNetworkRequest::PreferCache;
610 else if ( name == QLatin1String(
"AlwaysCache" ) )
612 return QNetworkRequest::AlwaysCache;
614 return QNetworkRequest::PreferNetwork;
620 mUseSystemProxy =
false;
622 Q_ASSERT( sMainNAM );
624 if ( sMainNAM !=
this )
626 connect(
this, &QNetworkAccessManager::proxyAuthenticationRequired,
627 sMainNAM, &QNetworkAccessManager::proxyAuthenticationRequired,
648 connect(
this, &QNetworkAccessManager::sslErrors,
649 sMainNAM, &QNetworkAccessManager::sslErrors,
662 if ( !mSslErrorHandler )
666 setAuthHandler( std::make_unique< QgsNetworkAuthenticationHandler>() );
669 connect(
this, &QgsNetworkAccessManager::sslErrorsOccurred, sMainNAM, &QgsNetworkAccessManager::handleSslErrors );
671 connect(
this, &QNetworkAccessManager::authenticationRequired,
this, &QgsNetworkAccessManager::onAuthRequired );
672 connect(
this, &QgsNetworkAccessManager::authRequestOccurred, sMainNAM, &QgsNetworkAccessManager::handleAuthRequest );
674 connect(
this, &QNetworkAccessManager::finished,
this, &QgsNetworkAccessManager::onReplyFinished );
679 QStringList excludes;
680 QStringList noProxyURLs;
682 const bool proxyEnabled = settings.
value( QStringLiteral(
"proxy/proxyEnabled" ),
false ).toBool();
687 excludes = settings.
value( QStringLiteral(
"proxy/proxyExcludedUrls" ), QStringList() ).toStringList();
689 noProxyURLs = settings.
value( QStringLiteral(
"proxy/noProxyUrls" ), QStringList() ).toStringList();
692 const QString proxyHost = settings.
value( QStringLiteral(
"proxy/proxyHost" ),
"" ).toString();
693 const int proxyPort = settings.
value( QStringLiteral(
"proxy/proxyPort" ),
"" ).toString().toInt();
695 const QString proxyUser = settings.
value( QStringLiteral(
"proxy/proxyUser" ),
"" ).toString();
696 const QString proxyPassword = settings.
value( QStringLiteral(
"proxy/proxyPassword" ),
"" ).toString();
698 const QString proxyTypeString = settings.
value( QStringLiteral(
"proxy/proxyType" ),
"" ).toString();
700 if ( proxyTypeString == QLatin1String(
"DefaultProxy" ) )
702 mUseSystemProxy =
true;
703 QNetworkProxyFactory::setUseSystemConfiguration(
true );
704 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
705 if ( !proxies.isEmpty() )
707 proxy = proxies.first();
713 QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
714 if ( proxyTypeString == QLatin1String(
"Socks5Proxy" ) )
716 proxyType = QNetworkProxy::Socks5Proxy;
718 else if ( proxyTypeString == QLatin1String(
"HttpProxy" ) )
720 proxyType = QNetworkProxy::HttpProxy;
722 else if ( proxyTypeString == QLatin1String(
"HttpCachingProxy" ) )
724 proxyType = QNetworkProxy::HttpCachingProxy;
726 else if ( proxyTypeString == QLatin1String(
"FtpCachingProxy" ) )
728 proxyType = QNetworkProxy::FtpCachingProxy;
732 .arg( proxyHost ).arg( proxyPort )
733 .arg( proxyUser, proxyPassword ), 2
735 proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
738 const QString authcfg = settings.
value( QStringLiteral(
"proxy/authcfg" ),
"" ).toString();
739 if ( !authcfg.isEmpty( ) )
741 QgsDebugMsgLevel( QStringLiteral(
"setting proxy from stored authentication configuration %1" ).arg( authcfg ), 2 );
744 authManager->updateNetworkProxy( proxy, authcfg );
755 if ( cacheDirectory.isEmpty() )
756 cacheDirectory = QStandardPaths::writableLocation( QStandardPaths::CacheLocation );
764 if ( cache() != newcache )
765 setCache( newcache );
767 if (
this != sMainNAM )
769 static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies(
static_cast<QgsNetworkCookieJar *
>( sMainNAM->cookieJar() )->allCookies() );
773void QgsNetworkAccessManager::syncCookies(
const QList<QNetworkCookie> &cookies )
775 if ( sender() !=
this )
777 static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies( cookies );
778 if (
this == sMainNAM )
799 br.
get( request, forceRefresh, feedback );
807 br.
post( request, data, forceRefresh, feedback );
813 QString
id = QUuid::createUuid().toString();
814 sCustomPreprocessors.emplace_back( std::make_pair(
id, processor ) );
820 const size_t prevCount = sCustomPreprocessors.size();
821 sCustomPreprocessors.erase( std::remove_if( sCustomPreprocessors.begin(), sCustomPreprocessors.end(), [
id]( std::pair< QString, std::function<
void( QNetworkRequest * ) > > &a )
823 return a.first == id;
824 } ), sCustomPreprocessors.end() );
825 return prevCount != sCustomPreprocessors.size();
830 QString
id = QUuid::createUuid().toString();
831 sCustomReplyPreprocessors.emplace_back( std::make_pair(
id, processor ) );
837 const size_t prevCount = sCustomReplyPreprocessors.size();
838 sCustomReplyPreprocessors.erase( std::remove_if( sCustomReplyPreprocessors.begin(), sCustomReplyPreprocessors.end(), [
id]( std::pair< QString, std::function<
void(
const QNetworkRequest &, QNetworkReply * ) > > &a )
840 return a.first == id;
841 } ), sCustomReplyPreprocessors.end() );
842 return prevCount != sCustomReplyPreprocessors.size();
847 for (
const auto &preprocessor : sCustomPreprocessors )
849 preprocessor.second( req );
859 : mOperation( operation )
860 , mRequest( request )
861 , mOriginatingThreadId( QStringLiteral(
"0x%2" ).arg( reinterpret_cast<quintptr>( QThread::currentThread() ), 2 * QT_POINTER_SIZE, 16, QLatin1Char(
'0' ) ) )
862 , mRequestId( requestId )
863 , mContent( content )
864 , mInitiatorClass( request.attribute( static_cast< QNetworkRequest::Attribute >(
QgsNetworkRequestParameters::AttributeInitiatorClass ) ).toString() )
865 , mInitiatorRequestId( request.attribute( static_cast< QNetworkRequest::Attribute >(
QgsNetworkRequestParameters::AttributeInitiatorRequestId ) ) )
877 QgsDebugError( QStringLiteral(
"SSL errors occurred accessing URL:\n%1" ).arg( reply->request().url().toString() ) );
887 QgsDebugError( QStringLiteral(
"Network reply required authentication, but no handler was in place to provide this authentication request while accessing the URL:\n%1" ).arg( reply->request().url().toString() ) );
893 QgsDebugError( QStringLiteral(
"Network authentication required external browser to open URL %1, but no handler was in place" ).arg( url.toString() ) );
898 QgsDebugError( QStringLiteral(
"Network authentication required external browser closed, but no handler was in place" ) );
902#include "qgsnetworkaccessmanager.moc"
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
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...
Configuration container for SSL server connection exceptions or overrides.
QSsl::SslProtocol sslProtocol() const
SSL server protocol to use in connections.
int sslPeerVerifyDepth() const
Number or SSL client's peer to verify in connections.
bool isNull() const
Whether configuration is null (missing components)
QSslSocket::PeerVerifyMode sslPeerVerifyMode() const
SSL client's peer verify mode to use in connections.
Singleton offering an interface to manage the authentication configuration database and to utilize co...
const QgsAuthConfigSslServer sslCertCustomConfigByHost(const QString &hostport)
sslCertCustomConfigByHost get an SSL certificate custom config by hostport (host:port)
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode post(QNetworkRequest &request, QIODevice *data, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "post" operation on the specified request, using the given data.
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
network access manager for QGIS
QStringList noProxyList() const
Returns the no proxy list.
void finished(QgsNetworkReplyContent reply)
Emitted whenever a pending network reply is finished.
static const QgsSettingsEntryInteger * settingsNetworkTimeout
Settings entry network timeout.
void cookiesChanged(const QList< QNetworkCookie > &cookies)
Emitted when the cookies changed.
static QgsNetworkReplyContent blockingGet(QNetworkRequest &request, const QString &authCfg=QString(), bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Posts a GET request to obtain the contents of the target request and returns a new QgsNetworkReplyCon...
void insertProxyFactory(QNetworkProxyFactory *factory)
Inserts a factory into the proxy factories list.
void setSslErrorHandler(std::unique_ptr< QgsSslErrorHandler > handler)
Sets the application SSL error handler, which is used to respond to SSL errors encountered during net...
Q_DECL_DEPRECATED void requestAboutToBeCreated(QNetworkAccessManager::Operation operation, const QNetworkRequest &request, QIODevice *device)
void abortAuthBrowser()
Abort any outstanding external browser login request.
void setCacheDisabled(bool disabled)
Sets whether all network caching should be disabled.
const QList< QNetworkProxyFactory * > proxyFactories() const
Returns a list of proxy factories used by the manager.
void downloadProgress(int requestId, qint64 bytesReceived, qint64 bytesTotal)
Emitted when a network reply receives a progress report.
void requestAuthOpenBrowser(const QUrl &url) const
Forwards an external browser login url opening request to the authentication handler.
void requestAuthCloseBrowser() const
Forwards an external browser login closure request to the authentication handler.
void requestEncounteredSslErrors(int requestId, const QList< QSslError > &errors)
Emitted when a network request encounters SSL errors.
static QString cacheLoadControlName(QNetworkRequest::CacheLoadControl control)
Returns the name for QNetworkRequest::CacheLoadControl.
void requestCreated(const QgsNetworkRequestParameters &request)
Emitted when a network request has been created.
static QString setReplyPreprocessor(const std::function< void(const QNetworkRequest &, QNetworkReply *)> &processor)
Sets a reply pre-processor function, which allows manipulation of QNetworkReply objects after they ar...
static bool removeRequestPreprocessor(const QString &id)
Removes the custom request pre-processor function with matching id.
void requestAuthDetailsAdded(int requestId, const QString &realm, const QString &user, const QString &password)
Emitted when network authentication details have been added to a request.
static QNetworkRequest::CacheLoadControl cacheLoadControlFromName(const QString &name)
Returns QNetworkRequest::CacheLoadControl from a name.
bool cacheDisabled() const
Returns true if all network caching is disabled.
QgsNetworkAccessManager(QObject *parent=nullptr)
void requestRequiresAuth(int requestId, const QString &realm)
Emitted when a network request prompts an authentication request.
void preprocessRequest(QNetworkRequest *req) const
Preprocesses request.
void setAuthHandler(std::unique_ptr< QgsNetworkAuthenticationHandler > handler)
Sets the application network authentication handler, which is used to respond to network authenticati...
static void setTimeout(int time)
Sets the maximum timeout time for network requests, in milliseconds.
static QgsNetworkReplyContent blockingPost(QNetworkRequest &request, const QByteArray &data, const QString &authCfg=QString(), bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Posts a POST request to obtain the contents of the target request, using the given data,...
const QNetworkProxy & fallbackProxy() const
Returns the fallback proxy used by the manager.
static int timeout()
Returns the network timeout length, in milliseconds.
void setupDefaultProxyAndCache(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Setup the QgsNetworkAccessManager (NAM) according to the user's settings.
static QString setRequestPreprocessor(const std::function< void(QNetworkRequest *request)> &processor)
Sets a request pre-processor function, which allows manipulation of a network request before it is pr...
void setFallbackProxyAndExcludes(const QNetworkProxy &proxy, const QStringList &excludes, const QStringList &noProxyURLs)
Sets the fallback proxy and URLs which shouldn't use it.
static bool removeReplyPreprocessor(const QString &id)
Removes the custom reply pre-processor function with matching id.
QStringList excludeList() const
Returns the proxy exclude list.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
void removeProxyFactory(QNetworkProxyFactory *factory)
Removes a factory from the proxy factories list.
void authBrowserAborted()
Emitted when external browser logins are to be aborted.
void requestTimedOut(QgsNetworkRequestParameters request)
Emitted when a network request has timed out.
bool useSystemProxy() const
Returns whether the system proxy should be used.
QNetworkReply * createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData=nullptr) override
virtual void handleAuthRequest(QNetworkReply *reply, QAuthenticator *auth)
Called whenever network authentication requests are encountered during a network reply.
virtual void handleAuthRequestCloseBrowser()
Called to terminate a network authentication through external browser.
virtual void handleAuthRequestOpenBrowser(const QUrl &url)
Called to initiate a network authentication through external browser url.
Wrapper implementation of QNetworkDiskCache with all methods guarded by a mutex soly for internal use...
void setCacheDirectory(const QString &cacheDir)
qint64 maximumCacheSize() const
void setMaximumCacheSize(qint64 size)
QString cacheDirectory() const
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
Encapsulates parameters and properties of a network request.
QgsNetworkRequestParameters()=default
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
An integer settings entry.
static const QgsSettingsEntryInteger64 * settingsNetworkCacheSize
Settings entry network cache directory.
static const QgsSettingsEntryString * settingsNetworkCacheDirectory
Settings entry network cache directory.
static QgsSettingsTreeNode * sTreeNetwork
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
virtual void handleSslErrors(QNetworkReply *reply, const QList< QSslError > &errors)
Called whenever SSL errors are encountered during a network reply.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)