Skip to content

Commit c1a76c4

Browse files
authoredApr 18, 2019
Merge pull request #9808 from qgis/backport-9773-to-release-3_6
[Backport release-3_6] [server] Allow server plugin filters to access config path
2 parents da7f655 + e197d0f commit c1a76c4

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed
 

‎src/server/qgsrequesthandler.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ void QgsRequestHandler::setParameter( const QString &key, const QString &value )
259259
{
260260
if ( !( key.isEmpty() || value.isEmpty() ) )
261261
{
262+
// Warn for potential breaking change if plugin set the MAP parameter
263+
// expecting changing the config file path, see PR #9773
264+
if ( key.compare( QLatin1String( "MAP" ), Qt::CaseInsensitive ) == 0 )
265+
{
266+
QgsMessageLog::logMessage( QStringLiteral( "Changing the 'MAP' parameter will have no effect on config path: use QgsSerververInterface::setConfigFilePath instead" ),
267+
"Server", Qgis::Warning );
268+
}
262269
mRequest.setParameter( key, value );
263270
}
264271
}

‎src/server/qgsserver.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,20 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
319319
// Set the request handler into the interface for plugins to manipulate it
320320
sServerInterface->setRequestHandler( &requestHandler );
321321

322+
// Initialize configfilepath so that is is available
323+
// before calling plugin methods
324+
// Note that plugins may still change that value using
325+
// setConfigFilePath() interface method
326+
if ( ! project )
327+
{
328+
QString configFilePath = configPath( *sConfigFilePath, request.serverParameters().map() );
329+
sServerInterface->setConfigFilePath( configFilePath );
330+
}
331+
else
332+
{
333+
sServerInterface->setConfigFilePath( project->fileName() );
334+
}
335+
322336
// Call requestReady() method (if enabled)
323337
responseDecorator.start();
324338

@@ -333,20 +347,12 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
333347
//Config file path
334348
if ( ! project )
335349
{
336-
QString configFilePath = configPath( *sConfigFilePath, params.map() );
337-
338350
// load the project if needed and not empty
339-
project = mConfigCache->project( configFilePath );
351+
project = mConfigCache->project( sServerInterface->configFilePath() );
340352
if ( ! project )
341353
{
342354
throw QgsServerException( QStringLiteral( "Project file error" ) );
343355
}
344-
345-
sServerInterface->setConfigFilePath( configFilePath );
346-
}
347-
else
348-
{
349-
sServerInterface->setConfigFilePath( project->fileName() );
350356
}
351357

352358
if ( ! params.fileName().isEmpty() )

‎tests/src/python/test_qgsserver_plugins.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,51 @@ def responseComplete(self):
177177
self.assertEqual(response, expected)
178178
self.assertEqual(headers2, {'Content-type': 'text/plain'})
179179

180+
def test_configpath(self):
181+
""" Test plugin can read confif path
182+
"""
183+
try:
184+
from qgis.server import QgsServerFilter
185+
from qgis.core import QgsProject
186+
except ImportError:
187+
print("QGIS Server plugins are not compiled. Skipping test")
188+
return
189+
190+
d = unitTestDataPath('qgis_server_accesscontrol') + '/'
191+
self.projectPath = os.path.join(d, "project.qgs")
192+
self.server = QgsServer()
193+
194+
# global to be modified inside plugin filters
195+
globals()['configFilePath2'] = None
196+
197+
class Filter0(QgsServerFilter):
198+
"""Body setter, clear body, keep headers"""
199+
200+
def requestReady(self):
201+
global configFilePath2
202+
configFilePath2 = self.serverInterface().configFilePath()
203+
204+
serverIface = self.server.serverInterface()
205+
serverIface.registerFilter(Filter0(serverIface), 100)
206+
207+
# Test using MAP
208+
self._execute_request('?service=simple&MAP=%s' % self.projectPath)
209+
210+
# Check config file path
211+
self.assertEqual(configFilePath2, self.projectPath)
212+
213+
# Reset result
214+
globals()['configFilePath2'] = None
215+
216+
# Test with prqject as argument
217+
project = QgsProject()
218+
project.read(self.projectPath)
219+
220+
self._execute_request_project('?service=simple', project=project)
221+
222+
# Check config file path
223+
self.assertEqual(configFilePath2, project.fileName())
224+
180225

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

0 commit comments

Comments
 (0)