Created
June 12, 2019 21:07
-
-
Save coderfi/06b1f8fea7fd8ed37aa171bff5fc11e2 to your computer and use it in GitHub Desktop.
Flask RawPathMiddleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answers: https://stackoverflow.com/questions/28056959/in-python-flask-how-to-access-complete-raw-url-prior-to-un-escaping