Ticket #17092: add_host_to_location.4.diff

File add_host_to_location.4.diff, 3.6 KB (added by anonymous, 10 years ago)
  • django/http/response.py

    diff --git a/django/http/response.py b/django/http/response.py
    index b3f44cd..3172500 100644
    a b class HttpResponseBase(six.Iterator):  
    9898    status_code = 200
    9999    reason_phrase = None        # Use default reason phrase for status code.
    100100
    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):
    102102        # _headers is a mapping of the lower-case name to the original case of
    103103        # the header (required for working with legacy systems) and the header
    104104        # value. Both the name of the header and its value are ASCII strings.
    class HttpResponseBase(six.Iterator):  
    120120            self.reason_phrase = REASON_PHRASES.get(self.status_code,
    121121                                                    'UNKNOWN STATUS CODE')
    122122        self['Content-Type'] = content_type
     123        self.add_host_to_location = add_host_to_location
    123124
    124125    def serialize_headers(self):
    125126        """HTTP headers as a bytestring."""
  • django/http/utils.py

    diff --git a/django/http/utils.py b/django/http/utils.py
    index 90155cd..104cc17 100644
    a b def fix_location_header(request, response):  
    1717    Code constructing response objects is free to insert relative paths, as
    1818    this function converts them to absolute paths.
    1919    """
    20     if 'Location' in response and request.get_host():
     20    if 'Location' in response and response.add_host_to_location and request.get_host():
    2121        response['Location'] = request.build_absolute_uri(response['Location'])
    2222    return response
    2323
  • docs/ref/request-response.txt

    diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
    index 8e42fa1..053b2e6 100644
    a b Attributes  
    635635    This attribute exists so middleware can treat streaming responses
    636636    differently from regular responses.
    637637
     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
    638646Methods
    639647-------
    640648
  • tests/requests/tests.py

    diff --git a/tests/requests/tests.py b/tests/requests/tests.py
    index b778fe3..4b96ba4 100644
    a b from django.utils import six  
    1717from django.utils.http import cookie_date, urlencode
    1818from django.utils.six.moves.urllib.parse import urlencode as original_urlencode
    1919from django.utils.timezone import utc
    20 
     20from django.http.utils import fix_location_header
    2121
    2222class RequestsTests(SimpleTestCase):
    2323    def test_httprequest(self):
    class BuildAbsoluteURITestCase(SimpleTestCase):  
    761761            request.build_absolute_uri(location='/foo/bar/'),
    762762            'http://testserver/foo/bar/'
    763763        )
     764
     765class 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')
Back to Top