Code

Ticket #11877: patch_11877.diff

File patch_11877.diff, 1.2 KB (added by arnav, 4 years ago)

patch with changes to documentation including example

Line 
1diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
2index 1788e06..0540551 100644
3--- a/docs/ref/request-response.txt
4+++ b/docs/ref/request-response.txt
5@@ -202,6 +202,28 @@ Methods
6 
7    Example: ``"127.0.0.1:8000"``
8 
9+   Warning: The ``get_host()`` method fails when the host is behind multiple
10+   proxies. Specific middleware can be used to work around this problem, like so::
11+
12+      class MultipleProxyMiddleware(object):
13+         FORWARDED_FOR_FIELDS = [
14+         'HTTP_X_FORWARDED_FOR',
15+         'HTTP_X_FORWARDED_HOST',
16+         'HTTP_X_FORWARDED_SERVER',
17+         ]
18+         
19+         def process_request(self, request):
20+            """
21+            This middleware rewrites these proxy headers so that only the most
22+            recent proxy is used.
23+            """
24+            for field in self.FORWARDED_FOR_FIELDS:
25+               if field in request.META:
26+                  if ',' in request.META[field]:
27+                     parts = request.META[field].split(',')
28+                     request.META[field] = parts[-1].strip()
29+
30+
31 .. method:: HttpRequest.get_full_path()
32 
33    Returns the ``path``, plus an appended query string, if applicable.