39#include <QNetworkReply> 
   40#include <QRecursiveMutex> 
   41#include <QThreadStorage> 
   42#include <QAuthenticator> 
   43#include <QStandardPaths> 
   49#include <QSslConfiguration> 
   57static std::vector< std::pair< QString, std::function< void( QNetworkRequest * ) > > > sCustomPreprocessors;
 
   58static std::vector< std::pair< QString, std::function< void( 
const QNetworkRequest &, QNetworkReply * ) > > > sCustomReplyPreprocessors;
 
   61class QgsNetworkProxyFactory : 
public QNetworkProxyFactory
 
   64    QgsNetworkProxyFactory() = 
default;
 
   66    QList<QNetworkProxy> queryProxy( 
const QNetworkProxyQuery &query = QNetworkProxyQuery() )
 override 
   72      for ( QNetworkProxyFactory *f : constProxyFactories )
 
   74        QList<QNetworkProxy> systemproxies = QNetworkProxyFactory::systemProxyForQuery( query );
 
   75        if ( !systemproxies.isEmpty() )
 
   78        QList<QNetworkProxy> proxies = f->queryProxy( query );
 
   79        if ( !proxies.isEmpty() )
 
   84      if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
 
   87      const QString url = query.url().toString();
 
   90      for ( 
const QString &noProxy : constNoProxyList )
 
   92        if ( !noProxy.trimmed().isEmpty() && url.startsWith( noProxy ) )
 
   94          QgsDebugMsgLevel( QStringLiteral( 
"don't using any proxy for %1 [exclude %2]" ).arg( url, noProxy ), 4 );
 
   95          return QList<QNetworkProxy>() << QNetworkProxy( QNetworkProxy::NoProxy );
 
  100      for ( 
const QString &exclude : constExcludeList )
 
  102        if ( !exclude.trimmed().isEmpty() && url.startsWith( exclude ) )
 
  104          QgsDebugMsgLevel( QStringLiteral( 
"using default proxy for %1 [exclude %2]" ).arg( url, exclude ), 4 );
 
  105          return QList<QNetworkProxy>() << QNetworkProxy( QNetworkProxy::DefaultProxy );
 
  111        QgsDebugMsgLevel( QStringLiteral( 
"requesting system proxy for query %1" ).arg( url ), 4 );
 
  112        QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery( query );
 
  113        if ( !proxies.isEmpty() )
 
  116                            .arg( proxies.first().hostName() ).arg( proxies.first().port() ), 4 );
 
  121      QgsDebugMsgLevel( QStringLiteral( 
"using fallback proxy for %1" ).arg( url ), 4 );
 
  128class QgsNetworkCookieJar : 
