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 setRedirectPolicy( QNetworkRequest::NoLessSafeRedirectPolicy );
220 setProxyFactory(
new QgsNetworkProxyFactory() );
221 setCookieJar(
new QgsNetworkCookieJar(
this ) );
222 enableStrictTransportSecurityStore(
true );
223 setStrictTransportSecurityEnabled(
true );
228 Q_ASSERT( sMainNAM ==
this );
229 mSslErrorHandler = std::move( handler );
234 Q_ASSERT( sMainNAM ==
this );
235 mAuthHandler = std::move( handler );
240 mProxyFactories.insert( 0, factory );
245 mProxyFactories.removeAll( factory );
250 return mProxyFactories;
255 return mExcludedURLs;
265 return mFallbackProxy;
270 QgsDebugMsgLevel( QStringLiteral(
"proxy settings: (type:%1 host: %2:%3, user:%4, password:%5" )
271 .arg( proxy.type() == QNetworkProxy::DefaultProxy ? QStringLiteral(
"DefaultProxy" ) :
272 proxy.type() == QNetworkProxy::Socks5Proxy ? QStringLiteral(
"Socks5Proxy" ) :
273 proxy.type() == QNetworkProxy::NoProxy ? QStringLiteral(
"NoProxy" ) :
274 proxy.type() == QNetworkProxy::HttpProxy ? QStringLiteral(
"HttpProxy" ) :
275 proxy.type() == QNetworkProxy::HttpCachingProxy ? QStringLiteral(
"HttpCachingProxy" ) :
276 proxy.type() == QNetworkProxy::FtpCachingProxy ? QStringLiteral(
"FtpCachingProxy" ) :
277 QStringLiteral(
"Undefined" ),
281 proxy.password().isEmpty() ? QStringLiteral(
"not set" ) : QStringLiteral(
"set" ) ), 4 );
283 mFallbackProxy = proxy;
284 mExcludedURLs = excludes;
286 mExcludedURLs.erase( std::remove_if( mExcludedURLs.begin(), mExcludedURLs.end(),
287 [](
const QString & url )
289 return url.trimmed().isEmpty();
290 } ), mExcludedURLs.end() );
292 mNoProxyURLs = noProxyURLs;
293 mNoProxyURLs.erase( std::remove_if( mNoProxyURLs.begin(), mNoProxyURLs.end(),
294 [](
const QString & url )
296 return url.trimmed().isEmpty();
297 } ), mNoProxyURLs.end() );
304 QNetworkRequest *pReq(
const_cast< QNetworkRequest *
>( &req ) );
306 QString userAgent = s.
value( QStringLiteral(
"/qgis/networkAndProxy/userAgent" ),
"Mozilla/5.0" ).toString();
307 if ( !userAgent.isEmpty() )
309 userAgent += QStringLiteral(
"QGIS/%1/%2" ).arg(
Qgis::versionInt() ).arg( QSysInfo::prettyProductName() );
310 pReq->setRawHeader(
"User-Agent", userAgent.toLatin1() );
313 const bool ishttps = pReq->url().scheme().compare( QLatin1String(
"https" ), Qt::CaseInsensitive ) == 0;
316 QgsDebugMsgLevel( QStringLiteral(
"Adding trusted CA certs to request" ), 3 );
317 QSslConfiguration sslconfig( pReq->sslConfiguration() );
321 const QString hostport( QStringLiteral(
"%1:%2" )
322 .arg( pReq->url().host().trimmed() )
323 .arg( pReq->url().port() != -1 ? pReq->url().port() : 443 ) );
325 if ( !servconfig.
isNull() )
327 QgsDebugMsgLevel( QStringLiteral(
"Adding SSL custom config to request for %1" ).arg( hostport ), 2 );
333 pReq->setSslConfiguration( sslconfig );
337 if ( sMainNAM->mCacheDisabled )
340 pReq->setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
341 pReq->setAttribute( QNetworkRequest::CacheSaveControlAttribute,
false );
344 for (
const auto &preprocessor : sCustomPreprocessors )
346 preprocessor.second( pReq );
349 static QAtomicInt sRequestId = 0;
350 const int requestId = ++sRequestId;
352 if ( QBuffer *buffer = qobject_cast<QBuffer *>( outgoingData ) )
354 content = buffer->buffer();
361 QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
362 reply->setProperty(
"requestId", requestId );
369 connect( reply, &QNetworkReply::downloadProgress,
this, &QgsNetworkAccessManager::onReplyDownloadProgress );
371 connect( reply, &QNetworkReply::sslErrors,
this, &QgsNetworkAccessManager::onReplySslErrors );
374 for (
const auto &replyPreprocessor : sCustomReplyPreprocessors )
376 replyPreprocessor.second( req, reply );
384 QTimer *timer =
new QTimer( reply );
385 timer->setObjectName( QStringLiteral(
"timeoutTimer" ) );
386 connect( timer, &QTimer::timeout,
this, &QgsNetworkAccessManager::abortRequest );
387 timer->setSingleShot(
true );
390 connect( reply, &QNetworkReply::downloadProgress, timer, [timer] { timer->start(); } );
391 connect( reply, &QNetworkReply::uploadProgress, timer, [timer] { timer->start(); } );
392 connect( reply, &QNetworkReply::finished, timer, &QTimer::stop );
394 QgsDebugMsgLevel( QStringLiteral(
"Created [reply:%1]" ).arg(
reinterpret_cast< qint64
>( reply ), 0, 16 ), 3 );
399void QgsNetworkAccessManager::abortRequest()
401 QTimer *timer = qobject_cast<QTimer *>( sender() );
404 QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent() );
408 QgsDebugMsgLevel( QStringLiteral(
"Abort [reply:%1] %2" ).arg(
reinterpret_cast< qint64
>( reply ), 0, 16 ).arg( reply->url().toString() ), 3 );
415void QgsNetworkAccessManager::onReplyFinished( QNetworkReply *reply )
420void QgsNetworkAccessManager::onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
422 if ( QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() ) )
429void QgsNetworkAccessManager::onReplySslErrors(
const QList<QSslError> &errors )
431 QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() );
433 Q_ASSERT( reply->manager() ==
this );
435 QgsDebugMsgLevel( QStringLiteral(
"Stopping network reply timeout whilst SSL error is handled" ), 2 );
436 pauseTimeout( reply );
441 mSslErrorHandlerSemaphore.acquire();
445 emit sslErrorsOccurred( reply, errors );
446 if (
this != sMainNAM )
450 mSslErrorHandlerSemaphore.acquire();
451 mSslErrorHandlerSemaphore.release();
452 afterSslErrorHandled( reply );
456void QgsNetworkAccessManager::afterSslErrorHandled( QNetworkReply *reply )
458 if ( reply->manager() ==
this )
460 restartTimeout( reply );
461 emit sslErrorsHandled( reply );
465void QgsNetworkAccessManager::afterAuthRequestHandled( QNetworkReply *reply )
467 if ( reply->manager() ==
this )
469 restartTimeout( reply );
470 emit authRequestHandled( reply );
474void QgsNetworkAccessManager::pauseTimeout( QNetworkReply *reply )
476 Q_ASSERT( reply->manager() ==
this );
478 QTimer *timer = reply->findChild<QTimer *>( QStringLiteral(
"timeoutTimer" ) );
479 if ( timer && timer->isActive() )
485void QgsNetworkAccessManager::restartTimeout( QNetworkReply *reply )
487 Q_ASSERT( reply->manager() ==
this );
489 QTimer *timer = reply->findChild<QTimer *>( QStringLiteral(
"timeoutTimer" ) );
492 Q_ASSERT( !timer->isActive() );
493 QgsDebugMsgLevel( QStringLiteral(
"Restarting network reply timeout" ), 2 );
494 timer->setSingleShot(
true );
499int QgsNetworkAccessManager::getRequestId( QNetworkReply *reply )
501 return reply->property(
"requestId" ).toInt();
504void QgsNetworkAccessManager::handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
506 mSslErrorHandler->handleSslErrors( reply, errors );
507 afterSslErrorHandled( reply );
508 qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mSslErrorHandlerSemaphore.release();
513void QgsNetworkAccessManager::onAuthRequired( QNetworkReply *reply, QAuthenticator *auth )
516 Q_ASSERT( reply->manager() ==
this );
518 QgsDebugMsgLevel( QStringLiteral(
"Stopping network reply timeout whilst auth request is handled" ), 2 );
519 pauseTimeout( reply );
524 mAuthRequestHandlerSemaphore.acquire();
528 emit authRequestOccurred( reply, auth );
530 if (
this != sMainNAM )
534 mAuthRequestHandlerSemaphore.acquire();
535 mAuthRequestHandlerSemaphore.release();
536 afterAuthRequestHandled( reply );
542 if (
this != sMainNAM )
548 mAuthHandler->handleAuthRequestOpenBrowser( url );
553 if (
this != sMainNAM )
559 mAuthHandler->handleAuthRequestCloseBrowser();
564 if (
this != sMainNAM )
571void QgsNetworkAccessManager::handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth )
573 mAuthHandler->handleAuthRequest( reply, auth );
577 afterAuthRequestHandled( reply );
578 qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mAuthRequestHandlerSemaphore.release();
585 case QNetworkRequest::AlwaysNetwork:
586 return QStringLiteral(
"AlwaysNetwork" );
587 case QNetworkRequest::PreferNetwork:
588 return QStringLiteral(
"PreferNetwork" );
589 case QNetworkRequest::PreferCache:
590 return QStringLiteral(
"PreferCache" );
591 case QNetworkRequest::AlwaysCache:
592 return QStringLiteral(
"AlwaysCache" );
594 return QStringLiteral(
"PreferNetwork" );
599 if ( name == QLatin1String(
"AlwaysNetwork" ) )
601 return QNetworkRequest::AlwaysNetwork;
603 else if ( name == QLatin1String(
"PreferNetwork" ) )
605 return QNetworkRequest::PreferNetwork;
607 else if ( name == QLatin1String(
"PreferCache" ) )
609 return QNetworkRequest::PreferCache;
611 else if ( name == QLatin1String(
"AlwaysCache" ) )
613 return QNetworkRequest::AlwaysCache;
615 return QNetworkRequest::PreferNetwork;
621 mUseSystemProxy =
false;
623 Q_ASSERT( sMainNAM );
625 if ( sMainNAM !=
this )
627 connect(
this, &QNetworkAccessManager::proxyAuthenticationRequired,
628 sMainNAM, &QNetworkAccessManager::proxyAuthenticationRequired,
649 connect(
this, &QNetworkAccessManager::sslErrors,
650 sMainNAM, &QNetworkAccessManager::sslErrors,
663 if ( !mSslErrorHandler )
667 setAuthHandler( std::make_unique< QgsNetworkAuthenticationHandler>() );
670 connect(
this, &QgsNetworkAccessManager::sslErrorsOccurred, sMainNAM, &QgsNetworkAccessManager::handleSslErrors );
672 connect(
this, &QNetworkAccessManager::authenticationRequired,
this, &QgsNetworkAccessManager::onAuthRequired );
673 connect(
this, &QgsNetworkAccessManager::authRequestOccurred, sMainNAM, &QgsNetworkAccessManager::handleAuthRequest );
675 connect(
this, &QNetworkAccessManager::finished,
this, &QgsNetworkAccessManager::onReplyFinished );
680 QStringList excludes;
681 QStringList noProxyURLs;
683 const bool proxyEnabled = settings.
value( QStringLiteral(
"proxy/proxyEnabled" ),
false ).toBool();
688 excludes = settings.
value( QStringLiteral(
"proxy/proxyExcludedUrls" ), QStringList() ).toStringList();
690 noProxyURLs = settings.
value( QStringLiteral(
"proxy/noProxyUrls" ), QStringList() ).toStringList();
693 const QString proxyHost = settings.
value( QStringLiteral(
"proxy/proxyHost" ),
"" ).toString();
694 const int proxyPort = settings.
value( QStringLiteral(
"proxy/proxyPort" ),
"" ).toString().toInt();
696 const QString proxyUser = settings.
value( QStringLiteral(
"proxy/proxyUser" ),
"" ).toString();
697 const QString proxyPassword = settings.
value( QStringLiteral(
"proxy/proxyPassword" ),
"" ).toString();
699 const QString proxyTypeString = settings.
value( QStringLiteral(
"proxy/proxyType" ),
"" ).toString();
701 if ( proxyTypeString == QLatin1String(
"DefaultProxy" ) )
703 mUseSystemProxy =
true;
704 QNetworkProxyFactory::setUseSystemConfiguration(
true );
705 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
706 if ( !proxies.isEmpty() )
708 proxy = proxies.first();
714 QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
715 if ( proxyTypeString == QLatin1String(
"Socks5Proxy" ) )
717 proxyType = QNetworkProxy::Socks5Proxy;
719 else if ( proxyTypeString == QLatin1String(
"HttpProxy" ) )
721 proxyType = QNetworkProxy::HttpProxy;
723 else if ( proxyTypeString == QLatin1String(
"HttpCachingProxy" ) )
725 proxyType = QNetworkProxy::HttpCachingProxy;
727 else if ( proxyTypeString == QLatin1String(
"FtpCachingProxy" ) )
729 proxyType = QNetworkProxy::FtpCachingProxy;
733 .arg( proxyHost ).arg( proxyPort )
734 .arg( proxyUser, proxyPassword ), 2
736 proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
739 const QString authcfg = settings.
value( QStringLiteral(
"proxy/authcfg" ),
"" ).toString();
740 if ( !authcfg.isEmpty( ) )
742 QgsDebugMsgLevel( QStringLiteral(
"setting proxy from stored authentication configuration %1" ).arg( authcfg ), 2 );
745 authManager->updateNetworkProxy( proxy, authcfg );
756 if ( cacheDirectory.isEmpty() )
757 cacheDirectory = QStandardPaths::writableLocation( QStandardPaths::CacheLocation );
765 if ( cache() != newcache )
766 setCache( newcache );
768 if (
this != sMainNAM )
770 static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies(
static_cast<QgsNetworkCookieJar *
>( sMainNAM->cookieJar() )->allCookies() );
774void QgsNetworkAccessManager::syncCookies(
const QList<QNetworkCookie> &cookies )
776 if ( sender() !=
this )
778 static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies( cookies );
779 if (
this == sMainNAM )
800 br.
get( request, forceRefresh, feedback );
808 br.
post( request, data, forceRefresh, feedback );
814 QString
id = QUuid::createUuid().toString();
815 sCustomPreprocessors.emplace_back( std::make_pair(
id, processor ) );
821 const size_t prevCount = sCustomPreprocessors.size();
822 sCustomPreprocessors.erase( std::remove_if( sCustomPreprocessors.begin(), sCustomPreprocessors.end(), [
id]( std::pair< QString, std::function<
void( QNetworkRequest * ) > > &a )
824 return a.first == id;
825 } ), sCustomPreprocessors.end() );
826 return prevCount != sCustomPreprocessors.size();
831 QString
id = QUuid::createUuid().toString();
832 sCustomReplyPreprocessors.emplace_back( std::make_pair(
id, processor ) );
838 const size_t prevCount = sCustomReplyPreprocessors.size();
839 sCustomReplyPreprocessors.erase( std::remove_if( sCustomReplyPreprocessors.begin(), sCustomReplyPreprocessors.end(), [
id]( std::pair< QString, std::function<
void(
const QNetworkRequest &, QNetworkReply * ) > > &a )
841 return a.first == id;
842 } ), sCustomReplyPreprocessors.end() );
843 return prevCount != sCustomReplyPreprocessors.size();
848 for (
const auto &preprocessor : sCustomPreprocessors )
850 preprocessor.second( req );
860 : mOperation( operation )
861 , mRequest( request )
862 , mOriginatingThreadId( QStringLiteral(
"0x%2" ).arg( reinterpret_cast<quintptr>( QThread::currentThread() ), 2 * QT_POINTER_SIZE, 16, QLatin1Char(
'0' ) ) )
863 , mRequestId( requestId )
864 , mContent( content )
865 , mInitiatorClass( request.attribute( static_cast< QNetworkRequest::Attribute >(
QgsNetworkRequestParameters::AttributeInitiatorClass ) ).toString() )
866 , mInitiatorRequestId( request.attribute( static_cast< QNetworkRequest::Attribute >(
QgsNetworkRequestParameters::AttributeInitiatorRequestId ) ) )
878 QgsDebugError( QStringLiteral(
"SSL errors occurred accessing URL:\n%1" ).arg( reply->request().url().toString() ) );
888 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() ) );
894 QgsDebugError( QStringLiteral(
"Network authentication required external browser to open URL %1, but no handler was in place" ).arg( url.toString() ) );
899 QgsDebugError( QStringLiteral(
"Network authentication required external browser closed, but no handler was in place" ) );
903#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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
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)