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() );
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;
682 Q_DECL_DEPRECATED
void requestAboutToBeCreated( QNetworkAccessManager::Operation,
const QNetworkRequest &, QIODevice * )
SIP_DEPRECATED;
740 void downloadProgress(
int requestId, qint64 bytesReceived, qint64 bytesTotal );
757 void requestRequiresAuth(
int requestId,
const QString &realm );
774 void requestAuthDetailsAdded(
int requestId,
const QString &realm,
const QString &user,
const QString &password );
792 void requestEncounteredSslErrors(
int requestId,
const QList<QSslError> &errors );
797 void sslErrorsOccurred( QNetworkReply *,
const QList<QSslError> &errors );
798 void sslErrorsHandled( QNetworkReply *reply );
807 Q_DECL_DEPRECATED
void requestCreated( QNetworkReply * )
SIP_DEPRECATED;
809 void requestTimedOut( QNetworkReply * );
814 void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
815 void authRequestHandled( QNetworkReply *reply );
824 void authBrowserAborted();
831 void cookiesChanged(
const QList<QNetworkCookie> &cookies );
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 unlockAfterSslErrorHandled();
856 void afterSslErrorHandled( QNetworkReply *reply );
859 void afterAuthRequestHandled( QNetworkReply *reply );
861 void pauseTimeout( QNetworkReply *reply );
862 void restartTimeout( QNetworkReply *reply );
863 static int getRequestId( QNetworkReply *reply );
865 QList<QNetworkProxyFactory *> mProxyFactories;
866 QNetworkProxy mFallbackProxy;
867 QStringList mExcludedURLs;
868 QStringList mNoProxyURLs;
869 bool mUseSystemProxy =
false;
870 bool mInitialized =
false;
871 bool mCacheDisabled =
false;
874 std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
876 QMutex mSslErrorHandlerMutex;
878 QWaitCondition mSslErrorWaitCondition;
881 std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
883 QSemaphore mAuthRequestHandlerSemaphore;
886 #endif // QGSNETWORKACCESSMANAGER_H