summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJen Montes <jen@jenmontes.com>2015-02-18 18:21:09 -0500
committerJen Montes <jen@jenmontes.com>2015-02-18 18:21:09 -0500
commit2fdaa8b4c283030fc7ab454d75e54ce430c89b7c (patch)
tree2f7df483dba8a924dce457b61ac4933130e643ce
parent854fbd7f88aa2f809f54dd724aea7ecf918a3b6e (diff)
downloadbottle-2fdaa8b4c283030fc7ab454d75e54ce430c89b7c.tar.gz
Added another recipe for removing trailing slashes
This one is using a `before_request` hook to strip the trailing slash from `request.environ['PATH_INFO']`. I prefer it to the WSGI middleware method because it leaves the API of the app intact. For example, in the `StripPathMiddleware` snippet, you can't later add a route to `myapp` because you'll get the following error: `AttributeError: 'StripPathMiddleware' object has no attribute 'route'`.
-rwxr-xr-xdocs/recipes.rst8
1 files changed, 7 insertions, 1 deletions
diff --git a/docs/recipes.rst b/docs/recipes.rst
index 8c57706..54f9684 100755
--- a/docs/recipes.rst
+++ b/docs/recipes.rst
@@ -139,7 +139,7 @@ For Bottle, ``/example`` and ``/example/`` are two different routes [1]_. To tre
@route('/test/')
def test(): return 'Slash? no?'
-or add a WSGI middleware that strips trailing slashes from all URLs::
+add a WSGI middleware that strips trailing slashes from all URLs::
class StripPathMiddleware(object):
def __init__(self, app):
@@ -152,6 +152,12 @@ or add a WSGI middleware that strips trailing slashes from all URLs::
myapp = StripPathMiddleware(app)
bottle.run(app=myapp)
+or add a ``before_request`` hook to strip the trailing slashes::
+
+ @hook('before_request')
+ def strip_path():
+ request.environ['PATH_INFO'] = request.environ['PATH_INFO'].rstrip('/')
+
.. rubric:: Footnotes
.. [1] Because they are. See <http://www.ietf.org/rfc/rfc3986.txt>