QGIS API Documentation 3.39.0-Master (3aed037ce22)
Loading...
Searching...
No Matches
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
59
64 QgsNetworkRequestParameters( QNetworkAccessManager::Operation operation,
65 const QNetworkRequest &request,
66 int requestId,
67 const QByteArray &content = QByteArray() );
68
72 QNetworkAccessManager::Operation operation() const { return mOperation; }
73
80 QNetworkRequest request() const { return mRequest; }
81
85 QString originatingThreadId() const { return mOriginatingThreadId; }
86
90 int requestId() const { return mRequestId; }
91
96 QByteArray content() const { return mContent; }
97
106 QString initiatorClassName() const { return mInitiatorClass; }
107
117 QVariant initiatorRequestId() const { return mInitiatorRequestId; }
118
119 private:
120
121 QNetworkAccessManager::Operation mOperation;
122 QNetworkRequest mRequest;
123 QString mOriginatingThreadId;
124 int mRequestId = 0;
125 QByteArray mContent;
126 QString mInitiatorClass;
127 QVariant mInitiatorRequestId;
128};
129
131
132#ifndef SIP_RUN
133
162class CORE_EXPORT QgsSslErrorHandler
163{
164
165 public:
166
167 virtual ~QgsSslErrorHandler() = default;
168
179 virtual void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
180
181};
182
207{
208
209 public:
210
212
221 virtual void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
222
228 virtual void handleAuthRequestOpenBrowser( const QUrl &url );
229
235 virtual void handleAuthRequestCloseBrowser();
236
237};
238#endif
239
240
256class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
257{
258 Q_OBJECT
259
260 public:
261
280 static QgsNetworkAccessManager *instance( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
281
282 QgsNetworkAccessManager( QObject *parent = nullptr );
283
284#ifndef SIP_RUN
285
302 void setSslErrorHandler( std::unique_ptr< QgsSslErrorHandler > handler );
303
320 void setAuthHandler( std::unique_ptr< QgsNetworkAuthenticationHandler > handler );
321#endif
322
331 void insertProxyFactory( QNetworkProxyFactory *factory SIP_TRANSFER );
332
339 void removeProxyFactory( QNetworkProxyFactory *factory SIP_TRANSFERBACK );
340
347 const QList<QNetworkProxyFactory *> proxyFactories() const;
348
357 const QNetworkProxy &fallbackProxy() const;
358
368 QStringList excludeList() const;
369
379 QStringList noProxyList() const;
380
392 void setFallbackProxyAndExcludes( const QNetworkProxy &proxy, const QStringList &excludes, const QStringList &noProxyURLs );
393
399 static QString cacheLoadControlName( QNetworkRequest::CacheLoadControl control );
400
406 static QNetworkRequest::CacheLoadControl cacheLoadControlFromName( const QString &name );
407
415 void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
416
417#ifndef SIP_RUN
418
426 bool cacheDisabled() const { return mCacheDisabled; }
427
438 void setCacheDisabled( bool disabled ) { mCacheDisabled = disabled; }
439#endif
440
444 bool useSystemProxy() const { return mUseSystemProxy; }
445
452 static int timeout();
453
461 static void setTimeout( int time );
462
483 static QgsNetworkReplyContent blockingGet( QNetworkRequest &request, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = nullptr );
484
505 static QgsNetworkReplyContent blockingPost( QNetworkRequest &request, const QByteArray &data, const QString &authCfg = QString(), bool forceRefresh = false, QgsFeedback *feedback = nullptr );
506
518#ifndef SIP_RUN
519 static QString setRequestPreprocessor( const std::function< void( QNetworkRequest *request )> &processor );
520#else
521 static QString setRequestPreprocessor( SIP_PYCALLABLE / AllowNone / );
522 % MethodCode
523 PyObject *s = 0;
524 QString id;
525 Py_XINCREF( a0 );
526 Py_BEGIN_ALLOW_THREADS
527 id = QgsNetworkAccessManager::setRequestPreprocessor( [a0]( QNetworkRequest *arg )->QString
528 {
529 QString res;
530 SIP_BLOCK_THREADS
531 PyObject *s = sipCallMethod( NULL, a0, "D", arg, sipType_QNetworkRequest, NULL );
532 int state;
533 int sipIsError = 0;
534 QString *t1 = reinterpret_cast<QString *>( sipConvertToType( s, sipType_QString, 0, SIP_NOT_NONE, &state, &sipIsError ) );
535 if ( sipIsError == 0 )
536 {
537 res = QString( *t1 );
538 }
539 sipReleaseType( t1, sipType_QString, state );
540 SIP_UNBLOCK_THREADS
541 return res;
542 } );
543 Py_END_ALLOW_THREADS
544
545 s = sipConvertFromNewType( new QString( id ), sipType_QString, 0 );
546 return s;
547 % End
548#endif
549
560#ifndef SIP_RUN
561 static bool removeRequestPreprocessor( const QString &id );
562#else
563 static void removeRequestPreprocessor( const QString &id );
564 % MethodCode
566 {
567 PyErr_SetString( PyExc_KeyError, QStringLiteral( "No processor with id %1 exists." ).arg( *a0 ).toUtf8().constData() );
568 sipIsErr = 1;
569 }
570 % End
571#endif
572
584#ifndef SIP_RUN
585 static QString setReplyPreprocessor( const std::function<void ( const QNetworkRequest &, QNetworkReply * )> &processor );
586#else
587 static QString setReplyPreprocessor( SIP_PYCALLABLE / AllowNone / );
588 % MethodCode
589 PyObject *s = 0;
590 QString id;
591 Py_XINCREF( a0 );
592 Py_BEGIN_ALLOW_THREADS
593 id = QgsNetworkAccessManager::setReplyPreprocessor( [a0]( const QNetworkRequest &request, QNetworkReply *reply )
594 {
595 SIP_BLOCK_THREADS
596 Py_XDECREF( sipCallMethod( NULL, a0, "ND", new QNetworkRequest( request ), sipType_QNetworkRequest, NULL, reply, sipType_QNetworkReply, NULL ) );
597 SIP_UNBLOCK_THREADS
598 } );
599
600 Py_END_ALLOW_THREADS
601 s = sipConvertFromNewType( new QString( id ), sipType_QString, 0 );
602 return s;
603 % End
604#endif
605
616#ifndef SIP_RUN
617 static bool removeReplyPreprocessor( const QString &id );
618#else
619 static void removeReplyPreprocessor( const QString &id );
620 % MethodCode
622 {
623 PyErr_SetString( PyExc_KeyError, QStringLiteral( "No processor with id %1 exists." ).arg( *a0 ).toUtf8().constData() );
624 sipIsErr = 1;
625 }
626 % End
627#endif
628
635 void requestAuthOpenBrowser( const QUrl &url ) const;
636
643 void requestAuthCloseBrowser() const;
644
651 void abortAuthBrowser();
652
653
654#ifndef SIP_RUN
657#endif
658
664 void preprocessRequest( QNetworkRequest *req ) const;
665
666 signals:
667
671 Q_DECL_DEPRECATED void requestAboutToBeCreated( QNetworkAccessManager::Operation, const QNetworkRequest &, QIODevice * ) SIP_DEPRECATED;
672
686
700
716
729
744 void downloadProgress( int requestId, qint64 bytesReceived, qint64 bytesTotal );
745
761 void requestRequiresAuth( int requestId, const QString &realm );
762
778 void requestAuthDetailsAdded( int requestId, const QString &realm, const QString &user, const QString &password );
779
780#ifndef QT_NO_SSL
781
796 void requestEncounteredSslErrors( int requestId, const QList<QSslError> &errors );
797
798#ifndef SIP_RUN
800 // these signals are for internal use only - it's not safe to connect by external code
801 void sslErrorsOccurred( QNetworkReply *, const QList<QSslError> &errors );
802 void sslErrorsHandled( QNetworkReply *reply );
804#endif
805
806#endif
807
811 Q_DECL_DEPRECATED void requestCreated( QNetworkReply * ) SIP_DEPRECATED;
812
813 void requestTimedOut( QNetworkReply * );
814
815#ifndef SIP_RUN
817 // these signals are for internal use only - it's not safe to connect by external code
818 void authRequestOccurred( QNetworkReply *, QAuthenticator *auth );
819 void authRequestHandled( QNetworkReply *reply );
821#endif
822
829
835 void cookiesChanged( const QList<QNetworkCookie> &cookies );
836
837 private slots:
838 void abortRequest();
839
840 void onReplyFinished( QNetworkReply *reply );
841
842 void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
843#ifndef QT_NO_SSL
844 void onReplySslErrors( const QList<QSslError> &errors );
845
846 void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
847#endif
848
849 void onAuthRequired( QNetworkReply *reply, QAuthenticator *auth );
850 void handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth );
851
852 void syncCookies( const QList<QNetworkCookie> &cookies );
853
854 protected:
855 QNetworkReply *createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData = nullptr ) override;
856
857 private:
858#ifndef QT_NO_SSL
859 void afterSslErrorHandled( QNetworkReply *reply );
860#endif
861
862 void afterAuthRequestHandled( QNetworkReply *reply );
863
864 void pauseTimeout( QNetworkReply *reply );
865 void restartTimeout( QNetworkReply *reply );
866 static int getRequestId( QNetworkReply *reply );
867
868 QList<QNetworkProxyFactory *> mProxyFactories;
869 QNetworkProxy mFallbackProxy;
870 QStringList mExcludedURLs;
871 QStringList mNoProxyURLs;
872 bool mUseSystemProxy = false;
873 bool mInitialized = false;
874 bool mCacheDisabled = false;
875 static QgsNetworkAccessManager *sMainNAM;
876 // ssl error handler, will be set for main thread ONLY
877 std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;
878 // Used by worker threads to wait for ssl error handler run in main thread
879 QSemaphore mSslErrorHandlerSemaphore;
880
881 // auth request handler, will be set for main thread ONLY
882 std::unique_ptr< QgsNetworkAuthenticationHandler > mAuthHandler;
883 // Used by worker threads to wait for authentication handler run in main thread
884 QSemaphore mAuthRequestHandlerSemaphore;
885
886 friend class TestQgsNetworkAccessManager;
887};
888
889#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.
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