Code

Ticket #17092: add_host_to_location.diff

File add_host_to_location.diff, 4.1 KB (added by anonymous, 3 years ago)
Line 
1Index: django/http/__init__.py
2===================================================================
3--- django/http/__init__.py     (revision 17060)
4+++ django/http/__init__.py     (working copy)
5@@ -535,7 +535,7 @@
6     status_code = 200
7 
8     def __init__(self, content='', mimetype=None, status=None,
9-            content_type=None):
10+            content_type=None, add_host_to_location=True):
11         # _headers is a mapping of the lower-case name to the original case of
12         # the header (required for working with legacy systems) and the header
13         # value.  Both the name of the header and its value are ASCII strings.
14@@ -552,6 +552,7 @@
15             self.status_code = status
16 
17         self['Content-Type'] = content_type
18+        self.add_host_to_location = add_host_to_location
19 
20     def __str__(self):
21         """Full HTTP message, including headers."""
22Index: django/http/utils.py
23===================================================================
24--- django/http/utils.py        (revision 17060)
25+++ django/http/utils.py        (working copy)
26@@ -16,7 +16,8 @@
27     Code constructing response objects is free to insert relative paths, as
28     this function converts them to absolute paths.
29     """
30-    if 'Location' in response and request.get_host():
31+    if 'Location' in response and response.add_host_to_location \
32+       and request.get_host():
33         response['Location'] = request.build_absolute_uri(response['Location'])
34     return response
35 
36Index: docs/ref/request-response.txt
37===================================================================
38--- docs/ref/request-response.txt       (revision 17060)
39+++ docs/ref/request-response.txt       (working copy)
40@@ -597,7 +597,7 @@
41 Methods
42 -------
43 
44-.. method:: HttpResponse.__init__(content='', mimetype=None, status=200, content_type=DEFAULT_CONTENT_TYPE)
45+.. method:: HttpResponse.__init__(content='', mimetype=None, status=200, content_type=DEFAULT_CONTENT_TYPE, add_host_to_location=True)
46 
47     Instantiates an ``HttpResponse`` object with the given page content (a
48     string) and MIME type. The :setting:`DEFAULT_CONTENT_TYPE` is
49@@ -619,6 +619,12 @@
50     Otherwise, ``content_type`` is used. If neither is given, the
51     :setting:`DEFAULT_CONTENT_TYPE` setting is used.
52 
53+    .. versionadded:: 1.4
54+
55+    ``add_host_to_location`` by default django ensures that all location
56+    headers contain an absolute URI using request.build_absolute_uri if you
57+    need to disable this behavior set ``add_host_to_location`` to False.
58+
59 .. method:: HttpResponse.__setitem__(header, value)
60 
61     Sets the given header name to the given value. Both ``header`` and
62Index: tests/regressiontests/requests/tests.py
63===================================================================
64--- tests/regressiontests/requests/tests.py     (revision 17066)
65+++ tests/regressiontests/requests/tests.py     (working copy)
66@@ -6,12 +6,32 @@
67 from django.core.handlers.modpython import ModPythonRequest
68 from django.core.handlers.wsgi import WSGIRequest, LimitedStream
69 from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr
70+from django.http.utils import fix_location_header
71 from django.utils import unittest
72 from django.utils.http import cookie_date
73 
74+class FixHeaderTests(unittest.TestCase):
75+    def test_httprequest_add_host(self):
76+        print 'hi'
77+        request = HttpRequest()
78+        request.META = {
79+                u'HTTP_HOST': u'example.com',
80+            }
81+        response = HttpResponse()
82+        response['Location'] = '/test'
83+        response = fix_location_header(request, response)
84+        self.assertEqual(response['Location'], 'http://example.com/test')
85 
86+    def test_httprequest_no_host(self):
87+        request = HttpRequest()
88+        response = HttpResponse(add_host_to_location=False)
89+        response['Location'] = '/test'
90+        response = fix_location_header(request, response)
91+        self.assertEqual(response['Location'], '/test')
92+
93 class RequestsTests(unittest.TestCase):
94     def test_httprequest(self):
95+        print 'hi'
96         request = HttpRequest()
97         self.assertEqual(request.GET.keys(), [])
98         self.assertEqual(request.POST.keys(), [])