Skip to content
Snippets Groups Projects

Fix middleware to do exactly what we want/need

Merged Oliver Falk requested to merge devel into master
2 files
+ 6
14
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 5
13
@@ -6,21 +6,13 @@ from django.utils.deprecation import MiddlewareMixin
class MultipleProxyMiddleware(MiddlewareMixin): # pylint: disable=too-few-public-methods
"""
Middleware to rewrite proxy headers for deployments
multiple proxies
with multiple proxies
"""
FORWARDED_FOR_FIELDS = [
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED_HOST',
'HTTP_X_FORWARDED_SERVER',
]
def process_request(self, request):
"""
Rewrites the proxy headers so that only the most
recent proxy is used.
Rewrites the proxy headers so that forwarded server is
used if available.
"""
for field in self.FORWARDED_FOR_FIELDS:
if field in request.META:
if ',' in request.META[field]:
parts = request.META[field].split(',')
request.META[field] = parts[-1].strip()
if 'HTTP_X_FORWARDED_SERVER' in request.META:
request.META['HTTP_X_FORWARDED_HOST'] = request.META['HTTP_X_FORWARDED_SERVER']
Loading