Skip to content

Commit

Permalink
webconnections saving as .xml: add a xmlns:x=y attribute when the htt…
Browse files Browse the repository at this point in the history
…p-header: prefix is used

Fixes #60242
  • Loading branch information
rouault authored and nyalldawson committed Jan 29, 2025
1 parent 7755764 commit d5edd08
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
12 changes: 10 additions & 2 deletions python/PyQt6/core/auto_generated/network/qgshttpheaders.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,22 @@ KEY_REFERER value will be available at key "KEY_PREFIX+KEY_REFERER" and key "KEY
:return: ``True`` if the update succeed
%End

bool updateDomElement( QDomElement &el ) const;
bool updateDomElement( QDomElement &el ) const /Deprecated="Since 3.42. Will be removed in QGIS 4.0."/;
%Docstring
Updates a ``map`` by adding all the HTTP headers
Updates a DOM element by adding all the HTTP headers

KEY_REFERER value will be available at attribute "KEY_PREFIX+KEY_REFERER" and attribute "KEY_REFERER" (for backward compatibility)

:param el: DOM element

:return: ``True`` if the update succeed

.. deprecated:: 3.42

Will be removed in QGIS 4.0.
%End


void setFromSettings( const QgsSettings &settings, const QString &key = QString() );
%Docstring
Loads headers from the ``settings``
Expand Down Expand Up @@ -185,6 +192,7 @@ Returns key/value pairs as strings separated by space

};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
12 changes: 10 additions & 2 deletions python/core/auto_generated/network/qgshttpheaders.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,22 @@ KEY_REFERER value will be available at key "KEY_PREFIX+KEY_REFERER" and key "KEY
:return: ``True`` if the update succeed
%End

bool updateDomElement( QDomElement &el ) const;
bool updateDomElement( QDomElement &el ) const /Deprecated="Since 3.42. Will be removed in QGIS 4.0."/;
%Docstring
Updates a ``map`` by adding all the HTTP headers
Updates a DOM element by adding all the HTTP headers

KEY_REFERER value will be available at attribute "KEY_PREFIX+KEY_REFERER" and attribute "KEY_REFERER" (for backward compatibility)

:param el: DOM element

:return: ``True`` if the update succeed

.. deprecated:: 3.42

Will be removed in QGIS 4.0.
%End


void setFromSettings( const QgsSettings &settings, const QString &key = QString() );
%Docstring
Loads headers from the ``settings``
Expand Down Expand Up @@ -185,6 +192,7 @@ Returns key/value pairs as strings separated by space

};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
10 changes: 10 additions & 0 deletions src/core/network/qgshttpheaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,18 @@ bool QgsHttpHeaders::updateMap( QVariantMap &map ) const

bool QgsHttpHeaders::updateDomElement( QDomElement &el ) const
{
QMap<QString, QString> namespaceDeclarations;
return updateDomElement( el, namespaceDeclarations );
}

