Skip to content

Commit dd18e8f

Browse files
authored
Merge pull request #9807 from qgis/backport-9773-to-release-3_4
[Backport release-3_4] [server] Allow server plugin filters to access config path
2 parents 960e10d + 894ea6a commit dd18e8f

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

src/server/qgsrequesthandler.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ void QgsRequestHandler::setParameter( const QString &key, const QString &value )
265265
{
266266
if ( !( key.isEmpty() || value.isEmpty() ) )
267267
{
268+
// Warn for potential breaking change if plugin set the MAP parameter
269+
// expecting changing the config file path, see PR #9773
270+
if ( key.compare( QLatin1String( "MAP" ), Qt::CaseInsensitive ) == 0 )
271+
{
272+
QgsMessageLog::logMessage( QStringLiteral( "Changing the 'MAP' parameter will have no effect on config path: use QgsSerververInterface::setConfigFilePath instead" ),
273+
"Server", Qgis::Warning );
274+
}
268275
mRequest.setParameter( key, value );
269276
}
270277
}

src/server/qgsserver.cpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,20 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
323323
// Set the request handler into the interface for plugins to manipulate it
324324
sServerInterface->setRequestHandler( &requestHandler );
325325

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

@@ -337,20 +351,12 @@ void QgsServer::handleRequest( QgsServerRequest &request, QgsServerResponse &res
337351
//Config file path
338352
if ( ! project )
339353
{
340-
QString configFilePath = configPath( *sConfigFilePath, params.map() );
341-
342354
// load the project if needed and not empty
343-
project = mConfigCache->project( configFilePath );
355+
project = mConfigCache->project( sServerInterface->configFilePath() );
344356
if ( ! project )
345357
{
346358
throw QgsServerException( QStringLiteral( "Project file error" ) );
347359
}
348-
349-
sServerInterface->setConfigFilePath( configFilePath );
350-
}
351-
else
352-
{
353-
sServerInterface->setConfigFilePath( project->fileName() );
354360
}
355361

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

tests/src/python/test_qgsserver_plugins.py

+45
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)