37#include <QAuthenticator>
39#include <QNetworkReply>
40#include <QRecursiveMutex>
41#include <QStandardPaths>
42#include <QThreadStorage>
47#include "moc_qgsnetworkaccessmanager.cpp"
52#include <QSslConfiguration>
60static std::vector< std::pair< QString, std::function< void( QNetworkRequest * ) > > > sCustomPreprocessors;
61static std::vector< std::pair< QString, std::function< void( QNetworkRequest *,
int &op, QByteArray *data ) > > > sCustomAdvancedPreprocessors;
62static std::vector< std::pair< QString, std::function< void(
const QNetworkRequest &, QNetworkReply * ) > > > sCustomReplyPreprocessors;
65class QgsNetworkProxyFactory :
public QNetworkProxyFactory
68 QgsNetworkProxyFactory() =
default;
70 QList<QNetworkProxy> queryProxy(
const QNetworkProxyQuery &query = QNetworkProxyQuery() )
override
76 for ( QNetworkProxyFactory *f : constProxyFactories )
78 QList<QNetworkProxy> systemproxies = QNetworkProxyFactory::systemProxyForQuery( query );
79 if ( !systemproxies.isEmpty() )
82 QList<QNetworkProxy> proxies = f->queryProxy( query );
83 if ( !proxies.isEmpty() )
88 if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
91 const QString url = query.url().toString();
94 for (
const QString &noProxy : constNoProxyList )
96 if ( !noProxy.trimmed().isEmpty() && url.startsWith( noProxy ) )
98 QgsDebugMsgLevel( u
"don't using any proxy for %1 [exclude %2]"_s.arg( url, noProxy ), 4 );
99 return QList<QNetworkProxy>() << QNetworkProxy( QNetworkProxy::NoProxy );
104 for (
const QString &exclude : constExcludeList )
106 if ( !exclude.trimmed().isEmpty() && url.startsWith( exclude ) )
108 QgsDebugMsgLevel( u
"using default proxy for %1 [exclude %2]"_s.arg( url, exclude ), 4 );
109 return QList<QNetworkProxy>() << QNetworkProxy( QNetworkProxy::DefaultProxy );
115 QgsDebugMsgLevel( u
"requesting system proxy for query %1"_s.arg( url ), 4 );
116 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery( query );
117 if ( !proxies.isEmpty() )
120 .arg( proxies.first().hostName() ).arg( proxies.first().port() ), 4 );
132class QgsNetworkCookieJar :
public QNetworkCookieJar
137 QgsNetworkCookieJar( QgsNetworkAccessManager *parent )
138 : QNetworkCookieJar( parent )
142 bool deleteCookie(
const QNetworkCookie &cookie )
override
144 const QMutexLocker locker( &mMutex );
145 if ( QNetworkCookieJar::deleteCookie( cookie ) )
147 emit mNam->cookiesChanged( allCookies() );
152 bool insertCookie(
const QNetworkCookie &cookie )
override
154 const QMutexLocker locker( &mMutex );
155 if ( QNetworkCookieJar::insertCookie( cookie ) )
157 emit mNam->cookiesChanged( allCookies() );
162 bool setCookiesFromUrl(
const QList<QNetworkCookie> &cookieList,
const QUrl &url )
override
164 const QMutexLocker locker( &mMutex );
165 return QNetworkCookieJar::setCookiesFromUrl( cookieList, url );
167 bool updateCookie(
const QNetworkCookie &cookie )
override
169 const QMutexLocker locker( &mMutex );
170 if ( QNetworkCookieJar::updateCookie( cookie ) )
172 emit mNam->cookiesChanged( allCookies() );
179 QList<QNetworkCookie> allCookies()
const
181 const QMutexLocker locker( &mMutex );
182 return QNetworkCookieJar::allCookies();
184 void setAllCookies(
const QList<QNetworkCookie> &cookieList )
186 const QMutexLocker locker( &mMutex );
187 QNetworkCookieJar::setAllCookies( cookieList );
190 QgsNetworkAccessManager *mNam =
nullptr;
191 mutable QRecursiveMutex mMutex;
201 static QThreadStorage<QgsNetworkAccessManager> sInstances;
204 if ( nam->thread() == qApp->thread() )
207 if ( !nam->mInitialized )
217 : QNetworkAccessManager( parent )
218 , mSslErrorHandlerSemaphore( 1 )
219 , mAuthRequestHandlerSemaphore( 1 )
221 setRedirectPolicy( QNetworkRequest::NoLessSafeRedirectPolicy );
222 setProxyFactory(
new QgsNetworkProxyFactory() );
223 setCookieJar(
new QgsNetworkCookieJar(
this ) );
224 enableStrictTransportSecurityStore(
true );
225 setStrictTransportSecurityEnabled(
true );
230 Q_ASSERT( sMainNAM ==
this );
231 mSslErrorHandler = std::move( handler );
236 Q_ASSERT( sMainNAM ==
this );
237 mAuthHandler = std::move( handler );
242 mProxyFactories.insert( 0, factory );
247 mProxyFactories.removeAll( factory );
252 return mProxyFactories;
257 return mExcludedURLs;
267 return mFallbackProxy;
272 QgsDebugMsgLevel( u
"proxy settings: (type:%1 host: %2:%3, user:%4, password:%5"_s
273 .arg( proxy.type() == QNetworkProxy::DefaultProxy ? u
"DefaultProxy"_s :
274 proxy.type() == QNetworkProxy::Socks5Proxy ? u
"Socks5Proxy"_s :
275 proxy.type() == QNetworkProxy::NoProxy ? u
"NoProxy"_s :
276 proxy.type() == QNetworkProxy::HttpProxy ? u
"HttpProxy"_s :
277 proxy.type() == QNetworkProxy::HttpCachingProxy ? u
"HttpCachingProxy"_s :
278 proxy.type() == QNetworkProxy::FtpCachingProxy ? u
"FtpCachingProxy"_s :
283 proxy.password().isEmpty() ? u
"not set"_s : u
"set"_s ), 4 );
285 mFallbackProxy = proxy;
286 mExcludedURLs = excludes;
288 mExcludedURLs.erase( std::remove_if( mExcludedURLs.begin(), mExcludedURLs.end(),
289 [](
const QString & url )
291 return url.trimmed().isEmpty();
292 } ), mExcludedURLs.end() );
294 mNoProxyURLs = noProxyURLs;
295 mNoProxyURLs.erase( std::remove_if( mNoProxyURLs.begin(), mNoProxyURLs.end(),
296 [](
const QString & url )
298 return url.trimmed().isEmpty();
299 } ), mNoProxyURLs.end() );
306 QNetworkRequest *pReq(
const_cast< QNetworkRequest *
>( &req ) );
308 QString userAgent = s.
value( u
"/qgis/networkAndProxy/userAgent"_s,
"Mozilla/5.0" ).toString();
309 if ( !userAgent.isEmpty() )
311 userAgent += u
"QGIS/%1/%2"_s.arg(
Qgis::versionInt() ).arg( QSysInfo::prettyProductName() );
312 pReq->setRawHeader(
"User-Agent", userAgent.toLatin1() );
315 const bool ishttps = pReq->url().scheme().compare(
"https"_L1, Qt::CaseInsensitive ) == 0;
319 QSslConfiguration sslconfig( pReq->sslConfiguration() );
321 sslconfig.setCaCertificates( QgsAuthCertUtils::casMerge(
QgsApplication::authManager()->trustedCaCertsCache(), sslconfig.caCertificates( ) ) );
323 const QString hostport( u
"%1:%2"_s
324 .arg( pReq->url().host().trimmed() )
325 .arg( pReq->url().port() != -1 ? pReq->url().port() : 443 ) );
327 if ( !servconfig.
isNull() )
329 QgsDebugMsgLevel( u
"Adding SSL custom config to request for %1"_s.arg( hostport ), 2 );
335 pReq->setSslConfiguration( sslconfig );
339 if ( sMainNAM->mCacheDisabled )
342 pReq->setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
343 pReq->setAttribute( QNetworkRequest::CacheSaveControlAttribute,
false );
346 for (
const auto &preprocessor : sCustomPreprocessors )
348 preprocessor.second( pReq );
351 static QAtomicInt sRequestId = 0;
352 const int requestId = ++sRequestId;
354 if ( QBuffer *buffer = qobject_cast<QBuffer *>( outgoingData ) )
356 content = buffer->buffer();
359 for (
const auto &preprocessor : sCustomAdvancedPreprocessors )
361 int intOp =
static_cast< int >( op );
362 preprocessor.second( pReq, intOp, &content );
363 op =
static_cast< QNetworkAccessManager::Operation
>( intOp );
370 QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
371 reply->setProperty(
"requestId", requestId );
378 connect( reply, &QNetworkReply::downloadProgress,
this, &QgsNetworkAccessManager::onReplyDownloadProgress );
380 connect( reply, &QNetworkReply::sslErrors,
this, &QgsNetworkAccessManager::onReplySslErrors );
383 for (
const auto &replyPreprocessor : sCustomReplyPreprocessors )
385 replyPreprocessor.second( req, reply );
393 QTimer *timer =
new QTimer( reply );
394 timer->setObjectName( u
"timeoutTimer"_s );
395 connect( timer, &QTimer::timeout,
this, &QgsNetworkAccessManager::abortRequest );
396 timer->setSingleShot(
true );
399 connect( reply, &QNetworkReply::downloadProgress, timer, [timer] { timer->start(); } );
400 connect( reply, &QNetworkReply::uploadProgress, timer, [timer] { timer->start(); } );
401 connect( reply, &QNetworkReply::finished, timer, &QTimer::stop );
403 QgsDebugMsgLevel( u
"Created [reply:%1]"_s.arg(
reinterpret_cast< qint64
>( reply ), 0, 16 ), 3 );
408void QgsNetworkAccessManager::abortRequest()
410 QTimer *timer = qobject_cast<QTimer *>( sender() );
413 QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent() );
417 QgsDebugMsgLevel( u
"Abort [reply:%1] %2"_s.arg(
reinterpret_cast< qint64
>( reply ), 0, 16 ).arg( reply->url().toString() ), 3 );
424void QgsNetworkAccessManager::onReplyFinished( QNetworkReply *reply )
426 emit
finished( QgsNetworkReplyContent( reply ) );
429void QgsNetworkAccessManager::onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
431 if ( QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() ) )
437int QgsNetworkAccessManager::getRequestId( QNetworkReply *reply )
439 return reply->property(
"requestId" ).toInt();
442void QgsNetworkAccessManager::pauseTimeout( QNetworkReply *reply )
444 Q_ASSERT( reply->manager() ==
this );
446 QTimer *timer = reply->findChild<QTimer *>( u
"timeoutTimer"_s );
447 if ( timer && timer->isActive() )
453void QgsNetworkAccessManager::restartTimeout( QNetworkReply *reply )
455 Q_ASSERT( reply->manager() ==
this );
457 QTimer *timer = reply->findChild<QTimer *>( u
"timeoutTimer"_s );
460 Q_ASSERT( !timer->isActive() );
462 timer->setSingleShot(
true );
467void QgsNetworkAccessManager::afterAuthRequestHandled( QNetworkReply *reply )
469 if ( reply->manager() ==
this )
471 restartTimeout( reply );
472 emit authRequestHandled( reply );
477void QgsNetworkAccessManager::onReplySslErrors(
const QList<QSslError> &errors )
479 QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() );
481 Q_ASSERT( reply->manager() ==
this );
483 QgsDebugMsgLevel( u
"Stopping network reply timeout whilst SSL error is handled"_s, 2 );
484 pauseTimeout( reply );
489 mSslErrorHandlerSemaphore.acquire();
493 emit sslErrorsOccurred( reply, errors );
494 if (
this != sMainNAM )
498 mSslErrorHandlerSemaphore.acquire();
499 mSslErrorHandlerSemaphore.release();
500 afterSslErrorHandled( reply );
504void QgsNetworkAccessManager::afterSslErrorHandled( QNetworkReply *reply )
506 if ( reply->manager() ==
this )
508 restartTimeout( reply );
509 emit sslErrorsHandled( reply );
513void QgsNetworkAccessManager::handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
515 mSslErrorHandler->handleSslErrors( reply, errors );
516 afterSslErrorHandled( reply );
517 qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mSslErrorHandlerSemaphore.release();
522void QgsNetworkAccessManager::onAuthRequired( QNetworkReply *reply, QAuthenticator *auth )
525 Q_ASSERT( reply->manager() ==
this );
527 QgsDebugMsgLevel( u
"Stopping network reply timeout whilst auth request is handled"_s, 2 );
528 pauseTimeout( reply );
533 mAuthRequestHandlerSemaphore.acquire();
537 emit authRequestOccurred( reply, auth );
539 if (
this != sMainNAM )
543 mAuthRequestHandlerSemaphore.acquire();
544 mAuthRequestHandlerSemaphore.release();
545 afterAuthRequestHandled( reply );
551 if (
this != sMainNAM )
553 sMainNAM->requestAuthOpenBrowser( url );
557 mAuthHandler->handleAuthRequestOpenBrowser( url );
562 if (
this != sMainNAM )
564 sMainNAM->requestAuthCloseBrowser();
568 mAuthHandler->handleAuthRequestCloseBrowser();
573 if (
this != sMainNAM )
580void QgsNetworkAccessManager::handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth )
582 mAuthHandler->handleAuthRequest( reply, auth );
586 afterAuthRequestHandled( reply );
587 qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mAuthRequestHandlerSemaphore.release();
594 case QNetworkRequest::AlwaysNetwork:
595 return u
"AlwaysNetwork"_s;
596 case QNetworkRequest::PreferNetwork:
597 return u
"PreferNetwork"_s;
598 case QNetworkRequest::PreferCache:
599 return u
"PreferCache"_s;
600 case QNetworkRequest::AlwaysCache:
601 return u
"AlwaysCache"_s;
603 return u
"PreferNetwork"_s;
608 if ( name ==
"AlwaysNetwork"_L1 )
610 return QNetworkRequest::AlwaysNetwork;
612 else if ( name ==
"PreferNetwork"_L1 )
614 return QNetworkRequest::PreferNetwork;
616 else if ( name ==
"PreferCache"_L1 )
618 return QNetworkRequest::PreferCache;
620 else if ( name ==
"AlwaysCache"_L1 )
622 return QNetworkRequest::AlwaysCache;
624 return QNetworkRequest::PreferNetwork;
630 mUseSystemProxy =
false;
632 Q_ASSERT( sMainNAM );
634 if ( sMainNAM !=
this )
636 connect(
this, &QNetworkAccessManager::proxyAuthenticationRequired,
637 sMainNAM, &QNetworkAccessManager::proxyAuthenticationRequired,
658 connect(
this, &QNetworkAccessManager::sslErrors,
659 sMainNAM, &QNetworkAccessManager::sslErrors,
672 if ( !mSslErrorHandler )
676 setAuthHandler( std::make_unique< QgsNetworkAuthenticationHandler>() );
679 connect(
this, &QgsNetworkAccessManager::sslErrorsOccurred, sMainNAM, &QgsNetworkAccessManager::handleSslErrors );
681 connect(
this, &QNetworkAccessManager::authenticationRequired,
this, &QgsNetworkAccessManager::onAuthRequired );
682 connect(
this, &QgsNetworkAccessManager::authRequestOccurred, sMainNAM, &QgsNetworkAccessManager::handleAuthRequest );
684 connect(
this, &QNetworkAccessManager::finished,
this, &QgsNetworkAccessManager::onReplyFinished );
689 QStringList excludes;
690 QStringList noProxyURLs;
692 const bool proxyEnabled = settings.
value( u
"proxy/proxyEnabled"_s,
false ).toBool();
697 excludes = settings.
value( u
"proxy/proxyExcludedUrls"_s, QStringList() ).toStringList();
699 noProxyURLs = settings.
value( u
"proxy/noProxyUrls"_s, QStringList() ).toStringList();
702 const QString proxyHost = settings.
value( u
"proxy/proxyHost"_s,
"" ).toString();
703 const int proxyPort = settings.
value( u
"proxy/proxyPort"_s,
"" ).toString().toInt();
705 const QString proxyUser = settings.
value( u
"proxy/proxyUser"_s,
"" ).toString();
706 const QString proxyPassword = settings.
value( u
"proxy/proxyPassword"_s,
"" ).toString();
708 const QString proxyTypeString = settings.
value( u
"proxy/proxyType"_s,
"" ).toString();
710 if ( proxyTypeString ==
"DefaultProxy"_L1 )
712 mUseSystemProxy =
true;
713 QNetworkProxyFactory::setUseSystemConfiguration(
true );
714 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
715 if ( !proxies.isEmpty() )
717 proxy = proxies.first();
723 QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
724 if ( proxyTypeString ==
"Socks5Proxy"_L1 )
726 proxyType = QNetworkProxy::Socks5Proxy;
728 else if ( proxyTypeString ==
"HttpProxy"_L1 )
730 proxyType = QNetworkProxy::HttpProxy;
732 else if ( proxyTypeString ==
"HttpCachingProxy"_L1 )
734 proxyType = QNetworkProxy::HttpCachingProxy;
736 else if ( proxyTypeString ==
"FtpCachingProxy"_L1 )
738 proxyType = QNetworkProxy::FtpCachingProxy;
742 .arg( proxyHost ).arg( proxyPort )
743 .arg( proxyUser, proxyPassword ), 2
745 proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
748 const QString authcfg = settings.
value( u
"proxy/authcfg"_s,
"" ).toString();
749 if ( !authcfg.isEmpty( ) )
752 QgsDebugMsgLevel( u
"setting proxy from stored authentication configuration %1"_s.arg( authcfg ), 2 );
755 authManager->updateNetworkProxy( proxy, authcfg );
757 QgsDebugError( u
"Auth manager is not available - cannot update network proxy for authcfg: %1"_s.arg( authcfg ) );
769 if ( cacheDirectory.isEmpty() )
770 cacheDirectory = QStandardPaths::writableLocation( QStandardPaths::CacheLocation );
778 if ( cache() != newcache )
779 setCache( newcache );
781 if (
this != sMainNAM )
783 static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies(
static_cast<QgsNetworkCookieJar *
>( sMainNAM->cookieJar() )->allCookies() );
787void QgsNetworkAccessManager::syncCookies(
const QList<QNetworkCookie> &cookies )
789 if ( sender() !=
this )
791 static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies( cookies );
792 if (
this == sMainNAM )
813 br.
get( request, forceRefresh, feedback );
821 ( void )br.
post( request, data, forceRefresh, feedback );
827 QString
id = QUuid::createUuid().toString();
828 sCustomPreprocessors.emplace_back( std::make_pair(
id, processor ) );
834 const size_t prevCount = sCustomPreprocessors.size();
835 sCustomPreprocessors.erase( std::remove_if( sCustomPreprocessors.begin(), sCustomPreprocessors.end(), [
id]( std::pair< QString, std::function<
void( QNetworkRequest * ) > > &a )
837 return a.first == id;
838 } ), sCustomPreprocessors.end() );
839 return prevCount != sCustomPreprocessors.size();
844 const size_t prevCount = sCustomAdvancedPreprocessors.size();
845 sCustomAdvancedPreprocessors.erase( std::remove_if( sCustomAdvancedPreprocessors.begin(), sCustomAdvancedPreprocessors.end(), [
id]( std::pair< QString, std::function<
void( QNetworkRequest *,
int &, QByteArray * ) > > &a )
847 return a.first == id;
848 } ), sCustomAdvancedPreprocessors.end() );
849 return prevCount != sCustomAdvancedPreprocessors.size();
854 QString
id = QUuid::createUuid().toString();
855 sCustomAdvancedPreprocessors.emplace_back( std::make_pair(
id, processor ) );
861 QString
id = QUuid::createUuid().toString();
862 sCustomReplyPreprocessors.emplace_back( std::make_pair(
id, processor ) );
868 const size_t prevCount = sCustomReplyPreprocessors.size();
869 sCustomReplyPreprocessors.erase( std::remove_if( sCustomReplyPreprocessors.begin(), sCustomReplyPreprocessors.end(), [
id]( std::pair< QString, std::function<
void(
const QNetworkRequest &, QNetworkReply * ) > > &a )
871 return a.first == id;
872 } ), sCustomReplyPreprocessors.end() );
873 return prevCount != sCustomReplyPreprocessors.size();
878 for (
const auto &preprocessor : sCustomPreprocessors )
880 preprocessor.second( req );
892 , mOriginatingThreadId( u
"0x%2"_s.arg( reinterpret_cast<quintptr>( QThread::currentThread() ), 2 * QT_POINTER_SIZE, 16,
'0'_L1 ) )
908 QgsDebugError( u
"SSL errors occurred accessing URL:\n%1"_s.arg( reply->request().url().toString() ) );
918 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() ) );
924 QgsDebugError( u
"Network authentication required external browser to open URL %1, but no handler was in place"_s.arg( url.toString() ) );
929 QgsDebugError( u
"Network authentication required external browser closed, but no handler was in place"_s );
933#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)