diff --git a/django/http/response.py b/django/http/response.py
index b3f44cd..3172500 100644
|
a
|
b
|
class HttpResponseBase(six.Iterator):
|
| 98 | 98 | status_code = 200 |
| 99 | 99 | reason_phrase = None # Use default reason phrase for status code. |
| 100 | 100 | |
| 101 | | def __init__(self, content_type=None, status=None, reason=None): |
| | 101 | def __init__(self, content_type=None, status=None, reason=None, add_host_to_location=True): |
| 102 | 102 | # _headers is a mapping of the lower-case name to the original case of |
| 103 | 103 | # the header (required for working with legacy systems) and the header |
| 104 | 104 | # value. Both the name of the header and its value are ASCII strings. |
| … |
… |
class HttpResponseBase(six.Iterator):
|
| 120 | 120 | self.reason_phrase = REASON_PHRASES.get(self.status_code, |
| 121 | 121 | 'UNKNOWN STATUS CODE') |
| 122 | 122 | self['Content-Type'] = content_type |
| | 123 | self.add_host_to_location = add_host_to_location |
| 123 | 124 | |
| 124 | 125 | def serialize_headers(self): |
| 125 | 126 | """HTTP headers as a bytestring.""" |
diff --git a/django/http/utils.py b/django/http/utils.py
index 90155cd..104cc17 100644
|
a
|
b
|
def fix_location_header(request, response):
|
| 17 | 17 | Code constructing response objects is free to insert relative paths, as |
| 18 | 18 | this function converts them to absolute paths. |
| 19 | 19 | """ |
| 20 | | if 'Location' in response and request.get_host(): |
| | 20 | if 'Location' in response and response.add_host_to_location and request.get_host(): |
| 21 | 21 | response['Location'] = request.build_absolute_uri(response['Location']) |
| 22 | 22 | return response |
| 23 | 23 | |
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
index 8e42fa1..053b2e6 100644
|
a
|
b
|
Attributes
|
| 635 | 635 | This attribute exists so middleware can treat streaming responses |
| 636 | 636 | differently from regular responses. |
| 637 | 637 | |
| | 638 | .. versionadded:: 1.7 |
| | 639 | .. attribute:::: HttpResponse.add_host_to_location |
| | 640 | |
| | 641 | By default django ensures that all location headers contain an absolute |
| | 642 | URI using request.build_absolute_uri to build it. If you |
| | 643 | need to disable this behavior set ``add_host_to_location`` to False. |
| | 644 | |
| | 645 | |
| 638 | 646 | Methods |
| 639 | 647 | ------- |
| 640 | 648 | |
diff --git a/tests/requests/tests.py b/tests/requests/tests.py
index b778fe3..4b96ba4 100644
|
a
|
b
|
from django.utils import six
|
| 17 | 17 | from django.utils.http import cookie_date, urlencode |
| 18 | 18 | from django.utils.six.moves.urllib.parse import urlencode as original_urlencode |
| 19 | 19 | from django.utils.timezone import utc |
| 20 | | |
| | 20 | from django.http.utils import fix_location_header |
| 21 | 21 | |
| 22 | 22 | class RequestsTests(SimpleTestCase): |
| 23 | 23 | def test_httprequest(self): |
| … |
… |
class BuildAbsoluteURITestCase(SimpleTestCase):
|
| 761 | 761 | request.build_absolute_uri(location='/foo/bar/'), |
| 762 | 762 | 'http://testserver/foo/bar/' |
| 763 | 763 | ) |
| | 764 | |
| | 765 | class FixHeaderTests(SimpleTestCase): |
| | 766 | def test_httprequest_add_host(self): |
| | 767 | request = HttpRequest() |
| | 768 | request.META = { |
| | 769 | u'HTTP_HOST': u'example.com', |
| | 770 | } |
| | 771 | response = HttpResponse() |
| | 772 | response['Location'] = '/test' |
| | 773 | response = fix_location_header(request, response) |
| | 774 | self.assertEqual(response['Location'], 'http://example.com/test') |
| | 775 | |
| | 776 | def test_httprequest_no_host(self): |
| | 777 | request = HttpRequest() |
| | 778 | response = HttpResponse(add_host_to_location=False) |
| | 779 | response['Location'] = '/test' |
| | 780 | response = fix_location_header(request, response) |
| | 781 | self.assertEqual(response['Location'], '/test') |