Skip to content

Commit

Permalink
[bugfix] Apply proxy configuration to OGR connections
Browse files Browse the repository at this point in the history
Fixes #5212 Proxy settings ignored for layers
  • Loading branch information
elpaso committed Nov 10, 2017
1 parent d8a3c67 commit d8fc6e8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/providers/ogr/CMakeLists.txt
@@ -1,4 +1,3 @@

SET (OGR_SRCS
qgsogrprovider.cpp
qgsogrdataitems.cpp
Expand Down Expand Up @@ -40,12 +39,14 @@ QT5_WRAP_CPP(OGR_MOC_SRCS ${OGR_MOC_HDRS})

INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}/src/core
${CMAKE_SOURCE_DIR}/src/core/auth
${CMAKE_SOURCE_DIR}/src/core/raster
${CMAKE_SOURCE_DIR}/src/core/geometry
${CMAKE_SOURCE_DIR}/src/core/metadata
${CMAKE_SOURCE_DIR}/src/core/symbology
${CMAKE_SOURCE_DIR}/src/core/expression
${CMAKE_SOURCE_DIR}/src/gui
${CMAKE_SOURCE_DIR}/src/gui/auth

${CMAKE_BINARY_DIR}/src/core
${CMAKE_BINARY_DIR}/src/gui
Expand All @@ -54,6 +55,8 @@ INCLUDE_DIRECTORIES(
INCLUDE_DIRECTORIES(SYSTEM
${GDAL_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
${QTKEYCHAIN_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
${QSCINTILLA_INCLUDE_DIR}
)

Expand Down Expand Up @@ -86,5 +89,4 @@ ENDIF(CLANG_TIDY_EXE)

INSTALL (TARGETS ogrprovider
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})

LIBRARY DESTINATION ${QGIS_PLUGIN_DIR})
56 changes: 56 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -24,6 +24,7 @@ email : sherman at mrcc.com
#include "qgsfeedback.h"
#include "qgssettings.h"
#include "qgsapplication.h"
#include "qgsauthmanager.h"
#include "qgsdataitem.h"
#include "qgsdataprovider.h"
#include "qgsfeature.h"
Expand All @@ -35,6 +36,7 @@ email : sherman at mrcc.com
#include "qgsogrdataitems.h"
#include "qgsgeopackagedataitems.h"
#include "qgswkbtypes.h"
#include "qgsnetworkaccessmanager.h"

#ifdef HAVE_GUI
#include "qgssourceselectprovider.h"
Expand Down Expand Up @@ -419,6 +421,10 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri )
QgsSettings settings;
CPLSetConfigOption( "SHAPE_ENCODING", settings.value( QStringLiteral( "qgis/ignoreShapeEncoding" ), true ).toBool() ? "" : nullptr );
#ifndef QT_NO_NETWORKPROXY
setupProxy();
#endif
// make connection to the data source
QgsDebugMsg( "Data source uri is [" + uri + ']' );
Expand Down Expand Up @@ -1972,6 +1978,56 @@ 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 instanciation
// 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" ), "" ).toString().split( '|', QString::SkipEmptyParts );

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;
Expand Down
5 changes: 5 additions & 0 deletions src/providers/ogr/qgsogrprovider.h
Expand Up @@ -266,6 +266,11 @@ class QgsOgrProvider : public QgsVectorDataProvider
QgsVectorDataProvider::Capabilities mCapabilities;

bool doInitialActionsForEdition();

#ifndef QT_NO_NETWORKPROXY
void setupProxy();
#endif

};

/**
Expand Down

0 comments on commit d8fc6e8

Please sign in to comment.