QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 <QNetworkCookie>
27#include <QNetworkCookieJar>
28#include <QNetworkProxy>
29#include <QNetworkRequest>
30#include <QMutex>
31#include <QWaitCondition>
32#include <QSemaphore>
33#include <memory>
34
35#include "qgis_core.h"
36#include "qgis_sip.h"
37
38class QgsFeedback;
40
48{
49 public:
50
53 {
54 AttributeInitiatorClass = QNetworkRequest::User + 3000,
56 };
57
62
67 QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
68 const QNetworkRequest &request,
69 int requestId,
70 const QByteArray &content = QByteArray() );
71
75 QNetworkAccessManager::Operation operation() const { return mOperation; }
76
83 QNetworkRequest request() const { return mRequest; }
84
88 QString originatingThreadId() const { return mOriginatingThreadId; }
89
93 int requestId() const { return mRequestId; }
94
99 QByteArray content() const { return mContent; }
100
109 QString initiatorClassName() const { return mInitiatorClass; }
110
120 QVariant initiatorRequestId() const { return mInitiatorRequestId; }
121
122 private:
123
124 QNetworkAccessManager::Operation mOperation;
125 QNetworkRequest mRequest;
126 QString mOriginatingThreadId;
127 int mRequestId = 0;
128 QByteArray mContent;
129 QString mInitiatorClass;
130 QVariant mInitiatorRequestId;
131};
132
134
135#ifndef SIP_RUN
136
165class CORE_EXPORT QgsSslErrorHandler
166{
167
168 public:
169
170 virtual ~QgsSslErrorHandler() = default;
171
182 virtual void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
183
184};
185
210{
211
212 public:
213
215
224 virtual void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
225
231 virtual void handleAuthRequestOpenBrowser( const QUrl &url );
232
238 virtual void handleAuthRequestCloseBrowser();
239
240};
241#endif
242
243
259class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
260{
261 Q_OBJECT
262
263 public:
264
283 static QgsNetworkAccessManager *instance( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
284
285 QgsNetworkAccessManager( QObject *parent = nullptr );
286
287#ifndef SIP_RUN
288
305 void setSslErrorHandler( std::unique_ptr< QgsSslErrorHandler > handler );
306
323 void setAuthHandler( std::unique_ptr< QgsNetworkAuthenticationHandler > handler );
324#endif
325
334 void insertProxyFactory( QNetworkProxyFactory *factory SIP_TRANSFER );
335
342 void removeProxyFactory( QNetworkProxyFactory *factory SIP_TRANSFERBACK );
343
350 const QList<QNetworkProxyFactory *> proxyFactories() const;
351
360 const QNetworkProxy &fallbackProxy() const;
361
371 QStringList excludeList() const;
372
382 QStringList noProxyList() const;
383
395 void setFallbackProxyAndExcludes( const QNetworkProxy &proxy, const QStringList &excludes, const QStringList &noProxyURLs );
396
402 static QString cacheLoadControlName( QNetworkRequest::CacheLoadControl control );
403
409 static QNetworkRequest::CacheLoadControl cacheLoadControlFromName( const QString &name );
410
418 void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
419
420#ifndef SIP_RUN
421
429 bool cacheDisabled() const { return mCacheDisabled; }
430
441 void setCacheDisabled( bool disabled ) { mCacheDisabled = disabled; }
442#endif
443
447 bool useSystemProxy() const { return mUseSystemProxy; }
448
455 static int timeout();
456
464 static void setTimeout( int time );
465
486 static QgsNetworkReplyContent blockingGet( QNetworkRequest &request, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = nullptr );
487
508 static QgsNetworkReplyContent blockingPost( QNetworkRequest &request, const QByteArray &data, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = nullptr );
509
521#ifndef SIP_RUN
522 static QString setRequestPreprocessor( const std::function< void( QNetworkRequest *request )> &processor );
523#else
524 static QString setRequestPreprocessor( SIP_PYCALLABLE / AllowNone / );
525 % MethodCode
526 PyObject *s = 0;
527 QString id;
528 Py_XINCREF( a0 );
529 Py_BEGIN_ALLOW_THREADS
530 id = QgsNetworkAccessManager::setRequestPreprocessor( [a0]( QNetworkRequest *arg )->QString
531 {
532 QString res;
533 SIP_BLOCK_THREADS
534 PyObject *s = sipCallMethod( NULL, a0, "D", arg, sipType_QNetworkRequest, NULL );
535 int state;
536 int sipIsError = 0;
537 QString *t1 = reinterpret_cast<QString *>( sipConvertToType( s, sipType_QString, 0, SIP_NOT_NONE, &state, &sipIsError ) );
538 if ( sipIsError == 0 )
539 {
540 res = QString( *t1 );
541 }
542 sipReleaseType( t1, sipType_QString, state );
543 SIP_UNBLOCK_THREADS
544 return res;
545 } );
546 Py_END_ALLOW_THREADS
547
548 s = sipConvertFromNewType( new QString( id ), sipType_QString, 0 );
549 return s;
550 % End
551#endif
552
563#ifndef SIP_RUN
564 static bool removeRequestPreprocessor( const QString &id );
565#else
566 static void removeRequestPreprocessor( const QString &id );
567 % MethodCode
569 {
570 PyErr_SetString( PyExc_KeyError, QStringLiteral( "No processor with id %1 exists." ).arg( *a0 ).toUtf8().constData() );
571 sipIsErr = 1;
572 }
573 % End
574#endif
575
587#ifndef SIP_RUN
588 static QString setReplyPreprocessor( const std::function<void ( const QNetworkRequest &, QNetworkReply * )> &processor );
589#else
590 static QString setReplyPreprocessor( SIP_PYCALLABLE / AllowNone / );
591 % MethodCode
592 PyObject *s = 0;
593 QString id;
594 Py_XINCREF( a0 );
595 Py_BEGIN_ALLOW_THREADS
596 id = QgsNetworkAccessManager::setReplyPreprocessor( [a0]( const QNetworkRequest &request, QNetworkReply *reply )
597 {
598 SIP_BLOCK_THREADS
599 Py_XDECREF( sipCallMethod( NULL, a0, "ND", new QNetworkRequest( request ), sipType_QNetworkRequest, NULL, reply, sipType_QNetworkReply, NULL ) );
600 SIP_UNBLOCK_THREADS
601 } );
602
603 Py_END_ALLOW_THREADS
604 s = sipConvertFromNewType( new QString( id ), sipType_QString, 0 );
605 return s;
606 % End
607#endif
608
619#ifndef SIP_RUN
620 static bool removeReplyPreprocessor( const QString &id );
621#else
622 static void removeReplyPreprocessor( const QString &id );
623 % MethodCode
625 {
626 PyErr_SetString( PyExc_KeyError, QStringLiteral( "No processor with id %1 exists." ).arg( *a0 ).toUtf8().constData() );
627 sipIsErr = 1;
628 }
629 % End
630#endif
631
638 void requestAuthOpenBrowser( const QUrl &url ) const;
639
646 void requestAuthCloseBrowser() const;
647
654 void abortAuthBrowser();
655
656
657#ifndef SIP_RUN
660#endif
661
667 void preprocessRequest( QNetworkRequest *req ) const;
668
669 signals:
670
674 Q_DECL_DEPRECATED void requestAboutToBeCreated( QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice * ) SIP_DEPRECATED;
675
689
703
719
732
747 void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
748
764 void requestRequiresAuth( int requestId, const QString &realm );
765
781 void requestAuthDetailsAdded( int requestId, const QString &realm, const QString &user, const QString &password );
782
783#ifndef QT_NO_SSL
784
799 void requestEncounteredSslErrors( int requestId, const QList<QSslError> &errors );
800
801#ifndef SIP_RUN
803 // these signals are for internal use only - it's not safe to connect by external code
804 void sslErrorsOccurred( QNetworkReply *, const QList<QSslError> &errors );
805 void sslErrorsHandled( QNetworkReply *reply );
807#endif
808
809#endif
810
814 Q_DECL_DEPRECATED void requestCreated( QNetworkReply * ) SIP_DEPRECATED;
815
816 void requestTimedOut( QNetworkReply * );
817
818#ifndef SIP_RUN
820 // these signals are for internal use only - it's not safe to connect by external code
821 void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
822 void authRequestHandled( QNetworkReply *reply );
824#endif
825
832
838 void cookiesChanged( const QList<QNetworkCookie> &cookies );
839
840 private slots:
841 void abortRequest();
842
843 void onReplyFinished( QNetworkReply *reply );
844
845 void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
846#ifndef QT_NO_SSL
847 void onReplySslErrors( const QList<QSslError> &errors );
848
849 void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
850#endif
851
852 void onAuthRequired( QNetworkReply *reply, QAuthenticator *auth );
853 void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
854
855 void syncCookies( const QList<QNetworkCookie> &cookies );
856
857 protected:
858 QNetworkReply *createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData = nullptr ) override;
859
860 private:
861#ifndef QT_NO_SSL
862 void afterSslErrorHandled( QNetworkReply *reply );
863#endif
864
865 void afterAuthRequestHandled( QNetworkReply *reply );
866
867 void pauseTimeout( QNetworkReply *reply );
868 void restartTimeout( QNetworkReply *reply );
869 static int getRequestId( QNetworkReply *reply );
870
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;
878 static QgsNetworkAccessManager *sMainNAM;
879 // ssl error handler, will be set for main thread ONLY
880 std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
881 // Used by worker threads to wait for ssl error handler run in main thread
882 QSemaphore mSslErrorHandlerSemaphore;
883
884 // auth request handler, will be set for main thread ONLY
885 std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
886 // Used by worker threads to wait for authentication handler run in main thread
887 QSemaphore mAuthRequestHandlerSemaphore;
888
889 friend class TestQgsNetworkAccessManager;
890};
891
892#endif // QGSNETWORKACCESSMANAGER_H
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
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
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48