Skip to content

Commit

Permalink
Merge pull request #36657 from mhugent/gdal_proxy
Browse files Browse the repository at this point in the history
Apply proxy settings both in ogr and gdal providers
  • Loading branch information
mhugent committed May 25, 2020
2 parents 4ddce03 + acdaaa4 commit 281fdf1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 55 deletions.
4 changes: 4 additions & 0 deletions src/core/providers/gdal/qgsgdalprovider.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ QgsGdalProvider::QgsGdalProvider( const QString &uri, const ProviderOptions &opt


QgsGdalProviderBase::registerGdalDrivers(); QgsGdalProviderBase::registerGdalDrivers();


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

if ( !CPLGetConfigOption( "AAIGRID_DATATYPE", nullptr ) ) if ( !CPLGetConfigOption( "AAIGRID_DATATYPE", nullptr ) )
{ {
// GDAL tends to open AAIGrid as Float32 which results in lost precision // GDAL tends to open AAIGrid as Float32 which results in lost precision
Expand Down
53 changes: 2 additions & 51 deletions src/core/providers/ogr/qgsogrprovider.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ email : sherman at mrcc.com
///@cond PRIVATE ///@cond PRIVATE


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


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


// make connection to the data source // make connection to the data source
Expand Down Expand Up @@ -2605,56 +2606,6 @@ bool QgsOgrProvider::doInitialActionsForEdition()
return true; 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 QgsVectorDataProvider::Capabilities QgsOgrProvider::capabilities() const
{ {
return mCapabilities; return mCapabilities;
Expand Down
4 changes: 0 additions & 4 deletions src/core/providers/ogr/qgsogrprovider.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ class QgsOgrProvider final: public QgsVectorDataProvider


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


#ifndef QT_NO_NETWORKPROXY
void setupProxy();
#endif

QgsOgrTransaction *mTransaction = nullptr; QgsOgrTransaction *mTransaction = nullptr;


void setTransaction( QgsTransaction *transaction ) override; void setTransaction( QgsTransaction *transaction ) override;
Expand Down
53 changes: 53 additions & 0 deletions src/core/qgsgdalutils.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@


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


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


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


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


return GDALAutoCreateWarpedVRTEx( hSrcDS, pszSrcWKT, pszDstWKT, eResampleAlg, dfMaxError, psOptionsIn, opts ); 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> proxies( QgsNetworkAccessManager::instance()->proxyFactory()->queryProxy( ) );
if ( ! proxies.isEmpty() )
{
QNetworkProxy proxy( proxies.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
5 changes: 5 additions & 0 deletions src/core/qgsgdalutils.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ class CORE_EXPORT QgsGdalUtils
double dfMaxError, double dfMaxError,
const GDALWarpOptions *psOptionsIn ); const GDALWarpOptions *psOptionsIn );


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

friend class TestQgsGdalUtils; friend class TestQgsGdalUtils;
}; };


Expand Down

0 comments on commit 281fdf1

Please sign in to comment.