Skip to content
Permalink
Browse files

Merge pull request #5596 from boundlessgeo/bugfix-5212-ogr-proxy

[bugfix] Apply proxy configuration to OGR connections
  • Loading branch information
jef-n committed Nov 10, 2017
2 parents f8cbc42 + f2abcf2 commit 2ca4ee5ba80328bc3bb357b2a2a4b39c0d36c60c
@@ -66,7 +66,7 @@ class QgsNetworkProxyFactory : public QNetworkProxyFactory
return proxies;
}

// no proxies from the proxy factor list check for excludes
// no proxies from the proxy factory list check for excludes
if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
return QList<QNetworkProxy>() << nam->fallbackProxy();

@@ -364,16 +364,15 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
);
proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
}
}

// Setup network proxy authentication configuration
QString authcfg = settings.value( QStringLiteral( "proxy/authcfg" ), "" ).toString();
if ( !authcfg.isEmpty( ) )
{
QgsDebugMsg( QStringLiteral( "setting proxy from stored authentication configuration %1" ).arg( authcfg ) );
// Never crash! Never.
if ( QgsApplication::authManager() )
QgsApplication::authManager()->updateNetworkProxy( proxy, authcfg );
// Setup network proxy authentication configuration
QString authcfg = settings.value( QStringLiteral( "proxy/authcfg" ), "" ).toString();
if ( !authcfg.isEmpty( ) )
{
QgsDebugMsg( QStringLiteral( "setting proxy from stored authentication configuration %1" ).arg( authcfg ) );
// Never crash! Never.
if ( QgsApplication::authManager() )
QgsApplication::authManager()->updateNetworkProxy( proxy, authcfg );
}
}

setFallbackProxyAndExcludes( proxy, excludes );
@@ -1,4 +1,3 @@

SET (OGR_SRCS
qgsogrprovider.cpp
qgsogrdataitems.cpp
@@ -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
@@ -54,6 +55,8 @@ INCLUDE_DIRECTORIES(
INCLUDE_DIRECTORIES(SYSTEM
${GDAL_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
${QTKEYCHAIN_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
${QSCINTILLA_INCLUDE_DIR}
)

@@ -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})
@@ -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"
@@ -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"
@@ -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 + ']' );
@@ -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 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" ), "" ).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;
@@ -266,6 +266,11 @@ class QgsOgrProvider : public QgsVectorDataProvider
QgsVectorDataProvider::Capabilities mCapabilities;

bool doInitialActionsForEdition();

#ifndef QT_NO_NETWORKPROXY
void setupProxy();
#endif

};

/**
@@ -66,6 +66,8 @@ SET_TARGET_PROPERTIES(qgis_wcsprovidertest PROPERTIES

ADD_QGIS_TEST(gdalprovidertest testqgsgdalprovider.cpp)

ADD_QGIS_TEST(ogrprovidertest testqgsogrprovider.cpp)

ADD_QGIS_TEST(wmscapabilititestest
testqgswmscapabilities.cpp)
TARGET_LINK_LIBRARIES(qgis_wmscapabilititestest wmsprovider_a)
@@ -0,0 +1,124 @@
/***************************************************************************
testqgsogrprovider.cpp - TestQgsOgrProvider
---------------------
begin : 10.11.2017
copyright : (C) 2017 by Alessandro Pasotti
email : apasotti at boundlessgeo dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgstest.h"

//qgis includes...
#include <qgis.h>
#include <qgssettings.h>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsvectorlayer.h>
#include <qgsnetworkaccessmanager.h>

#include <QObject>

#include <cpl_conv.h>


/**
* \ingroup UnitTests
* This is a unit test for the ogr provider
*/
class TestQgsOgrProvider : public QObject
{
Q_OBJECT

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {}// will be called before each testfunction is executed.
void cleanup() {}// will be called after every testfunction.

void setupProxy();

private:
QString mTestDataDir;
QString mReport;
signals:

public slots:
};



//runs before all tests
void TestQgsOgrProvider::initTestCase()
{
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();

mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
mReport = QStringLiteral( "<h1>OGR Provider Tests</h1>\n" );
}