public QNetworkCookieJar
 
  134      : QNetworkCookieJar( parent )
 
  138    bool deleteCookie( 
const QNetworkCookie &cookie )
 override 
  140      const QMutexLocker locker( &mMutex );
 
  141      if ( QNetworkCookieJar::deleteCookie( cookie ) )
 
  143        emit mNam->cookiesChanged( allCookies() );
 
  148    bool insertCookie( 
const QNetworkCookie &cookie )
 override 
  150      const QMutexLocker locker( &mMutex );
 
  151      if ( QNetworkCookieJar::insertCookie( cookie ) )
 
  153        emit mNam->cookiesChanged( allCookies() );
 
  158    bool setCookiesFromUrl( 
const QList<QNetworkCookie> &cookieList, 
const QUrl &url )
 override 
  160      const QMutexLocker locker( &mMutex );
 
  161      return QNetworkCookieJar::setCookiesFromUrl( cookieList, url );
 
  163    bool updateCookie( 
const QNetworkCookie &cookie )
 override 
  165      const QMutexLocker locker( &mMutex );
 
  166      if ( QNetworkCookieJar::updateCookie( cookie ) )
 
  168        emit mNam->cookiesChanged( allCookies() );
 
  175    QList<QNetworkCookie> allCookies()
 const 
  177      const QMutexLocker locker( &mMutex );
 
  178      return QNetworkCookieJar::allCookies();
 
  180    void setAllCookies( 
const QList<QNetworkCookie> &cookieList )
 
  182      const QMutexLocker locker( &mMutex );
 
  183      QNetworkCookieJar::setAllCookies( cookieList );
 
  187    mutable QRecursiveMutex mMutex;
 
  197  static QThreadStorage<QgsNetworkAccessManager> sInstances;
 
  200  if ( nam->thread() == qApp->thread() )
 
  203  if ( !nam->mInitialized )
 
 
  213  : QNetworkAccessManager( parent )
 
  214  , mSslErrorHandlerSemaphore( 1 )
 
  215  , mAuthRequestHandlerSemaphore( 1 )
 
  217  setProxyFactory( 
new QgsNetworkProxyFactory() );
 
  218  setCookieJar( 
new QgsNetworkCookieJar( 
this ) );
 
  219  enableStrictTransportSecurityStore( 
true );
 
  220  setStrictTransportSecurityEnabled( 
true );
 
 
  225  Q_ASSERT( sMainNAM == 
this );
 
  226  mSslErrorHandler = std::move( handler );
 
 
  231  Q_ASSERT( sMainNAM == 
this );
 
  232  mAuthHandler = std::move( handler );
 
 
  237  mProxyFactories.insert( 0, factory );
 
 
  242  mProxyFactories.removeAll( factory );
 
 
  247  return mProxyFactories;
 
 
  252  return mExcludedURLs;
 
 
  262  return mFallbackProxy;
 
 
  267  QgsDebugMsgLevel( QStringLiteral( 
"proxy settings: (type:%1 host: %2:%3, user:%4, password:%5" )
 
  268                    .arg( proxy.type() == QNetworkProxy::DefaultProxy ? QStringLiteral( 
"DefaultProxy" ) :
 
  269                          proxy.type() == QNetworkProxy::Socks5Proxy ? QStringLiteral( 
"Socks5Proxy" ) :
 
  270                          proxy.type() == QNetworkProxy::NoProxy ? QStringLiteral( 
"NoProxy" ) :
 
  271                          proxy.type() == QNetworkProxy::HttpProxy ? QStringLiteral( 
"HttpProxy" ) :
 
  272                          proxy.type() == QNetworkProxy::HttpCachingProxy ? QStringLiteral( 
"HttpCachingProxy" ) :
 
  273                          proxy.type() == QNetworkProxy::FtpCachingProxy ? QStringLiteral( 
"FtpCachingProxy" ) :
 
  274                          QStringLiteral( 
"Undefined" ),
 
  278                          proxy.password().isEmpty() ? QStringLiteral( 
"not set" ) : QStringLiteral( 
"set" ) ), 4 );
 
  280  mFallbackProxy = proxy;
 
  281  mExcludedURLs = excludes;
 
  283  mExcludedURLs.erase( std::remove_if( mExcludedURLs.begin(), mExcludedURLs.end(), 
 
  284                                       []( 
const QString & url )
 
  286    return url.trimmed().isEmpty();
 
  287  } ), mExcludedURLs.end() ); 
 
  289  mNoProxyURLs = noProxyURLs;
 
  290  mNoProxyURLs.erase( std::remove_if( mNoProxyURLs.begin(), mNoProxyURLs.end(), 
 
  291                                      []( 
const QString & url )
 
  293    return url.trimmed().isEmpty();
 
  294  } ), mNoProxyURLs.end() ); 
 
 
  301  QNetworkRequest *pReq( 
const_cast< QNetworkRequest * 
>( &req ) ); 
 
  303  QString userAgent = s.
