Skip to content

Instantly share code, notes, and snippets.

@coderfi
Created June 12, 2019 21:07
Show Gist options
  • Save coderfi/06b1f8fea7fd8ed37aa171bff5fc11e2 to your computer and use it in GitHub Desktop.
Save coderfi/06b1f8fea7fd8ed37aa171bff5fc11e2 to your computer and use it in GitHub Desktop.
Flask RawPathMiddleware
class RawPathMiddleware(object):
"""Do not let werkzeug silently replace percent encoding.
https://github.com/pallets/werkzeug/pull
/1419/files#diff-2fd07ed8a4cc8c956ceeca2347ac5376
Usage:
app.wsgi_app = RawPathMiddleware(app.wsgi_app)
see https://stackoverflow.com/questions/25466904
/print-raw-http-request-in-flask-or-wsgi
"""
def __init__(self, app):
"""Initialize this instance with the app."""
self._app = app
def __call__(self, environ, resp):
"""Print the request and response."""
# gunicorn
for s in ('RAW_URI', 'REQUEST_URI'):
if s in environ:
v = environ[s]
idx = v.find('?') # take out the query params
if idx >= 0:
v = v[:idx]
environ['PATH_INFO'] = v
break
def identity(status, headers, *args):
return resp(status, headers, *args)
return self._app(environ, identity)
@coderfi
Copy link
Author

coderfi commented Jun 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment