Ticket #17092: add_host_to_location.diff

File add_host_to_location.diff, 4.1 KB (added by anonymous, 12 years ago)
  • django/http/__init__.py

     
    535535    status_code = 200
    536536
    537537    def __init__(self, content='', mimetype=None, status=None,
    538             content_type=None):
     538            content_type=None, add_host_to_location=True):
    539539        # _headers is a mapping of the lower-case name to the original case of
    540540        # the header (required for working with legacy systems) and the header
    541541        # value.  Both the name of the header and its value are ASCII strings.
     
    552552            self.status_code = status
    553553
    554554        self['Content-Type'] = content_type
     555        self.add_host_to_location = add_host_to_location
    555556
    556557    def __str__(self):
    557558        """Full HTTP message, including headers."""
  • django/http/utils.py

     
    1616    Code constructing response objects is free to insert relative paths, as
    1717    this function converts them to absolute paths.
    1818    """
    19     if 'Location' in response and request.get_host():
     19    if 'Location' in response and response.add_host_to_location \
     20       and request.get_host():
    2021        response['Location'] = request.build_absolute_uri(response['Location'])
    2122    return response
    2223
  • docs/ref/request-response.txt

     
    597597Methods
    598598-------
    599599
    600 .. method:: HttpResponse.__init__(content='', mimetype=None, status=200, content_type=DEFAULT_CONTENT_TYPE)
     600.. method:: HttpResponse.__init__(content='', mimetype=None, status=200, content_type=DEFAULT_CONTENT_TYPE, add_host_to_location=True)
    601601
    602602    Instantiates an ``HttpResponse`` object with the given page content (a
    603603    string) and MIME type. The :setting:`DEFAULT_CONTENT_TYPE` is
     
    619619    Otherwise, ``content_type`` is used. If neither is given, the
    620620    :setting:`DEFAULT_CONTENT_TYPE` setting is used.
    621621
     622    .. versionadded:: 1.4
     623
     624    ``add_host_to_location`` by default django ensures that all location
     625    headers contain an absolute URI using request.build_absolute_uri if you
     626    need to disable this behavior set ``add_host_to_location`` to False.
     627
    622628.. method:: HttpResponse.__setitem__(header, value)
    623629
    624630    Sets the given header name to the given value. Both ``header`` and
  • tests/regressiontests/requests/tests.py

     
    66from django.core.handlers.modpython import ModPythonRequest
    77from django.core.handlers.wsgi import WSGIRequest, LimitedStream
    88from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr
     9from django.http.utils import fix_location_header
    910from django.utils import unittest
    1011from django.utils.http import cookie_date
    1112
     13class FixHeaderTests(unittest.TestCase):
     14    def test_httprequest_add_host(self):
     15        print 'hi'
     16        request = HttpRequest()
     17        request.META = {
     18                u'HTTP_HOST': u'example.com',
     19            }
     20        response = HttpResponse()
     21        response['Location'] = '/test'
     22        response = fix_location_header(request, response)
     23        self.assertEqual(response['Location'], 'http://example.com/test')
    1224
     25    def test_httprequest_no_host(self):
     26        request = HttpRequest()
     27        response = HttpResponse(add_host_to_location=False)
     28        response['Location'] = '/test'
     29        response = fix_location_header(request, response)
     30        self.assertEqual(response['Location'], '/test')
     31
    1332class RequestsTests(unittest.TestCase):
    1433    def test_httprequest(self):
     34        print 'hi'
    1535        request = HttpRequest()
    1636        self.assertEqual(request.GET.keys(), [])
    1737        self.assertEqual(request.POST.keys(), [])
Back to Top