value( QStringLiteral( 
"/qgis/networkAndProxy/userAgent" ), 
"Mozilla/5.0" ).toString();
 
  304  if ( !userAgent.isEmpty() )
 
  306  userAgent += QStringLiteral( 
"QGIS/%1/%2" ).arg( 
Qgis::versionInt() ).arg( QSysInfo::prettyProductName() );
 
  307  pReq->setRawHeader( 
"User-Agent", userAgent.toLatin1() );
 
  310  const bool ishttps = pReq->url().scheme().compare( QLatin1String( 
"https" ), Qt::CaseInsensitive ) == 0;
 
  313    QgsDebugMsgLevel( QStringLiteral( 
"Adding trusted CA certs to request" ), 3 );
 
  314    QSslConfiguration sslconfig( pReq->sslConfiguration() );
 
  318    const QString hostport( QStringLiteral( 
"%1:%2" )
 
  319                            .arg( pReq->url().host().trimmed() )
 
  320                            .arg( pReq->url().port() != -1 ? pReq->url().port() : 443 ) );
 
  322    if ( !servconfig.
isNull() )
 
  324      QgsDebugMsgLevel( QStringLiteral( 
"Adding SSL custom config to request for %1" ).arg( hostport ), 2 );
 
  330    pReq->setSslConfiguration( sslconfig );
 
  334  if ( sMainNAM->mCacheDisabled )
 
  337    pReq->setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
 
  338    pReq->setAttribute( QNetworkRequest::CacheSaveControlAttribute, 
false );
 
  341  for ( 
const auto &preprocessor :  sCustomPreprocessors )
 
  343    preprocessor.second( pReq );
 
  346  static QAtomicInt sRequestId = 0;
 
  347  const int requestId = ++sRequestId;
 
  349  if ( QBuffer *buffer = qobject_cast<QBuffer *>( outgoingData ) )
 
  351    content = buffer->buffer();
 
  358  QNetworkReply *reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
 
  359  reply->setProperty( 
"requestId", requestId );
 
  366  connect( reply, &QNetworkReply::downloadProgress, 
this, &QgsNetworkAccessManager::onReplyDownloadProgress );
 
  368  connect( reply, &QNetworkReply::sslErrors, 
this, &QgsNetworkAccessManager::onReplySslErrors );
 
  371  for ( 
const auto &replyPreprocessor :  sCustomReplyPreprocessors )
 
  373    replyPreprocessor.second( req, reply );
 
  381    QTimer *timer = 
