Skip to content
Permalink
Browse files
[ogr] Fix authcfg support for remote URLs
  • Loading branch information
nirvn authored and nyalldawson committed Nov 10, 2021
1 parent 82f1ebf commit 7cec30746e2278376d7a783b399779c110734d71
Showing with 24 additions and 8 deletions.
  1. +6 −4 src/core/providers/ogr/qgsogrprovider.cpp
  2. +18 −4 src/core/providers/ogr/qgsogrprovidermetadata.cpp
@@ -391,13 +391,15 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
mOgrGeometryTypeFilter,
mOpenOptions );

if ( mFilePath.contains( QLatin1String( "authcfg" ) ) )
if ( uri.contains( QLatin1String( "authcfg" ) ) )
{
QRegularExpression authcfgRe( QStringLiteral( " authcfg='([^']+)'" ) );
QRegularExpressionMatch match;
if ( mFilePath.contains( authcfgRe, &match ) )
if ( uri.contains( authcfgRe, &match ) )
{
mAuthCfg = match.captured( 1 );
// momentarily re-add authcfg since it was stripped off in analyzeURI
mFilePath = QgsOgrProviderUtils::expandAuthConfig( QStringLiteral( "%1 authcfg='%2'" ).arg( mFilePath, mAuthCfg ) );
}
}
QgsCPLHTTPFetchOverrider oCPLHTTPFetcher( mAuthCfg );
@@ -3419,8 +3421,8 @@ void QgsOgrProvider::open( OpenMode mode )

// Try to open using VSIFileHandler
// see http://trac.osgeo.org/gdal/wiki/UserDocs/ReadInZip
QString vsiPrefix = QgsZipItem::vsiPrefix( dataSourceUri() );
if ( !vsiPrefix.isEmpty() )
QString vsiPrefix = QgsZipItem::vsiPrefix( dataSourceUri( true ) );
if ( !vsiPrefix.isEmpty() || mFilePath.startsWith( QStringLiteral( "/vsicurl/" ) ) )
{
// GDAL>=1.8.0 has write support for zip, but read and write operations
// cannot be interleaved, so for now just use read-only.
@@ -74,9 +74,18 @@ QVariantMap QgsOgrProviderMetadata::decodeUri( const QString &uri ) const
QString geometryType;
QStringList openOptions;
QString databaseName;
QString authcfg;

int layerId = -1;

const QRegularExpression authcfgRegex( " authcfg='([^']+)'" );
QRegularExpressionMatch match;
if ( path.contains( authcfgRegex, &match ) )
{
path = path.remove( match.capturedStart( 0 ), match.capturedLength( 0 ) );
authcfg = match.captured( 1 );
}

QString vsiPrefix = qgsVsiPrefix( path );
QString vsiSuffix;
if ( path.startsWith( vsiPrefix, Qt::CaseInsensitive ) )
@@ -107,7 +116,7 @@ QVariantMap QgsOgrProviderMetadata::decodeUri( const QString &uri ) const

// we first try to split off the geometry type component, if that's present. That's a known quantity which
// will never be more than a-z characters
QRegularExpressionMatch match = geometryTypeRegex.match( path );
match = geometryTypeRegex.match( path );
if ( match.hasMatch() )
{
geometryType = match.captured( 1 );
@@ -195,6 +204,8 @@ QVariantMap QgsOgrProviderMetadata::decodeUri( const QString &uri ) const
uriComponents.insert( QStringLiteral( "vsiPrefix" ), vsiPrefix );
if ( !vsiSuffix.isEmpty() )
uriComponents.insert( QStringLiteral( "vsiSuffix" ), vsiSuffix );
if ( !authcfg.isEmpty() )
uriComponents.insert( QStringLiteral( "authcfg" ), authcfg );
return uriComponents;
}

@@ -207,7 +218,9 @@ QString QgsOgrProviderMetadata::encodeUri( const QVariantMap &parts ) const
const QString layerId = parts.value( QStringLiteral( "layerId" ) ).toString();
const QString subset = parts.value( QStringLiteral( "subset" ) ).toString();
const QString geometryType = parts.value( QStringLiteral( "geometryType" ) ).toString();
const QString authcfg = parts.value( QStringLiteral( "authcfg" ) ).toString();
const QStringList openOptions = parts.value( QStringLiteral( "openOptions" ) ).toStringList();

QString uri = vsiPrefix + path + vsiSuffix
+ ( !layerName.isEmpty() ? QStringLiteral( "|layername=%1" ).arg( layerName ) : !layerId.isEmpty() ? QStringLiteral( "|layerid=%1" ).arg( layerId ) : QString() )
+ ( !geometryType.isEmpty() ? QStringLiteral( "|geometrytype=%1" ).arg( geometryType ) : QString() );
@@ -218,6 +231,8 @@ QString QgsOgrProviderMetadata::encodeUri( const QVariantMap &parts ) const
}
if ( !subset.isEmpty() )
uri += QStringLiteral( "|subset=%1" ).arg( subset );
if ( !authcfg.isEmpty() )
uri += QStringLiteral( " authcfg='%1'" ).arg( authcfg );
return uri;
}

@@ -1040,13 +1055,12 @@ bool QgsOgrProviderMetadata::uriIsBlocklisted( const QString &uri ) const

QList<QgsProviderSublayerDetails> QgsOgrProviderMetadata::querySublayers( const QString &u, Qgis::SublayerQueryFlags flags, QgsFeedback *feedback ) const
{
QString uri = u;
QString uri = QgsOgrProviderUtils::expandAuthConfig( u );
QStringList options { QStringLiteral( "@LIST_ALL_TABLES=YES" ) };

QVariantMap uriParts = decodeUri( uri );

// Try to open using VSIFileHandler
QString vsiPrefix = QgsZipItem::vsiPrefix( uri );
QString vsiPrefix = QgsZipItem::vsiPrefix( uriParts.value( QStringLiteral( "path" ) ).toString() );
if ( !vsiPrefix.isEmpty() && uriParts.value( QStringLiteral( "vsiPrefix" ) ).toString().isEmpty() )
{
if ( !uri.startsWith( vsiPrefix ) )

0 comments on commit 7cec307

Please sign in to comment.