From ceb0e29da307522b2d6509cc26341935c557aa0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 27 May 2015 14:30:41 +0200 Subject: [PATCH] Changed existing octoprint.server.http.* hooks to prefix routes with /plugin// This will hopefully prevent conflicting routes between multiple plugins from being registered and also ensures a more consistent behaviour compared to BlueprintPlugin mixins. Thanks to @Salandora for bringing this up. --- docs/plugins/hooks.rst | 15 +++++++++++++++ src/octoprint/server/__init__.py | 14 ++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index 7902b0ea..ce0cd7ad 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -285,6 +285,9 @@ octoprint.server.http.bodysize against which to match as first, a regular expression for the path to match against and the maximum body size as an integer as the third entry. + The path of the route will be prefixed by OctoPrint with ``/plugin//`` (if the path already begins + with a ``/`` that will be stripped first). + **Example** The following plugin example sets the maximum body size for ``POST`` requests against four custom URLs to 100, 200, @@ -320,6 +323,18 @@ octoprint.server.http.routes class to use for the route as the second and a dictionary with keywords parameters for the defined request handler as the third entry. + The path of the route will be prefixed by OctoPrint with ``/plugin//`` (if the path already begins + with a ``/`` that will be stripped first). + + .. note:: + + Static routes provided through this hook take precedence over routes defined through blueprints. + + If your plugin also implements the :class:`~octoprint.plugin.BlueprintPlugin` mixin and has defined a route for a + view on that which matches one of the paths provided via its ``octoprint.server.http.routes`` hook handler, the + view of the blueprint will thus not be reachable since processing of the request will directly be handed over + to your defined handler class. + **Example** The following example registers two new routes ``/plugin/add_tornado_route/download`` and ``/plugin/add_tornado_route/forward`` diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 495aca1d..8733a7df 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -785,8 +785,11 @@ class Server(): if not isinstance(entry[2], dict): continue - self._logger.debug("Adding additional route {route} handled by handler {handler} and with additional arguments {args!r}".format(route=entry[0], handler=entry[1], args=entry[2])) - server_routes.append(entry) + route, handler, kwargs = entry + route = r"/plugin/{name}/{route}".format(name=name, route=route if not route.startswith("/") else route[1:]) + + self._logger.debug("Adding additional route {route} handled by handler {handler} and with additional arguments {kwargs!r}".format(**locals())) + server_routes.append((route, handler, kwargs)) server_routes.append((r".*", util.tornado.UploadStorageFallbackHandler, dict(fallback=util.tornado.WsgiInputContainer(app.wsgi_app), file_prefix="octoprint-file-upload-", file_suffix=".tmp", suffixes=upload_suffixes))) @@ -811,8 +814,11 @@ class Server(): if not isinstance(entry[2], int): continue - self._logger.debug("Adding maximum body size of {size}B for {method} requests to {path} (Plugin: {name})".format(method=entry[0], path=entry[1], size=entry[2], name=name)) - max_body_sizes.append(entry) + method, route, size = entry + route = r"/plugin/{name}/{route}".format(name=name, route=route if not route.startswith("/") else route[1:]) + + self._logger.debug("Adding maximum body size of {size}B for {method} requests to {route})".format(**locals())) + max_body_sizes.append((method, route, size)) self._server = util.tornado.CustomHTTPServer(self._tornado_app, max_body_sizes=max_body_sizes, default_max_body_size=settings().getInt(["server", "maxSize"])) self._server.listen(self._port, address=self._host)