new QTimer( reply );
 
  382    timer->setObjectName( QStringLiteral( 
"timeoutTimer" ) );
 
  383    connect( timer, &QTimer::timeout, 
this, &QgsNetworkAccessManager::abortRequest );
 
  384    timer->setSingleShot( 
true );
 
  387    connect( reply, &QNetworkReply::downloadProgress, timer, [timer] { timer->start(); } );
 
  388    connect( reply, &QNetworkReply::uploadProgress, timer, [timer] { timer->start(); } );
 
  389    connect( reply, &QNetworkReply::finished, timer, &QTimer::stop );
 
  391  QgsDebugMsgLevel( QStringLiteral( 
"Created [reply:%1]" ).arg( 
reinterpret_cast< qint64 
>( reply ), 0, 16 ), 3 );
 
 
  396void QgsNetworkAccessManager::abortRequest()
 
  398  QTimer *timer = qobject_cast<QTimer *>( sender() );
 
  401  QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent() );
 
  405  QgsDebugMsgLevel( QStringLiteral( 
"Abort [reply:%1] %2" ).arg( 
reinterpret_cast< qint64 
>( reply ), 0, 16 ).arg( reply->url().toString() ), 3 );
 
  412void QgsNetworkAccessManager::onReplyFinished( QNetworkReply *reply )
 
  417void QgsNetworkAccessManager::onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
 
  419  if ( QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() ) )
 
  426void QgsNetworkAccessManager::onReplySslErrors( 
const QList<QSslError> &errors )
 
  428  QNetworkReply *reply = qobject_cast< QNetworkReply *>( sender() );
 
  430  Q_ASSERT( reply->manager() == 
this );
 
  432  QgsDebugMsgLevel( QStringLiteral( 
"Stopping network reply timeout whilst SSL error is handled" ), 2 );
 
  433  pauseTimeout( reply );
 
  438  mSslErrorHandlerSemaphore.acquire();
 
  442  emit sslErrorsOccurred( reply, errors );
 
  443  if ( 
this != sMainNAM )
 
  447    mSslErrorHandlerSemaphore.acquire();
 
  448    mSslErrorHandlerSemaphore.release();
 
  449    afterSslErrorHandled( reply );
 
  453void QgsNetworkAccessManager::afterSslErrorHandled( QNetworkReply *reply )
 
  455  if ( reply->manager() == 
this )
 
  457    restartTimeout( reply );
 
  458    emit sslErrorsHandled( reply );
 
  462void QgsNetworkAccessManager::afterAuthRequestHandled( QNetworkReply *reply )
 
  464  if ( reply->manager() == 
this )
 
  466    restartTimeout( reply );
 
  467    emit authRequestHandled( reply );
 
  471void QgsNetworkAccessManager::pauseTimeout( QNetworkReply *reply )
 
  473  Q_ASSERT( reply->manager() == 
this );
 
  475  QTimer *timer = reply->findChild<QTimer *>( QStringLiteral( 
"timeoutTimer" ) );
 
  476  if ( timer && timer->isActive() )
 
  482void QgsNetworkAccessManager::restartTimeout( QNetworkReply *reply )
 
  484  Q_ASSERT( reply->manager() == 
this );
 
  486  QTimer *timer = reply->findChild<QTimer *>( QStringLiteral( 
"timeoutTimer" ) );
 
  489    Q_ASSERT( !timer->isActive() );
 
  490    QgsDebugMsgLevel( QStringLiteral( 
"Restarting network reply timeout" ), 2 );
 
  491    timer->setSingleShot( 
true );
 
  496int QgsNetworkAccessManager::getRequestId( QNetworkReply *reply )
 
  498  return reply->property( 
"requestId" ).toInt();
 
  501void QgsNetworkAccessManager::handleSslErrors( QNetworkReply *reply, 
const QList<QSslError> &errors )
 
  503  mSslErrorHandler->handleSslErrors( reply, errors );
 
  504  afterSslErrorHandled( reply );
 
  505  qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mSslErrorHandlerSemaphore.release();
 
  510void QgsNetworkAccessManager::onAuthRequired( QNetworkReply *reply, QAuthenticator *auth )
 
  513  Q_ASSERT( reply->manager() == 
this );
 
  515  QgsDebugMsgLevel( QStringLiteral( 
"Stopping network reply timeout whilst auth request is handled" ), 2 );
 
  516  pauseTimeout( reply );
 
  521  mAuthRequestHandlerSemaphore.acquire();
 
  525  emit authRequestOccurred( reply, auth );
 
  527  if ( 
this != sMainNAM )
 
  531    mAuthRequestHandlerSemaphore.acquire();
 
  532    mAuthRequestHandlerSemaphore.release();
 
  533    afterAuthRequestHandled( reply );
 
  539  if ( 
this != sMainNAM )
 
  545  mAuthHandler->handleAuthRequestOpenBrowser( url );
 
 
  550  if ( 
this != sMainNAM )
 
  556  mAuthHandler->handleAuthRequestCloseBrowser();
 
 
  561  if ( 
this != sMainNAM )
 
 
  568void QgsNetworkAccessManager::handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth )
 
  570  mAuthHandler->handleAuthRequest( reply, auth );
 
  574  afterAuthRequestHandled( reply );
 
  575  qobject_cast<QgsNetworkAccessManager *>( reply->manager() )->mAuthRequestHandlerSemaphore.release();
 
  582    case QNetworkRequest::AlwaysNetwork:
 
  583      return QStringLiteral( 
"AlwaysNetwork" );
 
  584    case QNetworkRequest::PreferNetwork:
 
  585      return QStringLiteral( 
"PreferNetwork" );
 
  586    case QNetworkRequest::PreferCache:
 
  587      return QStringLiteral( 
"PreferCache" );
 
  588    case QNetworkRequest::AlwaysCache:
 
  589      return QStringLiteral( 
"AlwaysCache" );
 
  591  return QStringLiteral( 
"PreferNetwork" );
 
 
  596  if ( name == QLatin1String( 
"AlwaysNetwork" ) )
 
  598    return QNetworkRequest::AlwaysNetwork;
 
  600  else if ( name == QLatin1String( 
"PreferNetwork" ) )
 
  602    return QNetworkRequest::PreferNetwork;
 
  604  else if ( name == QLatin1String( 
"PreferCache" ) )
 
  606    return QNetworkRequest::PreferCache;
 
  608  else if ( name == QLatin1String( 
"AlwaysCache" ) )
 
  610    return QNetworkRequest::AlwaysCache;
 
  612  return QNetworkRequest::PreferNetwork;
 
 
  618  mUseSystemProxy = 