bool QgsHttpHeaders::updateDomElement( QDomElement &el, QMap<QString, QString> &namespaceDeclarations ) const
{
QString httpHeaderURIPrefix( QgsHttpHeaders::PARAM_PREFIX );
httpHeaderURIPrefix.chop( 1 );

for ( auto ite = mHeaders.constBegin(); ite != mHeaders.constEnd(); ++ite )
{
namespaceDeclarations.insert( httpHeaderURIPrefix, QStringLiteral( "https://qgis.org/" ) + httpHeaderURIPrefix );
el.setAttribute( QgsHttpHeaders::PARAM_PREFIX + ite.key().toUtf8(), ite.value().toString() );
}

Expand Down
18 changes: 16 additions & 2 deletions src/core/network/qgshttpheaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,26 @@ class CORE_EXPORT QgsHttpHeaders
bool updateMap( QVariantMap &map ) const;

/**
* \brief Updates a \a map by adding all the HTTP headers
* \brief Updates a DOM element by adding all the HTTP headers
*
* KEY_REFERER value will be available at attribute "KEY_PREFIX+KEY_REFERER" and attribute "KEY_REFERER" (for backward compatibility)
*
* \param el DOM element
* \return TRUE if the update succeed
* \deprecated QGIS 3.42. Will be removed in QGIS 4.0.
*/
bool updateDomElement( QDomElement &el ) const;
Q_DECL_DEPRECATED bool updateDomElement( QDomElement &el ) const SIP_DEPRECATED;

/**
* \brief Updates a DOM element by adding all the HTTP headers
*
* KEY_REFERER value will be available at attribute "KEY_PREFIX+KEY_REFERER" and attribute "KEY_REFERER" (for backward compatibility)
*
* \param el DOM element
* \param[out] namespaceDeclarations Map of (prefix, URI) tuples for namespaces used by el.
* \return TRUE if the update succeed
*/
bool updateDomElement( QDomElement &el, QMap<QString, QString> &namespaceDeclarations ) const SIP_SKIP;

/**
* \brief Loads headers from the \a settings
Expand Down Expand Up @@ -211,3 +224,4 @@ class CORE_EXPORT QgsHttpHeaders
};

#endif // QGSHTTPHEADERS_H

43 changes: 36 additions & 7 deletions src/gui/qgsmanageconnectionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,22 @@ bool QgsManageConnectionsDialog::populateConnections()
return true;
}

static void addNamespaceDeclarations( QDomElement &root, const QMap<QString, QString> &namespaceDeclarations )
{
for ( auto it = namespaceDeclarations.begin(); it != namespaceDeclarations.end(); ++it )
{
root.setAttribute( QStringLiteral( "xmlns:" ) + it.key(), it.value() );
}
}

QDomDocument QgsManageConnectionsDialog::saveOWSConnections( const QStringList &connections, const QString &service )
{
QDomDocument doc( QStringLiteral( "connections" ) );
QDomElement root = doc.createElement( "qgs" + service.toUpper() + "Connections" );
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
doc.appendChild( root );

QMap<QString, QString> namespaceDeclarations;
for ( int i = 0; i < connections.count(); ++i )
{
QDomElement el = doc.createElement( service.toLower() );
Expand All @@ -484,14 +493,16 @@ QDomDocument QgsManageConnectionsDialog::saveOWSConnections( const QStringList &
el.setAttribute( QStringLiteral( "dpiMode" ), static_cast<int>( QgsOwsConnection::settingsDpiMode->value( { service.toLower(), connections[i] } ) ) );

QgsHttpHeaders httpHeader( QgsOwsConnection::settingsHeaders->value( { service.toLower(), connections[i] } ) );
httpHeader.updateDomElement( el );
httpHeader.updateDomElement( el, namespaceDeclarations );
}

el.setAttribute( QStringLiteral( "username" ), QgsOwsConnection::settingsUsername->value( { service.toLower(), connections[i] } ) );
el.setAttribute( QStringLiteral( "password" ), QgsOwsConnection::settingsPassword->value( { service.toLower(), connections[i] } ) );
root.appendChild( el );
}

addNamespaceDeclarations( root, namespaceDeclarations );

return doc;
}

Expand Down Expand Up @@ -710,6 +721,7 @@ QDomDocument QgsManageConnectionsDialog::saveXyzTilesConnections( const QStringL
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
doc.appendChild( root );

QMap<QString, QString> namespaceDeclarations;
for ( int i = 0; i < connections.count(); ++i )
{
QDomElement el = doc.createElement( QStringLiteral( "xyztiles" ) );
Expand All @@ -724,11 +736,13 @@ QDomDocument QgsManageConnectionsDialog::saveXyzTilesConnections( const QStringL
el.setAttribute( QStringLiteral( "tilePixelRatio" ), QgsXyzConnectionSettings::settingsTilePixelRatio->value( connections[i] ) );

QgsHttpHeaders httpHeader( QgsXyzConnectionSettings::settingsHeaders->value( connections[i] ) );
httpHeader.updateDomElement( el );
httpHeader.updateDomElement( el, namespaceDeclarations );

root.appendChild( el );
}

addNamespaceDeclarations( root, namespaceDeclarations );

return doc;
}

Expand All @@ -739,14 +753,15 @@ QDomDocument QgsManageConnectionsDialog::saveArcgisConnections( const QStringLis
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
doc.appendChild( root );

QMap<QString, QString> namespaceDeclarations;
for ( const QString &connection : connections )
{
QDomElement el = doc.createElement( QStringLiteral( "arcgisfeatureserver" ) );
el.setAttribute( QStringLiteral( "name" ), connection );
el.setAttribute( QStringLiteral( "url" ), QgsArcGisConnectionSettings::settingsUrl->value( connection ) );

QgsHttpHeaders httpHeader( QgsArcGisConnectionSettings::settingsHeaders->value( connection ) );
httpHeader.updateDomElement( el );
httpHeader.updateDomElement( el, namespaceDeclarations );

el.setAttribute( QStringLiteral( "username" ), QgsArcGisConnectionSettings::settingsUsername->value( connection ) );
el.setAttribute( QStringLiteral( "password" ), QgsArcGisConnectionSettings::settingsPassword->value( connection ) );
Expand All @@ -755,6 +770,8 @@ QDomDocument QgsManageConnectionsDialog::saveArcgisConnections( const QStringLis
root.appendChild( el );
}

addNamespaceDeclarations( root, namespaceDeclarations );

return doc;
}

Expand All @@ -765,6 +782,7 @@ QDomDocument QgsManageConnectionsDialog::saveVectorTileConnections( const QStrin
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
doc.appendChild( root );

QMap<QString, QString> namespaceDeclarations;
for ( int i = 0; i < connections.count(); ++i )
{
QDomElement el = doc.createElement( QStringLiteral( "vectortile" ) );
Expand All @@ -780,11 +798,13 @@ QDomDocument QgsManageConnectionsDialog::saveVectorTileConnections( const QStrin
el.setAttribute( QStringLiteral( "styleUrl" ), QgsVectorTileProviderConnection::settingsStyleUrl->value( connections[i] ) );

QgsHttpHeaders httpHeader( QgsVectorTileProviderConnection::settingsHeaders->value( connections[i] ) );
httpHeader.updateDomElement( el );
httpHeader.updateDomElement( el, namespaceDeclarations );

root.appendChild( el );
}

addNamespaceDeclarations( root, namespaceDeclarations );

return doc;
}

Expand All @@ -795,6 +815,7 @@ QDomDocument QgsManageConnectionsDialog::saveTiledSceneConnections( const QStrin
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
doc.appendChild( root );

QMap<QString, QString> namespaceDeclarations;
for ( int i = 0; i < connections.count(); ++i )
{
QDomElement el = doc.createElement( QStringLiteral( "tiledscene" ) );
Expand All @@ -807,11 +828,13 @@ QDomDocument QgsManageConnectionsDialog::saveTiledSceneConnections( const QStrin
el.setAttribute( QStringLiteral( "password" ), QgsTiledSceneProviderConnection::settingsPassword->value( connections[i] ) );

QgsHttpHeaders httpHeader( QgsTiledSceneProviderConnection::settingsHeaders->value( connections[i] ) );
httpHeader.updateDomElement( el );
httpHeader.updateDomElement( el, namespaceDeclarations );

root.appendChild( el );
}

addNamespaceDeclarations( root, namespaceDeclarations );

return doc;
}

Expand All @@ -822,6 +845,7 @@ QDomDocument QgsManageConnectionsDialog::saveSensorThingsConnections( const QStr
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
doc.appendChild( root );

QMap<QString, QString> namespaceDeclarations;
for ( int i = 0; i < connections.count(); ++i )
{
QDomElement el = doc.createElement( QStringLiteral( "sensorthings" ) );
Expand All @@ -833,11 +857,13 @@ QDomDocument QgsManageConnectionsDialog::saveSensorThingsConnections( const QStr
el.setAttribute( QStringLiteral( "password" ), QgsSensorThingsProviderConnection::settingsPassword->value( connections[i] ) );

QgsHttpHeaders httpHeader( QgsTiledSceneProviderConnection::settingsHeaders->value( connections[i] ) );
httpHeader.updateDomElement( el );
httpHeader.updateDomElement( el, namespaceDeclarations );

root.appendChild( el );
}

addNamespaceDeclarations( root, namespaceDeclarations );

return doc;
}

Expand Down Expand Up @@ -882,6 +908,7 @@ QDomDocument QgsManageConnectionsDialog::saveStacConnections( const QStringList
root.setAttribute( QStringLiteral( "version" ), QStringLiteral( "1.0" ) );
doc.appendChild( root );

QMap<QString, QString> namespaceDeclarations;
for ( int i = 0; i < connections.count(); ++i )
{
QDomElement el = doc.createElement( QStringLiteral( "stac" ) );
Expand All @@ -893,11 +920,13 @@ QDomDocument QgsManageConnectionsDialog::saveStacConnections( const QStringList
el.setAttribute( QStringLiteral( "password" ), QgsStacConnection::settingsPassword->value( connections[i] ) );

QgsHttpHeaders httpHeader( QgsStacConnection::settingsHeaders->value( connections[i] ) );
httpHeader.updateDomElement( el );
httpHeader.updateDomElement( el, namespaceDeclarations );

root.appendChild( el );
}

addNamespaceDeclarations( root, namespaceDeclarations );

return doc;
}

Expand Down
5 changes: 4 additions & 1 deletion tests/src/core/testqgshttpheaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ void TestQgsHttpheaders::updateSetDomElement()
QDomElement element = doc.createElement( "qgs" );
// === update
QgsHttpHeaders h( QVariantMap( { { QStringLiteral( "key1" ), "value1" }, { QgsHttpHeaders::KEY_REFERER, "my_ref" } } ) );
h.updateDomElement( element );
QMap<QString, QString> namespaceDeclarations;
h.updateDomElement( element, namespaceDeclarations );

QVERIFY( element.hasAttribute( QgsHttpHeaders::PARAM_PREFIX + "key1" ) );
QCOMPARE( element.attribute( QgsHttpHeaders::PARAM_PREFIX + "key1" ), "value1" );
Expand All @@ -284,6 +285,8 @@ void TestQgsHttpheaders::updateSetDomElement()
QVERIFY( element.hasAttribute( QgsHttpHeaders::KEY_REFERER ) );
QCOMPARE( element.attribute( QgsHttpHeaders::KEY_REFERER ), "my_ref" );

QCOMPARE( namespaceDeclarations["http-header"], "https://qgis.org/http-header" );

// === setFrom
QgsHttpHeaders h2;
element.setAttribute( QgsHttpHeaders::KEY_REFERER, "my_ref_root" ); // overwrite root ref to ckeck backward compatibility
Expand Down

0 comments on commit d5edd08

Please sign in to comment.