18 #ifndef QGSNETWORKACCESSMANAGER_H
19 #define QGSNETWORKACCESSMANAGER_H
24 #include <QStringList>
25 #include <QNetworkAccessManager>
26 #include <QNetworkProxy>
27 #include <QNetworkRequest>
29 #include <QWaitCondition>
32 #include "qgis_core.h"
34 #include "qgssettingsentry.h"
39 #include "qgsconfig.h"
40 constexpr
int sFilePrefixLength = CMAKE_SOURCE_DIR[
sizeof( CMAKE_SOURCE_DIR ) - 1] ==
'/' ?
sizeof( CMAKE_SOURCE_DIR ) + 1 :
sizeof( CMAKE_SOURCE_DIR );
42 #define QgsSetRequestInitiatorClass(request, _class) request.setAttribute( static_cast< QNetworkRequest::Attribute >( QgsNetworkRequestParameters::AttributeInitiatorClass ), _class ); request.setAttribute( static_cast< QNetworkRequest::Attribute >( QgsNetworkRequestParameters::AttributeInitiatorRequestId ), QString(QString( __FILE__ ).mid( sFilePrefixLength ) + ':' + QString::number( __LINE__ ) + " (" + __FUNCTION__ + ")") );
43 #define QgsSetRequestInitiatorId(request, str) request.setAttribute( static_cast< QNetworkRequest::Attribute >( QgsNetworkRequestParameters::AttributeInitiatorRequestId ), QString(QString( __FILE__ ).mid( sFilePrefixLength ) + ':' + QString::number( __LINE__ ) + " (" + __FUNCTION__ + "): " + str) );
59 AttributeInitiatorClass = QNetworkRequest::User + 3000,
73 const QNetworkRequest &request,
75 const QByteArray &content = QByteArray() );
80 QNetworkAccessManager::Operation
operation()
const {
return mOperation; }
88 QNetworkRequest
request()
const {
return mRequest; }
104 QByteArray
content()
const {
return mContent; }
129 QNetworkAccessManager::Operation mOperation;
130 QNetworkRequest mRequest;
131 QString mOriginatingThreadId;
134 QString mInitiatorClass;
135 QVariant mInitiatorRequestId;
187 virtual void handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors );
229 virtual void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
236 virtual void handleAuthRequestOpenBrowser(
const QUrl &url );
243 virtual void handleAuthRequestCloseBrowser();
312 void setSslErrorHandler( std::unique_ptr< QgsSslErrorHandler > handler );
330 void setAuthHandler( std::unique_ptr< QgsNetworkAuthenticationHandler > handler );
341 void insertProxyFactory( QNetworkProxyFactory *factory
SIP_TRANSFER );
357 const QList<QNetworkProxyFactory *> proxyFactories()
const;
367 const QNetworkProxy &fallbackProxy()
const;
378 QStringList excludeList()
const;
389 QStringList noProxyList()
const;
402 void setFallbackProxyAndExcludes(
const QNetworkProxy &proxy,
const QStringList &excludes,
const QStringList &noProxyURLs );
409 static QString cacheLoadControlName( QNetworkRequest::CacheLoadControl control );
416 static QNetworkRequest::CacheLoadControl cacheLoadControlFromName(
const QString &name );
425 void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
462 static int timeout();
471 static void setTimeout(
int time );
493 static QgsNetworkReplyContent blockingGet( QNetworkRequest &request,
const QString &authCfg = QString(),
bool forceRefresh =
false,
QgsFeedback *feedback =
nullptr );
515 static QgsNetworkReplyContent blockingPost( QNetworkRequest &request,
const QByteArray &data,
const QString &authCfg = QString(),
bool forceRefresh =
false,
QgsFeedback *feedback =
nullptr );
523 void requestAuthOpenBrowser(
const QUrl &url )
const;
531 void requestAuthCloseBrowser()
const;
539 void abortAuthBrowser();
544 static const inline QgsSettingsEntryInteger settingsNetworkTimeout = QgsSettingsEntryInteger( QStringLiteral(
"/qgis/networkAndProxy/networkTimeout" ), QgsSettings::NoSection, 60000, QObject::tr(
"Network timeout" ) );
668 void sslErrorsOccurred( QNetworkReply *,
const QList<QSslError> &errors );
669 void sslErrorsHandled( QNetworkReply *reply );
685 void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
686 void authRequestHandled( QNetworkReply *reply );
700 void onReplyFinished( QNetworkReply *reply );
702 void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
704 void onReplySslErrors(
const QList<QSslError> &errors );
706 void handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors );
709 void onAuthRequired( QNetworkReply *reply, QAuthenticator *auth );
710 void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
713 QNetworkReply *createRequest( QNetworkAccessManager::Operation op,
const QNetworkRequest &req, QIODevice *outgoingData =
nullptr )
override;
717 void unlockAfterSslErrorHandled();
718 void afterSslErrorHandled( QNetworkReply *reply );
721 void unlockAfterAuthRequestHandled();
722 void afterAuthRequestHandled( QNetworkReply *reply );
724 void pauseTimeout( QNetworkReply *reply );
725 void restartTimeout( QNetworkReply *reply );
726 static int getRequestId( QNetworkReply *reply );
728 QList<QNetworkProxyFactory *> mProxyFactories;
729 QNetworkProxy mFallbackProxy;
730 QStringList mExcludedURLs;
731 QStringList mNoProxyURLs;
732 bool mUseSystemProxy =
false;
733 bool mInitialized =
false;
734 bool mCacheDisabled =
false;
737 std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
739 QMutex mSslErrorHandlerMutex;
741 QWaitCondition mSslErrorWaitCondition;
744 std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
746 QMutex mAuthRequestHandlerMutex;
748 QWaitCondition mAuthRequestWaitCondition;
Base class for feedback objects to be used for cancellation of something running in a worker thread.
network access manager for QGIS
void finished(QgsNetworkReplyContent reply)
Emitted whenever a pending network reply is finished.
void setCacheDisabled(bool disabled)
Sets whether all network caching should be disabled.
void downloadProgress(int requestId, qint64 bytesReceived, qint64 bytesTotal)
Emitted when a network reply receives a progress report.
void requestEncounteredSslErrors(int requestId, const QList< QSslError > &errors)
Emitted when a network request encounters SSL errors.
void requestTimedOut(QNetworkReply *)
void requestAboutToBeCreated(QgsNetworkRequestParameters request)
Emitted when a network request is about to be created.
void requestAuthDetailsAdded(int requestId, const QString &realm, const QString &user, const QString &password)
Emitted when network authentication details have been added to a request.
bool cacheDisabled() const
Returns true if all network caching is disabled.
void requestRequiresAuth(int requestId, const QString &realm)
Emitted when a network request prompts an authentication request.
Q_DECL_DEPRECATED void requestCreated(QNetworkReply *)
Q_DECL_DEPRECATED void requestAboutToBeCreated(QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice *)
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.
Network authentication handler, used for responding to network authentication requests during network...
virtual ~QgsNetworkAuthenticationHandler()=default
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.
RequestAttributes
Custom request attributes.
@ AttributeInitiatorRequestId
Internal ID used by originator object to identify requests.
QgsNetworkRequestParameters()=default
Default constructor.
QNetworkAccessManager::Operation operation() const
Returns the request operation, e.g.
QNetworkRequest request() const
Returns the network request.
QString originatingThreadId() const
Returns a string identifying the thread which the request originated from.
QString initiatorClassName() const
Returns the class name of the object which initiated this request.
QByteArray content() const
Returns the request's content.
QVariant initiatorRequestId() const
Returns the internal ID used by the object which initiated this request to identify individual reques...
SSL error handler, used for responding to SSL errors encountered during network requests.
virtual ~QgsSslErrorHandler()=default
constexpr int sFilePrefixLength