18 #ifndef QGSNETWORKACCESSMANAGER_H
19 #define QGSNETWORKACCESSMANAGER_H
24 #include <QStringList>
25 #include <QNetworkAccessManager>
26 #include <QNetworkCookie>
27 #include <QNetworkCookieJar>
28 #include <QNetworkProxy>
29 #include <QNetworkRequest>
31 #include <QWaitCondition>
35 #include "qgis_core.h"
42 #include "qgsconfig.h"
43 constexpr
int sFilePrefixLength = CMAKE_SOURCE_DIR[
sizeof( CMAKE_SOURCE_DIR ) - 1] ==
'/' ?
sizeof( CMAKE_SOURCE_DIR ) + 1 :
sizeof( CMAKE_SOURCE_DIR );
45 #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__ + ")") );
46 #define QgsSetRequestInitiatorId(request, str) request.setAttribute( static_cast< QNetworkRequest::Attribute >( QgsNetworkRequestParameters::AttributeInitiatorRequestId ), QString(QString( __FILE__ ).mid( sFilePrefixLength ) + ':' + QString::number( __LINE__ ) + " (" + __FUNCTION__ + "): " + str) );
62 AttributeInitiatorClass = QNetworkRequest::User + 3000,
76 const QNetworkRequest &request,
78 const QByteArray &content = QByteArray() );
83 QNetworkAccessManager::Operation
operation()
const {
return mOperation; }
91 QNetworkRequest
request()
const {
return mRequest; }
107 QByteArray
content()
const {
return mContent; }
132 QNetworkAccessManager::Operation mOperation;
133 QNetworkRequest mRequest;
134 QString mOriginatingThreadId;
137 QString mInitiatorClass;
138 QVariant mInitiatorRequestId;
190 virtual void handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors );
232 virtual void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
239 virtual void handleAuthRequestOpenBrowser(
const QUrl &url );
246 virtual void handleAuthRequestCloseBrowser();
315 void setSslErrorHandler( std::unique_ptr< QgsSslErrorHandler > handler );
333 void setAuthHandler( std::unique_ptr< QgsNetworkAuthenticationHandler > handler );
344 void insertProxyFactory( QNetworkProxyFactory *factory
SIP_TRANSFER );
360 const QList<QNetworkProxyFactory *> proxyFactories()
const;
370 const QNetworkProxy &fallbackProxy()
const;
381 QStringList excludeList()
const;
392 QStringList noProxyList()
const;
405 void setFallbackProxyAndExcludes(
const QNetworkProxy &proxy,
const QStringList &excludes,
const QStringList &noProxyURLs );
412 static QString cacheLoadControlName( QNetworkRequest::CacheLoadControl control );
419 static QNetworkRequest::CacheLoadControl cacheLoadControlFromName(
const QString &name );
428 void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
465 static int timeout();
474 static void setTimeout(
int time );
496 static QgsNetworkReplyContent blockingGet( QNetworkRequest &request,
const QString &authCfg = QString(),
bool forceRefresh =
false,
QgsFeedback *feedback =
nullptr );
518 static QgsNetworkReplyContent blockingPost( QNetworkRequest &request,
const QByteArray &data,
const QString &authCfg = QString(),
bool forceRefresh =
false,
QgsFeedback *feedback =
nullptr );
532 static QString setRequestPreprocessor(
const std::function<
void( QNetworkRequest *request )> &processor );
534 static QString setRequestPreprocessor( SIP_PYCALLABLE / AllowNone / );
537 Py_BEGIN_ALLOW_THREADS
543 PyObject *s = sipCallMethod( NULL, a0,
"D", arg, sipType_QNetworkRequest, NULL );
546 QString *t1 =
reinterpret_cast<QString *
>( sipConvertToType( s, sipType_QString, 0, SIP_NOT_NONE, &state, &sipIsError ) );
547 if ( sipIsError == 0 )
549 res = QString( *t1 );
551 sipReleaseType( t1, sipType_QString, state );
556 s = sipConvertFromNewType(
new QString(
id ), sipType_QString, 0 );
573 static bool removeRequestPreprocessor(
const QString &
id );
575 static void removeRequestPreprocessor(
const QString &
id );
579 PyErr_SetString( PyExc_KeyError, QStringLiteral(
"No processor with id %1 exists." ).arg( *a0 ).toUtf8().constData() );
591 void requestAuthOpenBrowser(
const QUrl &url )
const;
599 void requestAuthCloseBrowser()
const;
607 void abortAuthBrowser();
620 void preprocessRequest( QNetworkRequest *req )
const;
742 void sslErrorsOccurred( QNetworkReply *,
const QList<QSslError> &errors );
743 void sslErrorsHandled( QNetworkReply *reply );
759 void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
760 void authRequestHandled( QNetworkReply *reply );
781 void onReplyFinished( QNetworkReply *reply );
783 void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
785 void onReplySslErrors(
const QList<QSslError> &errors );
787 void handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors );
790 void onAuthRequired( QNetworkReply *reply, QAuthenticator *auth );
791 void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
793 void syncCookies(
const QList<QNetworkCookie> &cookies );
796 QNetworkReply *createRequest( QNetworkAccessManager::Operation op,
const QNetworkRequest &req, QIODevice *outgoingData =
nullptr )
override;
800 void unlockAfterSslErrorHandled();
801 void afterSslErrorHandled( QNetworkReply *reply );
804 void afterAuthRequestHandled( QNetworkReply *reply );
806 void pauseTimeout( QNetworkReply *reply );
807 void restartTimeout( QNetworkReply *reply );
808 static int getRequestId( QNetworkReply *reply );
810 QList<QNetworkProxyFactory *> mProxyFactories;
811 QNetworkProxy mFallbackProxy;
812 QStringList mExcludedURLs;
813 QStringList mNoProxyURLs;
814 bool mUseSystemProxy =
false;
815 bool mInitialized =
false;
816 bool mCacheDisabled =
false;
819 std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
821 QMutex mSslErrorHandlerMutex;
823 QWaitCondition mSslErrorWaitCondition;
826 std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
828 QSemaphore mAuthRequestHandlerSemaphore;
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 cookiesChanged(const QList< QNetworkCookie > &cookies)
Emitted when the cookies changed.
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.
static bool removeRequestPreprocessor(const QString &id)
Removes the custom 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.
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.
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...
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...
An integer settings entry.
SSL error handler, used for responding to SSL errors encountered during network requests.
virtual ~QgsSslErrorHandler()=default
constexpr int sFilePrefixLength