I would normalize the path before it gets to Flask, either using an HTTP server that hosts the WSGI container, or a proxy server that sits in front of your stack, or using the WSGI middleware.
The latter is easy to write:
import re from functools import partial class PathNormaliser(object): _collapse_slashes = partial(re.compile(r'/+').sub, r'/') def __init__(self, application): self.application = application def __call__(self, env, start_response): env['PATH_INFO'] = self._collapse_slashes(env['PATH_INFO']) return self.application(env, start_response)
You may need to register that you are applying this conversion, along with diagnostic information such as the REMOTE_HOST and HTTP_USER_AGENT entries. Personally, I would make this particular application generate non-broken URLs as soon as possible.
Take a look at your WSGI server documentation to learn how to add additional WSGI middleware components.
source share