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"
54 AttributeInitiatorClass = QNetworkRequest::User + 3000,
68 const QNetworkRequest &request,
70 const QByteArray &content = QByteArray() );
75 QNetworkAccessManager::Operation
operation()
const {
return mOperation; }
83 QNetworkRequest
request()
const {
return mRequest; }
99 QByteArray
content()
const {
return mContent; }
124 QNetworkAccessManager::Operation mOperation;
125 QNetworkRequest mRequest;
126 QString mOriginatingThreadId;
129 QString mInitiatorClass;
130 QVariant mInitiatorRequestId;
182 virtual void handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors );
224 virtual void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
231 virtual void handleAuthRequestOpenBrowser(
const QUrl &url );
238 virtual void handleAuthRequestCloseBrowser();
305 void setSslErrorHandler( std::unique_ptr< QgsSslErrorHandler > handler );
323 void setAuthHandler( std::unique_ptr< QgsNetworkAuthenticationHandler > handler );
334 void insertProxyFactory( QNetworkProxyFactory *factory
SIP_TRANSFER );
350 const QList<QNetworkProxyFactory *> proxyFactories()
const;
360 const QNetworkProxy &fallbackProxy()
const;
371 QStringList excludeList()
const;
382 QStringList noProxyList()
const;
395 void setFallbackProxyAndExcludes(
const QNetworkProxy &proxy,
const QStringList &excludes,
const QStringList &noProxyURLs );
402 static QString cacheLoadControlName( QNetworkRequest::CacheLoadControl control );
409 static QNetworkRequest::CacheLoadControl cacheLoadControlFromName(
const QString &name );
418 void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
455 static int timeout();
464 static void setTimeout(
int time );
486 static QgsNetworkReplyContent blockingGet( QNetworkRequest &request,
const QString &authCfg = QString(),
bool forceRefresh =
false,
QgsFeedback *feedback =
nullptr );
508 static QgsNetworkReplyContent blockingPost( QNetworkRequest &request,
const QByteArray &data,
const QString &authCfg = QString(),
bool forceRefresh =
false,
QgsFeedback *feedback =
nullptr );
522 static QString setRequestPreprocessor(
const std::function<
void( QNetworkRequest *request )> &processor );
524 static QString setRequestPreprocessor( SIP_PYCALLABLE / AllowNone / );
529 Py_BEGIN_ALLOW_THREADS
534 PyObject *s = sipCallMethod( NULL, a0,
"D", arg, sipType_QNetworkRequest, NULL );
537 QString *t1 =
reinterpret_cast<QString *
>( sipConvertToType( s, sipType_QString, 0, SIP_NOT_NONE, &state, &sipIsError ) );
538 if ( sipIsError == 0 )
540 res = QString( *t1 );
542 sipReleaseType( t1, sipType_QString, state );
548 s = sipConvertFromNewType(
new QString(
id ), sipType_QString, 0 );
564 static bool removeRequestPreprocessor(
const QString &
id );
566 static void removeRequestPreprocessor(
const QString &
id );
570 PyErr_SetString( PyExc_KeyError, QStringLiteral(
"No processor with id %1 exists." ).arg( *a0 ).toUtf8().constData() );
588 static QString setReplyPreprocessor(
const std::function<
void (
const QNetworkRequest &, QNetworkReply * )> &processor );
590 static QString setReplyPreprocessor( SIP_PYCALLABLE / AllowNone / );
595 Py_BEGIN_ALLOW_THREADS
599 Py_XDECREF( sipCallMethod( NULL, a0,
"ND",
new QNetworkRequest( request ), sipType_QNetworkRequest, NULL, reply, sipType_QNetworkReply, NULL ) );
604 s = sipConvertFromNewType(
new QString(
id ), sipType_QString, 0 );
620 static bool removeReplyPreprocessor(
const QString &
id );
622 static void removeReplyPreprocessor(
const QString &
id );
626 PyErr_SetString( PyExc_KeyError, QStringLiteral(
"No processor with id %1 exists." ).arg( *a0 ).toUtf8().constData() );
638 void requestAuthOpenBrowser(
const QUrl &url )
const;
646 void requestAuthCloseBrowser()
const;
654 void abortAuthBrowser();
667 void preprocessRequest( QNetworkRequest *req )
const;
804 void sslErrorsOccurred( QNetworkReply *,
const QList<QSslError> &errors );
805 void sslErrorsHandled( QNetworkReply *reply );
821 void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
822 void authRequestHandled( QNetworkReply *reply );
843 void onReplyFinished( QNetworkReply *reply );
845 void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
847 void onReplySslErrors(
const QList<QSslError> &errors );
849 void handleSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors );
852 void onAuthRequired( QNetworkReply *reply, QAuthenticator *auth );
853 void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
855 void syncCookies(
const QList<QNetworkCookie> &cookies );
858 QNetworkReply *createRequest( QNetworkAccessManager::Operation op,
const QNetworkRequest &req, QIODevice *outgoingData =
nullptr )
override;
862 void afterSslErrorHandled( QNetworkReply *reply );
865 void afterAuthRequestHandled( QNetworkReply *reply );
867 void pauseTimeout( QNetworkReply *reply );
868 void restartTimeout( QNetworkReply *reply );
869 static int getRequestId( QNetworkReply *reply );
871 QList<QNetworkProxyFactory *> mProxyFactories;
872 QNetworkProxy mFallbackProxy;
873 QStringList mExcludedURLs;
874 QStringList mNoProxyURLs;
875 bool mUseSystemProxy =
false;
876 bool mInitialized =
false;
877 bool mCacheDisabled =
false;
880 std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
882 QSemaphore mSslErrorHandlerSemaphore;
885 std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
887 QSemaphore mAuthRequestHandlerSemaphore;
889 friend class TestQgsNetworkAccessManager;
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.
static const QgsSettingsEntryInteger * settingsNetworkTimeout
Settings entry network timeout.
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.
void requestCreated(const QgsNetworkRequestParameters &request)
Emitted when a network request has been 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.
SSL error handler, used for responding to SSL errors encountered during network requests.
virtual ~QgsSslErrorHandler()=default