false;
 
  620  Q_ASSERT( sMainNAM );
 
  622  if ( sMainNAM != 
this )
 
  624    connect( 
this, &QNetworkAccessManager::proxyAuthenticationRequired,
 
  625             sMainNAM, &QNetworkAccessManager::proxyAuthenticationRequired,
 
  646    connect( 
this, &QNetworkAccessManager::sslErrors,
 
  647             sMainNAM, &QNetworkAccessManager::sslErrors,
 
  660    if ( !mSslErrorHandler )
 
  664      setAuthHandler( std::make_unique< QgsNetworkAuthenticationHandler>() );
 
  667  connect( 
this, &QgsNetworkAccessManager::sslErrorsOccurred, sMainNAM, &QgsNetworkAccessManager::handleSslErrors );
 
  669  connect( 
this, &QNetworkAccessManager::authenticationRequired, 
this, &QgsNetworkAccessManager::onAuthRequired );
 
  670  connect( 
this, &QgsNetworkAccessManager::authRequestOccurred, sMainNAM, &QgsNetworkAccessManager::handleAuthRequest );
 
  672  connect( 
this, &QNetworkAccessManager::finished, 
this, &QgsNetworkAccessManager::onReplyFinished );
 
  677  QStringList excludes;
 
  678  QStringList noProxyURLs;
 
  680  const bool proxyEnabled = settings.
value( QStringLiteral( 
"proxy/proxyEnabled" ), 
false ).toBool();
 
  685    excludes = settings.
value( QStringLiteral( 
"proxy/proxyExcludedUrls" ), QStringList() ).toStringList();
 
  687    noProxyURLs = settings.
value( QStringLiteral( 
"proxy/noProxyUrls" ), QStringList() ).toStringList();
 
  690    const QString proxyHost = settings.
value( QStringLiteral( 
"proxy/proxyHost" ), 
"" ).toString();
 
  691    const int proxyPort = settings.
value( QStringLiteral( 
"proxy/proxyPort" ), 
"" ).toString().toInt();
 
  693    const QString proxyUser = settings.
value( QStringLiteral( 
"proxy/proxyUser" ), 
"" ).toString();
 
  694    const QString proxyPassword = settings.
value( QStringLiteral( 
"proxy/proxyPassword" ), 
"" ).toString();
 
  696    const QString proxyTypeString = settings.
value( QStringLiteral( 
"proxy/proxyType" ), 
"" ).toString();
 
  698    if ( proxyTypeString == QLatin1String( 
"DefaultProxy" ) )
 
  700      mUseSystemProxy = 
true;
 
  701      QNetworkProxyFactory::setUseSystemConfiguration( 
true );
 
  702      QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
 
  703      if ( !proxies.isEmpty() )
 
  705        proxy = proxies.first();
 
  711      QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
 
  712      if ( proxyTypeString == QLatin1String( 
"Socks5Proxy" ) )
 
  714        proxyType = QNetworkProxy::Socks5Proxy;
 
  716      else if ( proxyTypeString == QLatin1String( 
"HttpProxy" ) )
 
  718        proxyType = QNetworkProxy::HttpProxy;
 
  720      else if ( proxyTypeString == QLatin1String( 
"HttpCachingProxy" ) )
 
  722        proxyType = QNetworkProxy::HttpCachingProxy;
 
  724      else if ( proxyTypeString == QLatin1String( 
"FtpCachingProxy" ) )
 
  726        proxyType = QNetworkProxy::FtpCachingProxy;
 
  730                        .arg( proxyHost ).arg( proxyPort )
 
  731                        .arg( proxyUser, proxyPassword ), 2
 
  733      proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
 
  736    const QString authcfg = settings.
value( QStringLiteral( 
"proxy/authcfg" ), 
"" ).toString();
 
  737    if ( !authcfg.isEmpty( ) )
 
  739      QgsDebugMsgLevel( QStringLiteral( 
"setting proxy from stored authentication configuration %1" ).arg( authcfg ), 2 );
 
  742        authManager->updateNetworkProxy( proxy, authcfg );
 
  752  QString cacheDirectory = settings.
value( QStringLiteral( 
"cache/directory" ) ).toString();
 
  753  if ( cacheDirectory.isEmpty() )
 
  754    cacheDirectory = QStandardPaths::writableLocation( QStandardPaths::CacheLocation );
 
  755  const qint64 cacheSize = settings.
value( QStringLiteral( 
"cache/size" ), 256 * 1024 * 1024 ).toLongLong();
 
  761  if ( cache() != newcache )
 
  762    setCache( newcache );
 
  764  if ( 
this != sMainNAM )
 
  766    static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies( 
static_cast<QgsNetworkCookieJar *
>( sMainNAM->cookieJar() )->allCookies() );
 
 
  770void QgsNetworkAccessManager::syncCookies( 
const QList<QNetworkCookie> &cookies )
 
  772  if ( sender() != 
this )
 
  774    static_cast<QgsNetworkCookieJar *
>( cookieJar() )->setAllCookies( cookies );
 
  775    if ( 
this == sMainNAM )
 
  796  br.
get( request, forceRefresh, feedback );
 
 
  804  br.
post( request, data, forceRefresh, feedback );
 
 
  810  QString 
id = QUuid::createUuid().toString();
 
  811  sCustomPreprocessors.emplace_back( std::make_pair( 
id, processor ) );
 
 
  817  const size_t prevCount = sCustomPreprocessors.size();
 
  818  sCustomPreprocessors.erase( std::remove_if( sCustomPreprocessors.begin(), sCustomPreprocessors.end(), [
id]( std::pair< QString, std::function< 
void( QNetworkRequest * ) > > &a )
 
  820    return a.first == id;
 
  821  } ), sCustomPreprocessors.end() );
 
  822  return prevCount != sCustomPreprocessors.size();
 
 
  827  QString 
id = QUuid::createUuid().toString();
 
  828  sCustomReplyPreprocessors.emplace_back( std::make_pair( 
id, processor ) );
 
 
  834  const size_t prevCount = sCustomReplyPreprocessors.size();
 
  835  sCustomReplyPreprocessors.erase( std::remove_if( sCustomReplyPreprocessors.begin(), sCustomReplyPreprocessors.end(), [
id]( std::pair< QString, std::function< 
void( 
const QNetworkRequest &, QNetworkReply * ) > > &a )
 
  837    return a.first == id;
 
  838  } ), sCustomReplyPreprocessors.end() );
 
  839  return prevCount != sCustomReplyPreprocessors.size();
 
 
  844  for ( 
const auto &preprocessor :  sCustomPreprocessors )
 
  846    preprocessor.second( req );
 
 
  856  : mOperation( operation )
 
  857  , mRequest( request )
 
  858  , mOriginatingThreadId( QStringLiteral( 
"0x%2" ).arg( reinterpret_cast<quintptr>( QThread::currentThread() ), 2 * QT_POINTER_SIZE, 16, QLatin1Char( 
'0' ) ) )
 
  859  , mRequestId( requestId )
 
  860  , mContent( content )
 
  861  , mInitiatorClass( request.attribute( static_cast< QNetworkRequest::Attribute >( 
QgsNetworkRequestParameters::AttributeInitiatorClass ) ).toString() )
 
  862  , mInitiatorRequestId( request.attribute( static_cast< QNetworkRequest::Attribute >( 
QgsNetworkRequestParameters::AttributeInitiatorRequestId ) ) )
 
 
  874  QgsDebugError( QStringLiteral( 
"SSL errors occurred accessing URL:\n%1" ).arg( reply->request().url().toString() ) );
 
 
  884  QgsDebugError( QStringLiteral( 
"Network reply required authentication, but no handler was in place to provide this authentication request while accessing the URL:\n%1" ).arg( reply->request().url().toString() ) );
 
 
  890  QgsDebugError( QStringLiteral( 
"Network authentication required external browser to open URL %1, but no handler was in place" ).arg( url.toString() ) );
 
 
  895  QgsDebugError( QStringLiteral( 
"Network authentication required external browser closed, but no handler was in place" ) );
 
 
  899#include "qgsnetworkaccessmanager.moc" 
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
 
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
 
