18#ifndef QGSNETWORKACCESSMANAGER_H
19#define QGSNETWORKACCESSMANAGER_H
25#include <QNetworkAccessManager>
26#include <QNetworkCookie>
27#include <QNetworkCookieJar>
28#include <QNetworkProxy>
29#include <QNetworkRequest>
31#include <QWaitCondition>
43constexpr 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() );
597 static QString setReplyPreprocessor(
const std::function<
void (
const QNetworkRequest &, QNetworkReply * )> &processor );
599 static QString setReplyPreprocessor( SIP_PYCALLABLE / AllowNone / );
602 Py_BEGIN_ALLOW_THREADS
607 Py_XDECREF( sipCallMethod( NULL, a0,
"ND",
new QNetworkRequest( request ), sipType_QNetworkRequest, NULL, reply, sipType_QNetworkReply, NULL ) );
611 s = sipConvertFromNewType(
new QString(
id ), sipType_QString, 0 );
628 static bool removeReplyPreprocessor(
const QString &
id );
630 static void removeReplyPreprocessor(
const QString &
id );
634 PyErr_SetString( PyExc_KeyError, QStringLiteral(
"No processor with id %1 exists." ).arg( *a0 ).toUtf8().constData() );
646 void requestAuthOpenBrowser(
const QUrl &url )
const;
654 void requestAuthCloseBrowser()
const;
662 void abortAuthBrowser();
675 void preprocessRequest( QNetworkRequest *req )
const;
797 void sslErrorsOccurred( QNetworkReply *,
const QList<QSslError> &errors );
798 void sslErrorsHandled( QNetworkReply *reply );
814 void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
815 void authRequestHandled( QNetworkReply *reply );
836 void onReplyFinished( QNetworkReply *reply );
838 void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
840 void onReplySslErrors(
const QList<QSslError> &errors );
842 void handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors );
845 void onAuthRequired( QNetworkReply *reply, QAuthenticator *auth );
846 void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
848 void syncCookies(
const QList<QNetworkCookie> &cookies );
851 QNetworkReply *createRequest( QNetworkAccessManager::Operation op,
const QNetworkRequest &req, QIODevice *outgoingData =
nullptr )
override;
855 void afterSslErrorHandled( QNetworkReply *reply );
858 void afterAuthRequestHandled( QNetworkReply *reply );
860 void pauseTimeout( QNetworkReply *reply );
861 void restartTimeout( QNetworkReply *reply );
862 static int getRequestId( QNetworkReply *reply );
864 QList<QNetworkProxyFactory *> mProxyFactories;
865 QNetworkProxy mFallbackProxy;
866 QStringList mExcludedURLs;
867 QStringList mNoProxyURLs;
868 bool mUseSystemProxy =
false;
869 bool mInitialized =
false;
870 bool mCacheDisabled =
false;
873 std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
875 QSemaphore mSslErrorHandlerSemaphore;
878 std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
880 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 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.
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 *)
static bool removeReplyPreprocessor(const QString &id)
Removes the custom reply pre-processor function with matching id.
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.
static const char * QGIS_NETWORKANDPROXY
SSL error handler, used for responding to SSL errors encountered during network requests.
virtual ~QgsSslErrorHandler()=default
constexpr int sFilePrefixLength