Skip to content
Permalink
Browse files

Apply proxy settings both in ogr and gdal providers

  • Loading branch information
mhugent committed May 22, 2020
1 parent 25e24e7 commit b35a2dff4cb44d4620acc4fba23e4ae8536bb008
@@ -160,6 +160,10 @@ QgsGdalProvider::QgsGdalProvider( const QString &uri, const ProviderOptions &opt

QgsGdalProviderBase::registerGdalDrivers();

#ifndef QT_NO_NETWORKPROXY
QgsGdalUtils::setupProxy();
#endif

if ( !CPLGetConfigOption( "AAIGRID_DATATYPE", nullptr ) )
{
// GDAL tends to open AAIGrid as Float32 which results in lost precision
@@ -19,6 +19,7 @@ email : sherman at mrcc.com
///@cond PRIVATE

#include "qgscplerrorhandler.h"
#include "qgsgdalutils.h"
#include "qgsogrfeatureiterator.h"
#include "qgslogger.h"
#include "qgsmessagelog.h"
@@ -493,7 +494,7 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
CPLSetConfigOption( "SHAPE_ENCODING", "" );

#ifndef QT_NO_NETWORKPROXY
setupProxy();
QgsGdalUtils::setupProxy();
#endif

// make connection to the data source
@@ -2605,56 +2606,6 @@ bool QgsOgrProvider::doInitialActionsForEdition()
return true;
}

#ifndef QT_NO_NETWORKPROXY
void QgsOgrProvider::setupProxy()
{
// Check proxy configuration, they are application level but
// instead of adding an API and complex signal/slot connections
// given the limited cost of checking them on every provider instantiation
// we can do it here so that new settings are applied whenever a new layer
// is created.
QgsSettings settings;
// Check that proxy is enabled
if ( settings.value( QStringLiteral( "proxy/proxyEnabled" ), false ).toBool() )
{
// Get the first configured proxy
QList<QNetworkProxy> proxyes( QgsNetworkAccessManager::instance()->proxyFactory()->queryProxy( ) );
if ( ! proxyes.isEmpty() )
{
QNetworkProxy proxy( proxyes.first() );
// TODO/FIXME: check excludes (the GDAL config options are global, we need a per-connection config option)
//QStringList excludes;
//excludes = settings.value( QStringLiteral( "proxy/proxyExcludedUrls" ), "" ).toStringList();

QString proxyHost( proxy.hostName() );
qint16 proxyPort( proxy.port() );

QString proxyUser( proxy.user() );
QString proxyPassword( proxy.password() );

if ( ! proxyHost.isEmpty() )
{
QString connection( proxyHost );
if ( proxyPort )
{
connection += ':' + QString::number( proxyPort );
}
CPLSetConfigOption( "GDAL_HTTP_PROXY", connection.toUtf8() );
if ( ! proxyUser.isEmpty( ) )
{
QString credentials( proxyUser );
if ( ! proxyPassword.isEmpty( ) )
{
credentials += ':' + proxyPassword;
}
CPLSetConfigOption( "GDAL_HTTP_PROXYUSERPWD", credentials.toUtf8() );
}
}
}
}
}
#endif

QgsVectorDataProvider::Capabilities QgsOgrProvider::capabilities() const
{
return mCapabilities;
@@ -321,10 +321,6 @@ class QgsOgrProvider final: public QgsVectorDataProvider

bool addAttributeOGRLevel( const QgsField &field, bool &ignoreErrorOut );

#ifndef QT_NO_NETWORKPROXY
void setupProxy();
#endif

QgsOgrTransaction *mTransaction = nullptr;

void setTransaction( QgsTransaction *transaction ) override;
@@ -15,12 +15,15 @@

#include "qgsgdalutils.h"
#include "qgslogger.h"
#include "qgsnetworkaccessmanager.h"
#include "qgssettings.h"

#define CPL_SUPRESS_CPLUSPLUS //#spellok
#include "gdal.h"
#include "gdalwarper.h"
#include "cpl_string.h"

#include <QNetworkProxy>
#include <QString>
#include <QImage>

@@ -486,3 +489,53 @@ GDALDatasetH QgsGdalUtils::rpcAwareAutoCreateWarpedVrt(

return GDALAutoCreateWarpedVRTEx( hSrcDS, pszSrcWKT, pszDstWKT, eResampleAlg, dfMaxError, psOptionsIn, opts );
}

#ifndef QT_NO_NETWORKPROXY
void QgsGdalUtils::setupProxy()
{
// Check proxy configuration, they are application level but
// instead of adding an API and complex signal/slot connections
// given the limited cost of checking them on every provider instantiation
// we can do it here so that new settings are applied whenever a new layer
// is created.
QgsSettings settings;
// Check that proxy is enabled
if ( settings.value( QStringLiteral( "proxy/proxyEnabled" ), false ).toBool() )
{
// Get the first configured proxy
QList<QNetworkProxy> proxyes( QgsNetworkAccessManager::instance()->proxyFactory()->queryProxy( ) );
if ( ! proxyes.isEmpty() )
{
QNetworkProxy proxy( proxyes.first() );
// TODO/FIXME: check excludes (the GDAL config options are global, we need a per-connection config option)
//QStringList excludes;
//excludes = settings.value( QStringLiteral( "proxy/proxyExcludedUrls" ), "" ).toStringList();

QString proxyHost( proxy.hostName() );
qint16 proxyPort( proxy.port() );

QString proxyUser( proxy.user() );
QString proxyPassword( proxy.password() );

if ( ! proxyHost.isEmpty() )
{
QString connection( proxyHost );
if ( proxyPort )
{
connection += ':' + QString::number( proxyPort );
}
CPLSetConfigOption( "GDAL_HTTP_PROXY", connection.toUtf8() );
if ( ! proxyUser.isEmpty( ) )
{
QString credentials( proxyUser );
if ( ! proxyPassword.isEmpty( ) )
{
credentials += ':' + proxyPassword;
}
CPLSetConfigOption( "GDAL_HTTP_PROXYUSERPWD", credentials.toUtf8() );
}
}
}
}
}
#endif
@@ -116,6 +116,11 @@ class CORE_EXPORT QgsGdalUtils
double dfMaxError,
const GDALWarpOptions *psOptionsIn );

#ifndef QT_NO_NETWORKPROXY
//! Sets the gdal proxy variables
static void setupProxy();
#endif

friend class TestQgsGdalUtils;
};

0 comments on commit b35a2df

Please sign in to comment.
You can’t perform that action at this time.