QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
539  void abortAuthBrowser();
540 
541 
542 #ifndef SIP_RUN
544  static const inline QgsSettingsEntryInteger settingsNetworkTimeout = QgsSettingsEntryInteger( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), QgsSettings::NoSection, 60000, QObject::tr( "Network timeout" ) );
545 #endif
546 
547 
548  signals:
549 
553  Q_DECL_DEPRECATED void requestAboutToBeCreated( QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice * ) SIP_DEPRECATED;
554 
567 
583 
596 
611  void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
612 
628  void requestRequiresAuth( int requestId, const QString &realm );
629 
645  void requestAuthDetailsAdded( int requestId, const QString &realm, const QString &user, const QString &password );
646 
647 #ifndef QT_NO_SSL
648 
663  void requestEncounteredSslErrors( int requestId, const QList<QSslError> &errors );
664 
665 #ifndef SIP_RUN
667  // these signals are for internal use only - it's not safe to connect by external code
668  void sslErrorsOccurred( QNetworkReply *, const QList<QSslError> &errors );
669  void sslErrorsHandled( QNetworkReply *reply );
671 #endif
672 
673 #endif
674 
678  Q_DECL_DEPRECATED void requestCreated( QNetworkReply * ) SIP_DEPRECATED;
679 
680  void requestTimedOut( QNetworkReply * );
681 
682 #ifndef SIP_RUN
684  // these signals are for internal use only - it's not safe to connect by external code
685  void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
686  void authRequestHandled( QNetworkReply *reply );
688 #endif
689 
696 
697  private slots:
698  void abortRequest();
699 
700  void onReplyFinished( QNetworkReply *reply );
701 
702  void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
703 #ifndef QT_NO_SSL
704  void onReplySslErrors( const QList<QSslError> &errors );
705 
706  void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
707 #endif
708 
709  void onAuthRequired( QNetworkReply *reply, QAuthenticator *auth );
710  void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
711 
712  protected:
713  QNetworkReply *createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData = nullptr ) override;
714 
715  private:
716 #ifndef QT_NO_SSL
717  void unlockAfterSslErrorHandled();
718  void afterSslErrorHandled( QNetworkReply *reply );
719 #endif
720 
721  void unlockAfterAuthRequestHandled();
722  void afterAuthRequestHandled( QNetworkReply *reply );
723 
724  void pauseTimeout( QNetworkReply *reply );
725  void restartTimeout( QNetworkReply *reply );
726  static int getRequestId( QNetworkReply *reply );
727 
728  QList<QNetworkProxyFactory *> mProxyFactories;
729  QNetworkProxy mFallbackProxy;
730  QStringList mExcludedURLs;
731  QStringList mNoProxyURLs;
732  bool mUseSystemProxy = false;
733  bool mInitialized = false;
734  bool mCacheDisabled = false;
735  static QgsNetworkAccessManager *sMainNAM;
736  // ssl error handler, will be set for main thread ONLY
737  std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
738  // only in use by worker threads, unused in main thread
739  QMutex mSslErrorHandlerMutex;
740  // only in use by worker threads, unused in main thread
741  QWaitCondition mSslErrorWaitCondition;
742 
743  // auth request handler, will be set for main thread ONLY
744  std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
745  // only in use by worker threads, unused in main thread
746  QMutex mAuthRequestHandlerMutex;
747  // only in use by worker threads, unused in main thread
748  QWaitCondition mAuthRequestWaitCondition;
749 
750 };
751 
752 #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 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...
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