http: Enforce router's URL namespace

This commit is contained in:
Stein Magnus Jodal 2014-05-20 23:53:20 +02:00
parent 4b383c1762
commit fe9a126a2a
3 changed files with 15 additions and 11 deletions

View File

@ -98,7 +98,8 @@ class Router(object):
server side functionality, this method must be overridden.
Must return a list of request handlers compatible with
:class:`tornado.web.Application`.
:class:`tornado.web.Application`. The URL patterns should not include
the :attr:`name` prefix, as that will be prepended by the web server.
"""
if self.static_file_path is None:
raise ValueError('Undefined static file path in %s' % self)
@ -108,7 +109,7 @@ class Router(object):
logger.info(
'Serving HTTP extension %s at %s', type(self), self.get_root_url())
return [
(r'/%s/(.*)' % self.name, StaticFileHandler, {
(r'/(.*)', StaticFileHandler, {
'path': self.static_file_path,
'default_filename': 'index.html'
}),

View File

@ -79,13 +79,16 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
return request_handlers
def _get_extension_request_handlers(self):
request_handlers = []
result = []
for router_class in self.routers:
router = router_class(self.config, self.core)
request_handlers.extend(router.get_request_handlers())
logger.info(
'Loaded HTTP extension: %s', router_class.__name__)
return request_handlers
request_handlers = router.get_request_handlers()
for handler in request_handlers:
handler = list(handler)
handler[0] = '/%s%s' % (router.name, handler[0])
result.append(tuple(handler))
logger.info('Loaded HTTP extension: %s', router_class.__name__)
return result
def _publish_zeroconf(self):
if not self.zeroconf_name:
@ -126,9 +129,9 @@ class MopidyHttpRouter(http.Router):
def get_request_handlers(self):
return [
(r'/mopidy/ws/?', handlers.WebSocketHandler, {'core': self.core}),
(r'/mopidy/rpc', handlers.JsonRpcHandler, {'core': self.core}),
(r'/mopidy/(.*)', handlers.StaticFileHandler, {
(r'/ws/?', handlers.WebSocketHandler, {'core': self.core}),
(r'/rpc', handlers.JsonRpcHandler, {'core': self.core}),
(r'/(.*)', handlers.StaticFileHandler, {
'path': mopidy_data_dir, 'default_filename': 'mopidy.html'
}),
]

View File

@ -45,7 +45,7 @@ class HttpRouterTest(unittest.TestCase):
(pattern, handler_class, kwargs) = router.get_request_handlers()[0]
self.assertEqual(pattern, r'/test/(.*)')
self.assertEqual(pattern, r'/(.*)')
self.assertIs(handler_class, handlers.StaticFileHandler)
self.assertEqual(
kwargs['path'], os.path.join(os.path.dirname(__file__), 'static'))