37#include <QAuthenticator>
39#include <QNetworkReply>
40#include <QRecursiveMutex>
41#include <QStandardPaths>
43#include <QThreadStorage>
48#include "moc_qgsnetworkaccessmanager.cpp"
50using namespace Qt::StringLiterals;
55#include <QSslConfiguration>
63static std::vector< std::pair< QString, std::function< void( QNetworkRequest * ) > > > sCustomPreprocessors;
64static std::vector< std::pair< QString, std::function< void( QNetworkRequest *,
int &op, QByteArray *data ) > > > sCustomAdvancedPreprocessors;
65static std::vector< std::pair< QString, std::function< void(
const QNetworkRequest &, QNetworkReply * ) > > > sCustomReplyPreprocessors;
68class QgsNetworkProxyFactory :
public QNetworkProxyFactory
71 QgsNetworkProxyFactory() =
default;
73 QList<QNetworkProxy> queryProxy(
const QNetworkProxyQuery &query = QNetworkProxyQuery() )
override
79 for ( QNetworkProxyFactory *f : constProxyFactories )
81 QList<QNetworkProxy> systemproxies = QNetworkProxyFactory::systemProxyForQuery( query );
82 if ( !systemproxies.isEmpty() )
85 QList<QNetworkProxy> proxies = f->queryProxy( query );
86 if ( !proxies.isEmpty() )
91 if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
94 const QString url = query.url().toString();
97 for (
const QString &noProxy : constNoProxyList )
99 if ( !noProxy.trimmed().isEmpty() && url.startsWith( noProxy ) )
101 QgsDebugMsgLevel( u
"don't using any proxy for %1 [exclude %2]"_s.arg( url, noProxy ), 4 );
102 return QList<QNetworkProxy>() << QNetworkProxy( QNetworkProxy::NoProxy );
107 for (
const QString &exclude : constExcludeList )
109 if ( !exclude.trimmed().isEmpty() && url.startsWith( exclude ) )
111 QgsDebugMsgLevel( u
"using default proxy for %1 [exclude %2]"_s.arg( url, exclude ), 4 );
112 return QList<QNetworkProxy>() << QNetworkProxy( QNetworkProxy::DefaultProxy );
118 QgsDebugMsgLevel( u
"requesting system proxy for query %1"_s.arg( url ), 4 );
119 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery( query );
120 if ( !proxies.isEmpty() )
123 .arg( proxies.first().hostName() ).arg( proxies.first().port() ), 4 );
135class QgsNetworkCookieJar :
public QNetworkCookieJar
140 QgsNetworkCookieJar( QgsNetworkAccessManager *parent )
141 : QNetworkCookieJar( parent )
145 bool deleteCookie(
const QNetworkCookie &cookie )
override
147 const QMutexLocker locker( &mMutex );
148 if ( QNetworkCookieJar::deleteCookie( cookie ) )
150 emit mNam->cookiesChanged( allCookies() );
155 bool insertCookie(
const QNetworkCookie &cookie )
override
157 const QMutexLocker locker( &mMutex );
158 if ( QNetworkCookieJar::insertCookie( cookie ) )
160 emit mNam->cookiesChanged( allCookies() );
165 bool setCookiesFromUrl(
const QList<QNetworkCookie> &cookieList,
const QUrl &url )
override
167 const QMutexLocker locker( &mMutex );
168 return QNetworkCookieJar::setCookiesFromUrl( cookieList, url );
170 bool updateCookie(
const QNetworkCookie &cookie )
override
172 const QMutexLocker locker( &mMutex );
173 if ( QNetworkCookieJar::updateCookie( cookie ) )
175 emit mNam->cookiesChanged( allCookies() );
182 QList<QNetworkCookie> allCookies()
const
184 const QMutexLocker locker( &mMutex );
185 return QNetworkCookieJar::allCookies();
187 void setAllCookies(
const QList<QNetworkCookie> &cookieList )
189 const QMutexLocker locker( &mMutex );
190 QNetworkCookieJar::setAllCookies( cookieList );
193 QgsNetworkAccessManager *mNam =
nullptr;
194 mutable QRecursiveMutex mMutex;
204 static QThreadStorage<QgsNetworkAccessManager> sInstances;
207 if ( nam->thread() == qApp->thread() )
210 if ( !nam->mInitialized )
220 : QNetworkAccessManager( parent )
221 , mSslErrorHandlerSemaphore( 1 )
222 , mAuthRequestHandlerSemaphore( 1 )
224 setRedirectPolicy( QNetworkRequest::NoLessSafeRedirectPolicy );
225 setProxyFactory(
new QgsNetworkProxyFactory() );
226 setCookieJar(
new QgsNetworkCookieJar(
this ) );
227 enableStrictTransportSecurityStore(
true );
228 setStrictTransportSecurityEnabled(
true );
233 Q_ASSERT( sMainNAM ==
this );
234 mSslErrorHandler = std::move( handler );
239 Q_ASSERT( sMainNAM ==
this );
240 mAuthHandler = std::move( handler );
245 mProxyFactories.insert( 0, factory );
250 mProxyFactories.removeAll( factory );
255 return mProxyFactories;
260 return mExcludedURLs;
270 return mFallbackProxy;
275 QgsDebugMsgLevel( u
"proxy settings: (type:%1 host: %2:%3, user:%4, password:%5"_s
276 .arg( proxy.type() == QNetworkProxy::DefaultProxy ? u
"DefaultProxy"_s :
277 proxy.type() == QNetworkProxy::Socks5Proxy ? u
"Socks5Proxy"_s :
278 proxy.type() == QNetworkProxy::NoProxy ? u
"NoProxy"_s :
279 proxy.type() == QNetworkProxy::HttpProxy ? u
"HttpProxy"_s :
280 proxy.type() == QNetworkProxy::HttpCachingProxy ? u
"HttpCachingProxy"_s :
281 proxy.type() == QNetworkProxy::FtpCachingProxy ? u
"FtpCachingProxy"_s :
286 proxy.password().isEmpty() ? u
"not set"_s : u
"set"_s ), 4 );
288 mFallbackProxy = proxy;
289 mExcludedURLs = excludes;
291 mExcludedURLs.erase( std::remove_if( mExcludedURLs.begin(), mExcludedURLs.end(),
292 [](
const QString & url )
294 return url.trimmed().isEmpty();
295 } ), mExcludedURLs.end() );
297 mNoProxyURLs = noProxyURLs;
298 mNoProxyURLs.erase( std::remove_if( mNoProxyURLs.begin(), mNoProxyURLs.end(),
299 [](
const QString & url )
301 return url.trimmed().isEmpty();
302 } ), mNoProxyURLs.end() );
309 QNetworkRequest *pReq(
const_cast< QNetworkRequest *
>( &req ) );
311 QString userAgent = s.
value( u
"/qgis/networkAndProxy/userAgent"_s,
"Mozilla/5.0" ).toString();
312 if ( !userAgent.isEmpty() )
314 userAgent += u
"QGIS/%1/%2"_s.arg(
Qgis::versionInt() ).arg( QSysInfo::prettyProductName() );
315 pReq->setRawHeader(
"User-Agent", userAgent.toLatin1() );
318 const bool ishttps = pReq->url().scheme().compare(
"https"_L1, Qt::CaseInsensitive ) == 0;
322 QSslConfiguration sslconfig( pReq->sslConfiguration() );
324 sslconfig.setCaCertificates( QgsAuthCertUtils::casMerge(
QgsApplication::authManager()->trustedCaCertsCache(), sslconfig.caCertificates( ) ) );
326 const QString hostport( u
"%1:%2"_s
327 .arg( pReq->url().host().trimmed() )
328 .arg( pReq->url().port() != -1 ? pReq->url().port() : 443 ) );
330 if ( !servconfig.
isNull() )
332 QgsDebugMsgLevel( u
"Adding SSL custom config to request for %1"_s.arg( hostport ), 2 );
338 pReq->setSslConfiguration( sslconfig );
342 if ( sMainNAM->mCacheDisabled )
345 pReq->setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
346 pReq->setAttribute( QNetworkRequest::CacheSaveControlAttribute,
false );
349 for (
const auto &preprocessor : sCustomPreprocessors )
351 preprocessor.second( pReq );
354 static QAtomicInt sRequestId = 0;
355 const int requestId = ++sRequestId;
357 if ( QBuffer *buffer = qobject_cast<QBuffer *>( outgoingData ) )
359 content = buffer->buffer();
362 for (
const auto &preprocessor : sCustomAdvancedPreprocessors )
364 int intOp =
static_cast< int >( op );
365 preprocessor.second( pReq, intOp, &content );
366 op =
static_cast< QNetworkAccessManager::Operation
>( intOp );
373 QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
374 reply->setProperty(
"requestId", requestId );
381 connect( reply, &QNetworkReply::downloadProgress,
this, &QgsNetworkAccessManager::onReplyDownloadProgress );
383 connect( reply, &QNetworkReply::sslErrors,
this, &QgsNetworkAccessManager::onReplySslErrors );
386 for (
const auto &replyPreprocessor : sCustomReplyPreprocessors )
388 replyPreprocessor.second( req, reply );
396 QTimer *timer =
new QTimer( reply );
397 timer->setObjectName( u
"timeoutTimer"_s );
398 connect( timer, &QTimer::timeout,
this, &QgsNetworkAccessManager::abortRequest );
399 timer->setSingleShot(
true );
402 connect( reply, &QNetworkReply::downloadProgress, timer, [timer] { timer->start(); } );
403 connect( reply, &QNetworkReply::uploadProgress, timer, [timer] { timer->start(); } );
404 connect( reply, &QNetworkReply::finished, timer, &QTimer::stop );
406 QgsDebugMsgLevel( u
"Created [reply:%1]"_s.arg(
reinterpret_cast< qint64
>( reply ), 0, 16 ), 3 );
411void QgsNetworkAccessManager::abortRequest()
413 QTimer *timer = qobject_cast<QTimer *>( sender() );
416 QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent() );
420 QgsDebugMsgLevel( u
"Abort [reply:%1] %2"_s.arg(
reinterpret_cast< qint64
>( reply ), 0, 16 ).arg( reply->url().toString() ), 3 );
427void QgsNetworkAccessManager::onReplyFinished( QNetworkReply *reply )
429 emit
finished( QgsNetworkReplyContent( reply ) );
432void QgsNetworkAccessManager::onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
434 if ( QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() ) )
440int QgsNetworkAccessManager::getRequestId( QNetworkReply *reply )
442 return reply->property(
"requestId" ).toInt();
445void QgsNetworkAccessManager::pauseTimeout( QNetworkReply *reply )
447 Q_ASSERT( reply->manager() ==
this );
449 QTimer *timer = reply->findChild<QTimer *>( u
"timeoutTimer"_s );
450 if ( timer && timer->isActive() )
456void QgsNetworkAccessManager::restartTimeout( QNetworkReply *reply )
458 Q_ASSERT( reply->manager() ==
this );
460 QTimer *timer = reply->findChild<QTimer *>( u
"timeoutTimer"_s );
463 Q_ASSERT( !timer->isActive() );
465 timer->setSingleShot(
true );
470void QgsNetworkAccessManager::afterAuthRequestHandled( QNetworkReply *reply )
472 if ( reply->manager() ==
this )
474 restartTimeout( reply );
475 emit authRequestHandled( reply );
480void QgsNetworkAccessManager::onReplySslErrors(
const QList<QSslError> &errors )
482 QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() );
484 Q_ASSERT( reply->manager() ==
this );
486 QgsDebugMsgLevel( u
"Stopping network reply timeout whilst SSL error is handled"_s, 2 );
487 pauseTimeout( reply );
492 mSslErrorHandlerSemaphore.acquire();
496 emit sslErrorsOccurred( reply, errors );
497 if (
this != sMainNAM )
501 mSslErrorHandlerSemaphore.acquire();
502 mSslErrorHandlerSemaphore.release();
503 afterSslErrorHandled( reply );
507void QgsNetworkAccessManager::afterSslErrorHandled( QNetworkReply *reply )
509 if ( reply->manager() ==
this )
511 restartTimeout( reply );
512 emit sslErrorsHandled( reply );
516void QgsNetworkAccessManager::handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
518 mSslErrorHandler->handleSslErrors( reply, errors );
519 afterSslErrorHandled( reply );
520 qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mSslErrorHandlerSemaphore.release();
525void QgsNetworkAccessManager::onAuthRequired( QNetworkReply *reply, QAuthenticator *auth )
528 Q_ASSERT( reply->manager() ==
this );
530 QgsDebugMsgLevel( u
"Stopping network reply timeout whilst auth request is handled"_s, 2 );
531 pauseTimeout( reply );
536 mAuthRequestHandlerSemaphore.acquire();
540 emit authRequestOccurred( reply, auth );
542 if (
this != sMainNAM )
546 mAuthRequestHandlerSemaphore.acquire();
547 mAuthRequestHandlerSemaphore.release();
548 afterAuthRequestHandled( reply );
554 if (
this != sMainNAM )
556 sMainNAM->requestAuthOpenBrowser( url );
560 mAuthHandler->handleAuthRequestOpenBrowser( url );
565 if (
this != sMainNAM )
567 sMainNAM->requestAuthCloseBrowser();
571 mAuthHandler->handleAuthRequestCloseBrowser();
576 if (
this != sMainNAM )
583void QgsNetworkAccessManager::handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth )
585 mAuthHandler->handleAuthRequest( reply, auth );
589 afterAuthRequestHandled( reply );
590 qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mAuthRequestHandlerSemaphore.release();
597 case QNetworkRequest::AlwaysNetwork:
598 return u
"AlwaysNetwork"_s;
599 case QNetworkRequest::PreferNetwork:
600 return u
"PreferNetwork"_s;
601 case QNetworkRequest::PreferCache:
602 return u
"PreferCache"_s;
603 case QNetworkRequest::AlwaysCache:
604 return u
"AlwaysCache"_s;
606 return u
"PreferNetwork"_s;
611 if ( name ==
"AlwaysNetwork"_L1 )
613 return QNetworkRequest::AlwaysNetwork;
615 else if ( name ==
"PreferNetwork"_L1 )
617 return QNetworkRequest::PreferNetwork;
619 else if ( name ==
"PreferCache"_L1 )
621 return QNetworkRequest::PreferCache;
623 else if ( name ==
"AlwaysCache"_L1 )
625 return QNetworkRequest::AlwaysCache;
627 return QNetworkRequest::PreferNetwork;
633 mUseSystemProxy =
false;
635 Q_ASSERT( sMainNAM );
637 if ( sMainNAM !=
this )
639 connect(
this, &QNetworkAccessManager::proxyAuthenticationRequired,
640 sMainNAM, &QNetworkAccessManager::proxyAuthenticationRequired,
661 connect(
this, &QNetworkAccessManager::sslErrors,
662 sMainNAM, &QNetworkAccessManager::sslErrors,
675 if ( !mSslErrorHandler )
679 setAuthHandler( std::make_unique< QgsNetworkAuthenticationHandler>() );
682 connect(
this, &QgsNetworkAccessManager::sslErrorsOccurred, sMainNAM, &QgsNetworkAccessManager::handleSslErrors );
684 connect(
this, &QNetworkAccessManager::authenticationRequired,
this, &QgsNetworkAccessManager::onAuthRequired );
685 connect(
this, &QgsNetworkAccessManager::authRequestOccurred, sMainNAM, &QgsNetworkAccessManager::handleAuthRequest );
687 connect(
this, &QNetworkAccessManager::finished,
this, &QgsNetworkAccessManager::onReplyFinished );
692 QStringList excludes;
693 QStringList noProxyURLs;
695 const bool proxyEnabled = settings.
value( u
"proxy/proxyEnabled"_s,
false ).toBool();
700 excludes = settings.
value( u
"proxy/proxyExcludedUrls"_s, QStringList() ).toStringList();
702 noProxyURLs = settings.
value( u
"proxy/noProxyUrls"_s, QStringList() ).toStringList();
705 const QString proxyHost = settings.
value( u
"proxy/proxyHost"_s,
"" ).toString();
706 const int proxyPort = settings.
value( u
"proxy/proxyPort"_s,
"" ).toString().toInt();
708 const QString proxyUser = settings.
value( u
"proxy/proxyUser"_s,
"" ).toString();
709 const QString proxyPassword = settings.
value( u
"proxy/proxyPassword"_s,
"" ).toString();
711 const QString proxyTypeString = settings.
value( u
"proxy/proxyType"_s,
"" ).toString();
713 if ( proxyTypeString ==
"DefaultProxy"_L1 )
715 mUseSystemProxy =
true;
716 QNetworkProxyFactory::setUseSystemConfiguration(
true );
717 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
718 if ( !proxies.isEmpty() )
720 proxy = proxies.first();
726 QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
727 if ( proxyTypeString ==
"Socks5Proxy"_L1 )
729 proxyType = QNetworkProxy::Socks5Proxy;
731 else if ( proxyTypeString ==
"HttpProxy"_L1 )
733 proxyType = QNetworkProxy::HttpProxy;
735 else if ( proxyTypeString ==
"HttpCachingProxy"_L1 )
737 proxyType = QNetworkProxy::HttpCachingProxy;
739 else if ( proxyTypeString ==
"FtpCachingProxy"_L1 )
741 proxyType = QNetworkProxy::FtpCachingProxy;
745 .arg( proxyHost ).arg( proxyPort )
746 .arg( proxyUser, proxyPassword ), 2
748 proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
751 const QString authcfg = settings.
value( u
"proxy/authcfg"_s,
"" ).toString();
752 if ( !authcfg.isEmpty( ) )
755 QgsDebugMsgLevel( u
"setting proxy from stored authentication configuration %1"_s.arg( authcfg ), 2 );
758 authManager->updateNetworkProxy( proxy, authcfg );
760 QgsDebugError( u
"Auth manager is not available - cannot update network proxy for authcfg: %1"_s.arg( authcfg ) );
772 if ( cacheDirectory.isEmpty() )
773 cacheDirectory = QStandardPaths::writableLocation( QStandardPaths::CacheLocation );
781 if ( cache() != newcache )
782 setCache( newcache );
784 if (
this != sMainNAM )
786 static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies(
static_cast<QgsNetworkCookieJar *
>( sMainNAM->cookieJar() )->allCookies() );
790void QgsNetworkAccessManager::syncCookies(
const QList<QNetworkCookie> &cookies )
792 if ( sender() !=
this )
794 static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies( cookies );
795 if (
this == sMainNAM )
816 br.
get( request, forceRefresh, feedback );
824 ( void )br.
post( request, data, forceRefresh, feedback );
830 QString
id = QUuid::createUuid().toString();
831 sCustomPreprocessors.emplace_back( std::make_pair(
id, processor ) );
837 const size_t prevCount = sCustomPreprocessors.size();
838 sCustomPreprocessors.erase( std::remove_if( sCustomPreprocessors.begin(), sCustomPreprocessors.end(), [
id]( std::pair< QString, std::function<
void( QNetworkRequest * ) > > &a )
840 return a.first == id;
841 } ), sCustomPreprocessors.end() );
842 return prevCount != sCustomPreprocessors.size();
847 const size_t prevCount = sCustomAdvancedPreprocessors.size();
848 sCustomAdvancedPreprocessors.erase( std::remove_if( sCustomAdvancedPreprocessors.begin(), sCustomAdvancedPreprocessors.end(), [
id]( std::pair< QString, std::function<
void( QNetworkRequest *,
int &, QByteArray * ) > > &a )
850 return a.first == id;
851 } ), sCustomAdvancedPreprocessors.end() );
852 return prevCount != sCustomAdvancedPreprocessors.size();
857 QString
id = QUuid::createUuid().toString();
858 sCustomAdvancedPreprocessors.emplace_back( std::make_pair(
id, processor ) );
864 QString
id = QUuid::createUuid().toString();
865 sCustomReplyPreprocessors.emplace_back( std::make_pair(
id, processor ) );
871 const size_t prevCount = sCustomReplyPreprocessors.size();
872 sCustomReplyPreprocessors.erase( std::remove_if( sCustomReplyPreprocessors.begin(), sCustomReplyPreprocessors.end(), [
id]( std::pair< QString, std::function<
void(
const QNetworkRequest &, QNetworkReply * ) > > &a )
874 return a.first == id;
875 } ), sCustomReplyPreprocessors.end() );
876 return prevCount != sCustomReplyPreprocessors.size();
881 for (
const auto &preprocessor : sCustomPreprocessors )
883 preprocessor.second( req );
895 , mOriginatingThreadId( u
"0x%2"_s.arg( reinterpret_cast<quintptr>( QThread::currentThread() ), 2 * QT_POINTER_SIZE, 16,
'0'_L1 ) )
911 QgsDebugError( u
"SSL errors occurred accessing URL:\n%1"_s.arg( reply->request().url().toString() ) );
921 QgsDebugError( u
"Network reply required authentication, but no handler was in place to provide this authentication request while accessing the URL:\n%1"_s.arg( reply->request().url().toString() ) );
927 QgsDebugError( u
"Network authentication required external browser to open URL %1, but no handler was in place"_s.arg( url.toString() ) );
932 QgsDebugError( u
"Network authentication required external browser closed, but no handler was in place"_s );
936#include "qgsnetworkaccessmanager.moc"
QFlags< NetworkRequestFlag > NetworkRequestFlags
Flags controlling behavior of network requests.
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.
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 which offers an interface to manage the authentication configuration database and to utiliz...
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).
QNetworkAccessManager with additional QGIS specific logic.
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.
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 QgsNetworkReplyContent blockingGet(QNetworkRequest &request, const QString &authCfg=QString(), bool forceRefresh=false, QgsFeedback *feedback=nullptr, Qgis::NetworkRequestFlags flags=Qgis::NetworkRequestFlags())
Posts a GET request to obtain the contents of the target request and returns a new QgsNetworkReplyCon...
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.
static bool removeAdvancedRequestPreprocessor(const QString &id)
Removes an advanced request pre-processor function with matching id.
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 QString setAdvancedRequestPreprocessor(const std::function< void(QNetworkRequest *, int &op, QByteArray *data)> &processor)
Sets an advanced request pre-processor function, which allows manipulation of a network request befor...
const QNetworkProxy & fallbackProxy() const
Returns the fallback proxy used by the manager.
static QgsNetworkReplyContent blockingPost(QNetworkRequest &request, const QByteArray &data, const QString &authCfg=QString(), bool forceRefresh=false, QgsFeedback *feedback=nullptr, Qgis::NetworkRequestFlags flags=Qgis::NetworkRequestFlags())
Posts a POST request to obtain the contents of the target request, using the given data,...
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 solely for internal u...
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.
int requestId() const
Returns a unique ID identifying the request.
@ AttributeInitiatorClass
Class name of original object which created the request.
@ AttributeInitiatorRequestId
Internal ID used by originator object to identify requests.
QgsNetworkRequestParameters()=default
QNetworkAccessManager::Operation operation() const
Returns the request operation, e.g.
QNetworkRequest request() const
Returns the network request.
QByteArray content() const
Returns the request's content.
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
Stores settings for use within QGIS.
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)