QGIS API Documentation  3.19.0-Master (f616be29d3)
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 #include "qgssettingsentry.h"
35 
36 class QgsFeedback;
37 
38 #ifndef SIP_RUN
39 #include "qgsconfig.h"
40 constexpr int sFilePrefixLength = CMAKE_SOURCE_DIR[sizeof( CMAKE_SOURCE_DIR ) - 1] == '/' ? sizeof( CMAKE_SOURCE_DIR ) + 1 : sizeof( CMAKE_SOURCE_DIR );
41 
42 #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__ + ")") );
43 #define QgsSetRequestInitiatorId(request, str) request.setAttribute( static_cast< QNetworkRequest::Attribute >( QgsNetworkRequestParameters::AttributeInitiatorRequestId ), QString(QString( __FILE__ ).mid( sFilePrefixLength ) + ':' + QString::number( __LINE__ ) + " (" + __FUNCTION__ + "): " + str) );
44 #endif
45 
52 class CORE_EXPORT QgsNetworkRequestParameters
53 {
54  public:
55 
58  {
59  AttributeInitiatorClass = QNetworkRequest::User + 3000,
61  };
62 
67 
72  QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
73  const QNetworkRequest &request,
74  int requestId,
75  const QByteArray &content = QByteArray() );
76 
80  QNetworkAccessManager::Operation operation() const { return mOperation; }
81 
88  QNetworkRequest request() const { return mRequest; }
89 
93  QString originatingThreadId() const { return mOriginatingThreadId; }
94 
98  int requestId() const { return mRequestId; }
99 
104  QByteArray content() const { return mContent; }
105 
114  QString initiatorClassName() const { return mInitiatorClass; }
115 
125  QVariant initiatorRequestId() const { return mInitiatorRequestId; }
126 
127  private:
128 
129  QNetworkAccessManager::Operation mOperation;
130  QNetworkRequest mRequest;
131  QString mOriginatingThreadId;
132  int mRequestId = 0;
133  QByteArray mContent;
134  QString mInitiatorClass;
135  QVariant mInitiatorRequestId;
136 };
137 
139 
140 #ifndef SIP_RUN
141 
170 class CORE_EXPORT QgsSslErrorHandler
171 {
172 
173  public:
174 
175  virtual ~QgsSslErrorHandler() = default;
176 
187  virtual void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
188 
189 };
190 
215 {
216 
217  public:
218 
219  virtual ~QgsNetworkAuthenticationHandler() = default;
220 
229  virtual void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
230 
236  virtual void handleAuthRequestOpenBrowser( const QUrl &url );
237 
243  virtual void handleAuthRequestCloseBrowser();
244 
245 };
246 #endif
247 
248 
266 class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
267 {
268  Q_OBJECT
269 
270  public:
271 
290  static QgsNetworkAccessManager *instance( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
291 
292  QgsNetworkAccessManager( QObject *parent = nullptr );
293 
294 #ifndef SIP_RUN
295 
312  void setSslErrorHandler( std::unique_ptr< QgsSslErrorHandler > handler );
313 
330  void setAuthHandler( std::unique_ptr< QgsNetworkAuthenticationHandler > handler );
331 #endif
332 
341  void insertProxyFactory( QNetworkProxyFactory *factory SIP_TRANSFER );
342 
349  void removeProxyFactory( QNetworkProxyFactory *factory SIP_TRANSFERBACK );
350 
357  const QList<QNetworkProxyFactory *> proxyFactories() const;
358 
367  const QNetworkProxy &fallbackProxy() const;
368 
378  QStringList excludeList() const;
379 
389  QStringList noProxyList() const;
390 
402  void setFallbackProxyAndExcludes( const QNetworkProxy &proxy, const QStringList &excludes, const QStringList &noProxyURLs );
403 
409  static QString cacheLoadControlName( QNetworkRequest::CacheLoadControl control );
410 
416  static QNetworkRequest::CacheLoadControl cacheLoadControlFromName( const QString &name );
417 
425  void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
426 
427 #ifndef SIP_RUN
428 
436  bool cacheDisabled() const { return mCacheDisabled; }
437 
448  void setCacheDisabled( bool disabled ) { mCacheDisabled = disabled; }
449 #endif
450 
454  bool useSystemProxy() const { return mUseSystemProxy; }
455 
462  static int timeout();
463 
471  static void setTimeout( int time );
472 
493  static QgsNetworkReplyContent blockingGet( QNetworkRequest &request, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = nullptr );
494 
515  static QgsNetworkReplyContent blockingPost( QNetworkRequest &request, const QByteArray &data, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = nullptr );
516 
523  void requestAuthOpenBrowser( const QUrl &url ) const;
524 
531  void requestAuthCloseBrowser() const;
532 
533 #ifndef SIP_RUN
535  static const inline QgsSettingsEntryInteger settingsNetworkTimeout = QgsSettingsEntryInteger( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), QgsSettings::NoSection, 60000, QObject::tr( "Network timeout" ) );
536 #endif
537 
538 
539  signals:
540 
544  Q_DECL_DEPRECATED void requestAboutToBeCreated( QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice * ) SIP_DEPRECATED;
545 
558 
574 
587 
602  void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
603 
619  void requestRequiresAuth( int requestId, const QString &realm );
620 
636  void requestAuthDetailsAdded( int requestId, const QString &realm, const QString &user, const QString &password );
637 
638 #ifndef QT_NO_SSL
639 
654  void requestEncounteredSslErrors( int requestId, const QList<QSslError> &errors );
655 
656 #ifndef SIP_RUN
658  // these signals are for internal use only - it's not safe to connect by external code
659  void sslErrorsOccurred( QNetworkReply *, const QList<QSslError> &errors );
660  void sslErrorsHandled( QNetworkReply *reply );
662 #endif
663 
664 #endif
665 
669  Q_DECL_DEPRECATED void requestCreated( QNetworkReply * ) SIP_DEPRECATED;
670 
671  void requestTimedOut( QNetworkReply * );
672 
673 #ifndef SIP_RUN
675  // these signals are for internal use only - it's not safe to connect by external code
676  void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
677  void authRequestHandled( QNetworkReply *reply );
679 #endif
680 
681 
682  private slots:
683  void abortRequest();
684 
685  void onReplyFinished( QNetworkReply *reply );
686 
687  void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
688 #ifndef QT_NO_SSL
689  void onReplySslErrors( const QList<QSslError> &errors );
690 
691  void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
692 #endif
693 
694  void onAuthRequired( QNetworkReply *reply, QAuthenticator *auth );
695  void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
696 
697  protected:
698  QNetworkReply *createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData = nullptr ) override;
699 
700  private:
701 #ifndef QT_NO_SSL
702  void unlockAfterSslErrorHandled();
703  void afterSslErrorHandled( QNetworkReply *reply );
704 #endif
705 
706  void unlockAfterAuthRequestHandled();
707  void afterAuthRequestHandled( QNetworkReply *reply );
708 
709  void pauseTimeout( QNetworkReply *reply );
710  void restartTimeout( QNetworkReply *reply );
711  static int getRequestId( QNetworkReply *reply );
712 
713  QList<QNetworkProxyFactory *> mProxyFactories;
714  QNetworkProxy mFallbackProxy;
715  QStringList mExcludedURLs;
716  QStringList mNoProxyURLs;
717  bool mUseSystemProxy = false;
718  bool mInitialized = false;
719  bool mCacheDisabled = false;
720  static QgsNetworkAccessManager *sMainNAM;
721  // ssl error handler, will be set for main thread ONLY
722  std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
723  // only in use by worker threads, unused in main thread
724  QMutex mSslErrorHandlerMutex;
725  // only in use by worker threads, unused in main thread
726  QWaitCondition mSslErrorWaitCondition;
727 
728  // auth request handler, will be set for main thread ONLY
729  std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
730  // only in use by worker threads, unused in main thread
731  QMutex mAuthRequestHandlerMutex;
732  // only in use by worker threads, unused in main thread
733  QWaitCondition mAuthRequestWaitCondition;
734 
735 };
736 
737 #endif // QGSNETWORKACCESSMANAGER_H
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
network access manager for QGIS
void finished(QgsNetworkReplyContent reply)
Emitted whenever a pending network reply is finished.
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 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.
Q_DECL_DEPRECATED void requestCreated(QNetworkReply *)
Q_DECL_DEPRECATED void requestAboutToBeCreated(QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice *)
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...
SSL error handler, used for responding to SSL errors encountered during network requests.
virtual ~QgsSslErrorHandler()=default
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
constexpr int sFilePrefixLength