static QList< QSslCertificate > casMerge(const QList< QSslCertificate > &bundle1, const QList< QSslCertificate > &bundle2)
casMerge merges two certificate bundles in a single one removing duplicates, the certificates from th...
 
Configuration container for SSL server connection exceptions or overrides.
 
QSsl::SslProtocol sslProtocol() const
SSL server protocol to use in connections.
 
int sslPeerVerifyDepth() const
Number or SSL client's peer to verify in connections.
 
bool isNull() const
Whether configuration is null (missing components)
 
QSslSocket::PeerVerifyMode sslPeerVerifyMode() const
SSL client's peer verify mode to use in connections.
 
Singleton offering an interface to manage the authentication configuration database and to utilize co...
 
const QgsAuthConfigSslServer sslCertCustomConfigByHost(const QString &hostport)
sslCertCustomConfigByHost get an SSL certificate custom config by hostport (host:port)
 
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
 
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "get" operation on the specified request.
 
ErrorCode post(QNetworkRequest &request, QIODevice *data, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "post" operation on the specified request, using the given data.
 
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
 
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
 
Base class for feedback objects to be used for cancellation of something running in a worker thread.
 
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
 
network access manager for QGIS
 
QStringList noProxyList() const
Returns the no proxy list.
 
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.
 
static QgsNetworkReplyContent blockingGet(QNetworkRequest &request, const QString &authCfg=QString(), bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Posts a GET request to obtain the contents of the target request and returns a new QgsNetworkReplyCon...
 
void insertProxyFactory(QNetworkProxyFactory *factory)
Inserts a factory into the proxy factories list.
 
void setSslErrorHandler(std::unique_ptr< QgsSslErrorHandler > handler)
Sets the application SSL error handler, which is used to respond to SSL errors encountered during net...
 
void abortAuthBrowser()
Abort any outstanding external browser login request.
 
void setCacheDisabled(bool disabled)
Sets whether all network caching should be disabled.
 
const QList< QNetworkProxyFactory * > proxyFactories() const
Returns a list of proxy factories used by the manager.
 
void downloadProgress(int requestId, qint64 bytesReceived, qint64 bytesTotal)
Emitted when a network reply receives a progress report.
 
void requestAuthOpenBrowser(const QUrl &url) const
Forwards an external browser login url opening request to the authentication handler.
 
void requestAuthCloseBrowser() const
Forwards an external browser login closure request to the authentication handler.
 
void requestEncounteredSslErrors(int requestId, const QList< QSslError > &errors)
Emitted when a network request encounters SSL errors.
 
static QString cacheLoadControlName(QNetworkRequest::CacheLoadControl control)
Returns the name for QNetworkRequest::CacheLoadControl.
 
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.
 
static QNetworkRequest::CacheLoadControl cacheLoadControlFromName(const QString &name)
Returns QNetworkRequest::CacheLoadControl from a name.
 
bool cacheDisabled() const
Returns true if all network caching is disabled.
 
QgsNetworkAccessManager(QObject *parent=nullptr)
 
void requestRequiresAuth(int requestId, const QString &realm)
Emitted when a network request prompts an authentication request.
 
void preprocessRequest(QNetworkRequest *req) const
Preprocesses request.
 
void setAuthHandler(std::unique_ptr< QgsNetworkAuthenticationHandler > handler)
Sets the application network authentication handler, which is used to respond to network authenticati...
 
static void setTimeout(int time)
Sets the maximum timeout time for network requests, in milliseconds.
 
static QgsNetworkReplyContent blockingPost(QNetworkRequest &request, const QByteArray &data, const QString &authCfg=QString(), bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Posts a POST request to obtain the contents of the target request, using the given data,...
 
const QNetworkProxy & fallbackProxy() const
Returns the fallback proxy used by the manager.
 
static int timeout()
Returns the network timeout length, in milliseconds.
 
void setupDefaultProxyAndCache(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Setup the QgsNetworkAccessManager (NAM) according to the user's settings.
 
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...
 
void setFallbackProxyAndExcludes(const QNetworkProxy &proxy, const QStringList &excludes, const QStringList &noProxyURLs)
Sets the fallback proxy and URLs which shouldn't use it.
 
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 *)
 
QStringList excludeList() const
Returns the proxy exclude list.
 
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
 
void removeProxyFactory(QNetworkProxyFactory *factory)
Removes a factory from the proxy factories list.
 
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.
 
QNetworkReply * createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData=nullptr) override
 
virtual void handleAuthRequest(QNetworkReply *reply, QAuthenticator *auth)
Called whenever network authentication requests are encountered during a network reply.
 
virtual void handleAuthRequestCloseBrowser()
Called to terminate a network authentication through external browser.
 
virtual void handleAuthRequestOpenBrowser(const QUrl &url)
Called to initiate a network authentication through external browser url.
 
Wrapper implementation of QNetworkDiskCache with all methods guarded by a mutex soly for internal use...
 
void setCacheDirectory(const QString &cacheDir)
 
qint64 maximumCacheSize() const
 
void setMaximumCacheSize(qint64 size)
 
QString cacheDirectory() const
 
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.
 
QgsNetworkRequestParameters()=default
Default constructor.
 
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
 
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
 
An integer settings entry.
 
static QgsSettingsTreeNode * sTreeNetwork
 
This class is a composition of two QSettings instances:
 
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
 
virtual void handleSslErrors(QNetworkReply *reply, const QList< QSslError > &errors)
Called whenever SSL errors are encountered during a network reply.
 
#define Q_NOWARN_DEPRECATED_POP
 
#define Q_NOWARN_DEPRECATED_PUSH
 
#define QgsDebugMsgLevel(str, level)
 
#define QgsDebugError(str)