//runs after all tests
void TestQgsOgrProvider::cleanupTestCase()
{
QgsApplication::exitQgis();
QString myReportFile = QDir::tempPath() + "/qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}

void TestQgsOgrProvider::setupProxy()
{

QgsSettings settings;
{
settings.setValue( QStringLiteral( "proxy/proxyEnabled" ), true );
settings.setValue( QStringLiteral( "proxy/proxyPort" ), QStringLiteral( "1234" ) );
settings.setValue( QStringLiteral( "proxy/proxyHost" ), QStringLiteral( "myproxyhostname.com" ) );
settings.setValue( QStringLiteral( "proxy/proxyUser" ), QStringLiteral( "username" ) );
settings.setValue( QStringLiteral( "proxy/proxyPassword" ), QStringLiteral( "password" ) );
settings.setValue( QStringLiteral( "proxy/proxyExcludedUrls" ), QStringLiteral( "http://www.myhost.com|http://www.myotherhost.com" ) );
QgsNetworkAccessManager::instance()->setupDefaultProxyAndCache();
QgsVectorLayer vl( mTestDataDir + '/' + QStringLiteral( "lines.shp" ), QStringLiteral( "proxy_test" ), QLatin1Literal( "ogr" ) );
QVERIFY( vl.isValid() );
const char *proxyConfig = CPLGetConfigOption( "GDAL_HTTP_PROXY", NULL );
QCOMPARE( proxyConfig, "myproxyhostname.com:1234" );
const char *proxyCredentials = CPLGetConfigOption( "GDAL_HTTP_PROXYUSERPWD", NULL );
QCOMPARE( proxyCredentials, "username:password" );
}

{
// Test partial config
settings.setValue( QStringLiteral( "proxy/proxyEnabled" ), true );
settings.remove( QStringLiteral( "proxy/proxyPort" ) );
settings.setValue( QStringLiteral( "proxy/proxyHost" ), QStringLiteral( "myproxyhostname.com" ) );
settings.setValue( QStringLiteral( "proxy/proxyUser" ), QStringLiteral( "username" ) );
settings.remove( QStringLiteral( "proxy/proxyPassword" ) );
QgsNetworkAccessManager::instance()->setupDefaultProxyAndCache();
QgsVectorLayer vl( mTestDataDir + '/' + QStringLiteral( "lines.shp" ), QStringLiteral( "proxy_test" ), QLatin1Literal( "ogr" ) );
QVERIFY( vl.isValid() );
const char *proxyConfig = CPLGetConfigOption( "GDAL_HTTP_PROXY", NULL );
QCOMPARE( proxyConfig, "myproxyhostname.com" );
const char *proxyCredentials = CPLGetConfigOption( "GDAL_HTTP_PROXYUSERPWD", NULL );
QCOMPARE( proxyCredentials, "username" );
}

}


QGSTEST_MAIN( TestQgsOgrProvider )
#include "testqgsogrprovider.moc"
@@ -17,13 +17,12 @@
import sys
import tempfile

from qgis.core import QgsVectorLayer, QgsVectorDataProvider, QgsWkbTypes, QgsFeature, QgsFeatureRequest
from qgis.testing import (
start_app,
unittest
)
from utilities import unitTestDataPath
from osgeo import gdal, ogr # NOQA
from qgis.core import (QgsFeature, QgsFeatureRequest, QgsSettings,
QgsVectorDataProvider, QgsVectorLayer, QgsWkbTypes, QgsNetworkAccessManager)
from qgis.testing import start_app, unittest

from utilities import unitTestDataPath

start_app()
TEST_DATA_DIR = unitTestDataPath()
@@ -292,6 +291,33 @@ def testTriangleTINPolyhedralSurface(self):
Triangle => mapped to Polygon
"""

def testSetupProxy(self):
"""Test proxy setup"""
settings = QgsSettings()
settings.setValue("proxy/proxyEnabled", True)
settings.setValue("proxy/proxyPort", '1234')
settings.setValue("proxy/proxyHost", 'myproxyhostname.com')
settings.setValue("proxy/proxyUser", 'username')
settings.setValue("proxy/proxyPassword", 'password')
settings.setValue("proxy/proxyExcludedUrls", "http://www.myhost.com|http://www.myotherhost.com")
QgsNetworkAccessManager.instance().setupDefaultProxyAndCache()
vl = QgsVectorLayer(TEST_DATA_DIR + '/' + 'lines.shp', 'proxy_test', 'ogr')
self.assertTrue(vl.isValid())
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXY"), "myproxyhostname.com:1234")
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXYUSERPWD"), "username:password")

settings.setValue("proxy/proxyEnabled", True)
settings.remove("proxy/proxyPort")
settings.setValue("proxy/proxyHost", 'myproxyhostname.com')
settings.setValue("proxy/proxyUser", 'username')
settings.remove("proxy/proxyPassword")
settings.setValue("proxy/proxyExcludedUrls", "http://www.myhost.com|http://www.myotherhost.com")
QgsNetworkAccessManager.instance().setupDefaultProxyAndCache()
vl = QgsVectorLayer(TEST_DATA_DIR + '/' + 'lines.shp', 'proxy_test', 'ogr')
self.assertTrue(vl.isValid())
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXY"), "myproxyhostname.com")
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXYUSERPWD"), "username")


if __name__ == '__main__':
unittest.main()

0 comments on commit 2ca4ee5

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