upgrade gunicorn 19.0

This commit is contained in:
nadeemshahzad
2018-11-15 13:28:16 +00:00
parent c992edc83d
commit 9d95c2e632
12 changed files with 52 additions and 26 deletions

View File

@@ -0,0 +1,30 @@
"""
Middleware to use the X-Forwarded-For header as the request IP.
Updated the libray to use HTTP_HOST and X-Forwarded-Port as
SERVER_NAME and SERVER_PORT.
"""
class XForwardedForMiddleware(object):
"""
Gunicorn 19.0 has breaking changes for REMOTE_ADDR, SERVER_* headers
that can not override with forwarded and host headers.
This middleware can be used to update these headers set by proxy configuration.
"""
def process_request(self, request):
"""
Process the given request, update the value of REMOTE_ADDR, SERVER_NAME and SERVER_PORT based
on X-Forwarded-For, HTTP_HOST and X-Forwarded-Port headers
"""
for field, header in [("HTTP_X_FORWARDED_FOR", "REMOTE_ADDR"), ("HTTP_HOST", "SERVER_NAME"),
("HTTP_X_FORWARDED_PORT", "SERVER_PORT")]:
if field in request.META:
if ',' in request.META[field]:
request.META[header] = request.META[field].split(",")[0].strip()
else:
request.META[header] = request.META[field]
return None