QGIS API Documentation  3.17.0-Master (df2c9ff931)
qgsnetworkaccessmanager.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnetworkaccessmanager.h - description
3  -------------------
4  begin : 2010-05-08
5  copyright : (C) 2010 by Juergen E. Fischer
6  email : jef at norbit dot de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSNETWORKACCESSMANAGER_H
19 #define QGSNETWORKACCESSMANAGER_H
20 
21 #include <QList>
22 #include "qgsnetworkreply.h"
23 #include "qgis_sip.h"
24 #include <QStringList>
25 #include <QNetworkAccessManager>
26 #include <QNetworkProxy>
27 #include <QNetworkRequest>
28 #include <QMutex>
29 #include <QWaitCondition>
30 #include <memory>
31 
32 #include "qgis_core.h"
33 #include "qgis_sip.h"
34 
35 class QgsFeedback;
36 
37 #ifndef SIP_RUN
38 #include "qgsconfig.h"
39 constexpr int sFilePrefixLength = CMAKE_SOURCE_DIR[sizeof( CMAKE_SOURCE_DIR ) - 1] == '/' ? sizeof( CMAKE_SOURCE_DIR ) + 1 : sizeof( CMAKE_SOURCE_DIR );
40 
41 #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__ + ")") );
42 #define QgsSetRequestInitiatorId(request, str) request.setAttribute( static_cast< QNetworkRequest::Attribute >( QgsNetworkRequestParameters::AttributeInitiatorRequestId ), QString(QString( __FILE__ ).mid( sFilePrefixLength ) + ':' + QString::number( __LINE__ ) + " (" + __FUNCTION__ + "): " + str) );
43 #endif
44 
51 class CORE_EXPORT QgsNetworkRequestParameters
52 {
53  public:
54 
57  {
58  AttributeInitiatorClass = QNetworkRequest::User + 3000,
60  };
61 
65  QgsNetworkRequestParameters() = default;
66 
71  QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
72  const QNetworkRequest &request,
73  int requestId,
74  const QByteArray &content = QByteArray() );
75 
79  QNetworkAccessManager::Operation operation() const { return mOperation; }
80 
87  QNetworkRequest request() const { return mRequest; }
88 
92  QString originatingThreadId() const { return mOriginatingThreadId; }
93 
97  int requestId() const { return mRequestId; }
98 
103  QByteArray content() const { return mContent; }
104 
113  QString initiatorClassName() const { return mInitiatorClass; }
114 
124  QVariant initiatorRequestId() const { return mInitiatorRequestId; }
125 
126  private:
127 
128  QNetworkAccessManager::Operation mOperation;
129  QNetworkRequest mRequest;
130  QString mOriginatingThreadId;
131  int mRequestId = 0;
132  QByteArray mContent;
133  QString mInitiatorClass;
134  QVariant mInitiatorRequestId;
135 };
136 
138 
139 #ifndef SIP_RUN
140 
169 class CORE_EXPORT QgsSslErrorHandler
170 {
171 
172  public:
173 
174  virtual ~QgsSslErrorHandler() = default;
175 
186  virtual void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
187 
188 };
189 
214 {
215 
216  public:
217 
218  virtual ~QgsNetworkAuthenticationHandler() = default;
219 
228  virtual void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
229 
230 };
231 #endif
232 
233 
251 class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
252 {
253  Q_OBJECT
254 
255  public:
256 
275  static QgsNetworkAccessManager *instance( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
276 
277  QgsNetworkAccessManager( QObject *parent = nullptr );
278 
279 #ifndef SIP_RUN
280 
297  void setSslErrorHandler( std::unique_ptr< QgsSslErrorHandler > handler );
298 
315  void setAuthHandler( std::unique_ptr< QgsNetworkAuthenticationHandler > handler );
316 #endif
317 
326  void insertProxyFactory( QNetworkProxyFactory *factory SIP_TRANSFER );
327 
334  void removeProxyFactory( QNetworkProxyFactory *factory SIP_TRANSFERBACK );
335 
342  const QList<QNetworkProxyFactory *> proxyFactories() const;
343 
352  const QNetworkProxy &fallbackProxy() const;
353 
363  QStringList excludeList() const;
364 
374  QStringList noProxyList() const;
375 
387  void setFallbackProxyAndExcludes( const QNetworkProxy &proxy, const QStringList &excludes, const QStringList &noProxyURLs );
388 
394  static QString cacheLoadControlName( QNetworkRequest::CacheLoadControl control );
395 
401  static QNetworkRequest::CacheLoadControl cacheLoadControlFromName( const QString &name );
402 
410  void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
411 
415  bool useSystemProxy() const { return mUseSystemProxy; }
416 
423  static int timeout();
424 
432  static void setTimeout( int time );
433 
454  static QgsNetworkReplyContent blockingGet( QNetworkRequest &request, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = nullptr );
455 
476  static QgsNetworkReplyContent blockingPost( QNetworkRequest &request, const QByteArray &data, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = nullptr );
477 
478  signals:
479 
483  Q_DECL_DEPRECATED void requestAboutToBeCreated( QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice * ) SIP_DEPRECATED;
484 
496  void requestAboutToBeCreated( QgsNetworkRequestParameters request );
497 
512  void finished( QgsNetworkReplyContent reply );
513 
525  void requestTimedOut( QgsNetworkRequestParameters request );
526 
541  void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
542 
558  void requestRequiresAuth( int requestId, const QString &realm );
559 
575  void requestAuthDetailsAdded( int requestId, const QString &realm, const QString &user, const QString &password );
576 
577 #ifndef QT_NO_SSL
578 
593  void requestEncounteredSslErrors( int requestId, const QList<QSslError> &errors );
594 
595 #ifndef SIP_RUN
596  // these signals are for internal use only - it's not safe to connect by external code
598  void sslErrorsOccurred( QNetworkReply *, const QList<QSslError> &errors );
599  void sslErrorsHandled( QNetworkReply *reply );
601 #endif
602 
603 #endif
604 
608  Q_DECL_DEPRECATED void requestCreated( QNetworkReply * ) SIP_DEPRECATED;
609 
610  void requestTimedOut( QNetworkReply * );
611 
612 #ifndef SIP_RUN
613  // these signals are for internal use only - it's not safe to connect by external code
615  void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
616  void authRequestHandled( QNetworkReply *reply );
618 #endif
619 
620 
621  private slots:
622  void abortRequest();
623 
624  void onReplyFinished( QNetworkReply *reply );
625 
626  void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
627 #ifndef QT_NO_SSL
628  void onReplySslErrors( const QList<QSslError> &errors );
629 
630  void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
631 #endif
632 
633  void onAuthRequired( QNetworkReply *reply, QAuthenticator *auth );
634  void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
635 
636  protected:
637  QNetworkReply *createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData = nullptr ) override;
638 
639  private:
640 #ifndef QT_NO_SSL
641  void unlockAfterSslErrorHandled();
642  void afterSslErrorHandled( QNetworkReply *reply );
643 #endif
644 
645  void unlockAfterAuthRequestHandled();
646  void afterAuthRequestHandled( QNetworkReply *reply );
647 
648  void pauseTimeout( QNetworkReply *reply );
649  void restartTimeout( QNetworkReply *reply );
650  static int getRequestId( QNetworkReply *reply );
651 
652  QList<QNetworkProxyFactory *> mProxyFactories;
653  QNetworkProxy mFallbackProxy;
654  QStringList mExcludedURLs;
655  QStringList mNoProxyURLs;
656  bool mUseSystemProxy = false;
657  bool mInitialized = false;
658  static QgsNetworkAccessManager *sMainNAM;
659  // ssl error handler, will be set for main thread ONLY
660  std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
661  // only in use by worker threads, unused in main thread
662  QMutex mSslErrorHandlerMutex;
663  // only in use by worker threads, unused in main thread
664  QWaitCondition mSslErrorWaitCondition;
665 
666  // auth request handler, will be set for main thread ONLY
667  std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
668  // only in use by worker threads, unused in main thread
669  QMutex mAuthRequestHandlerMutex;
670  // only in use by worker threads, unused in main thread
671  QWaitCondition mAuthRequestWaitCondition;
672 
673 };
674 
675 #endif // QGSNETWORKACCESSMANAGER_H
QVariant initiatorRequestId() const
Returns the internal ID used by the object which initiated this request to identify individual reques...
QByteArray content() const
Returns the request&#39;s content.
constexpr int sFilePrefixLength
Encapsulates parameters and properties of a network request.
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
int requestId() const
Returns a unique ID identifying the request.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:43
Internal ID used by originator object to identify requests.
SSL error handler, used for responding to SSL errors encountered during network requests.
QString initiatorClassName() const
Returns the class name of the object which initiated this request.
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QNetworkRequest request() const
Returns the network request.
QNetworkAccessManager::Operation operation() const
Returns the request operation, e.g.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
Network authentication handler, used for responding to network authentication requests during network...
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QString originatingThreadId() const
Returns a string identifying the thread which the request originated from.
bool useSystemProxy() const
Returns whether the system proxy should be used.
RequestAttributes
Custom request attributes.
network access manager for QGISThis class implements the QGIS network access manager.