Ticket #987: redirect_absoluteuri.3.patch
File redirect_absoluteuri.3.patch, 7.2 KB (added by , 17 years ago) |
---|
-
django/core/handlers/base.py
3 3 from django import http 4 4 import sys 5 5 6 class BaseRequiredProcesses(object): 7 def process_response(self, request, response): 8 # Absolute URI redirects: Ensures that any redirect attempt (which uses 9 # the HTTP Location header) to a relative location is converted to an 10 # absolute URI, as required by RFC 2616, section 14.30. 11 # This is only possible if the host name can be gleaned from request. 12 if 'Location' in response.headers and http.get_host(request): 13 response['Location'] = request.build_absolute_uri(response['Location']) 14 return response 15 6 16 class BaseHandler(object): 17 required_processes = BaseRequiredProcesses 18 7 19 def __init__(self): 8 20 self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None 9 21 … … 15 27 """ 16 28 from django.conf import settings 17 29 from django.core import exceptions 18 self._request_middleware = [] 19 self._view_middleware = [] 20 self._response_middleware = [] 21 self._exception_middleware = [] 30 31 middleware_classes = [self.required_processes] 22 32 for middleware_path in settings.MIDDLEWARE_CLASSES: 23 33 try: 24 34 dot = middleware_path.rindex('.') … … 30 40 except ImportError, e: 31 41 raise exceptions.ImproperlyConfigured, 'Error importing middleware %s: "%s"' % (mw_module, e) 32 42 try: 33 m w_class = getattr(mod, mw_classname)43 middleware_classes.append(getattr(mod, mw_classname)) 34 44 except AttributeError: 35 45 raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname) 36 46 47 self._request_middleware = [] 48 self._view_middleware = [] 49 self._response_middleware = [] 50 self._exception_middleware = [] 51 for mw_class in middleware_classes: 37 52 try: 38 53 mw_instance = mw_class() 39 54 except exceptions.MiddlewareNotUsed: 40 55 continue 41 42 56 if hasattr(mw_instance, 'process_request'): 43 57 self._request_middleware.append(mw_instance.process_request) 44 58 if hasattr(mw_instance, 'process_view'): -
django/http/__init__.py
1 1 import os 2 2 from Cookie import SimpleCookie 3 3 from pprint import pformat 4 from urllib import urlencode 4 from urllib import urlencode, quote 5 from urlparse import urljoin 5 6 from django.utils.datastructures import MultiValueDict, FileDict 6 7 from django.utils.encoding import smart_str, iri_to_uri, force_unicode 7 8 … … 44 45 def get_full_path(self): 45 46 return '' 46 47 48 def build_absolute_uri(self, location=None): 49 """ 50 Builds an absolute URI from the location and the variables available in 51 this request. If no location is specified, the absolute URI is built on 52 ``request.get_full_path()``. 53 """ 54 if not location: 55 location = request.get_full_path() 56 if not ':' in location: 57 current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http', 58 get_host(self), self.path) 59 location = urljoin(current_uri, location) 60 return location 61 47 62 def is_secure(self): 48 63 return os.environ.get("HTTPS") == "on" 49 64 -
django/test/testcases.py
84 84 self.assertEqual(response.status_code, status_code, 85 85 ("Response didn't redirect as expected: Response code was %d" 86 86 " (expected %d)" % (response.status_code, status_code))) 87 scheme, netloc, path, query, fragment = urlsplit(response['Location']) 88 url = path 89 if query: 90 url += '?' + query 91 if fragment: 92 url += '#' + fragment 87 url = response['Location'] 88 scheme, netloc, path, query, fragment = urlsplit(url) 93 89 self.assertEqual(url, expected_url, 94 90 "Response redirected to '%s', expected '%s'" % (url, expected_url)) 95 91 -
docs/request_response.txt
161 161 162 162 Example: ``"/music/bands/the_beatles/?print=true"`` 163 163 164 ``build_absolute_uri(location)`` 165 Returns an absolute URI from the location. If no location is provided, the 166 location will be set to ``request.get_full_path()``. 167 168 If the location is already an absolute URI, it will not be altered, 169 otherwise the absolute URI is built using the server variables available in 170 this request. 171 172 Example: ``"http://example.com/music/bands/the_beatles/?print=true"`` 173 164 174 ``is_secure()`` 165 175 Returns ``True`` if the request is secure; that is, if it was made with 166 176 HTTPS. -
tests/modeltests/test_client/models.py
83 83 def test_redirect(self): 84 84 "GET a URL that redirects elsewhere" 85 85 response = self.client.get('/test_client/redirect_view/') 86 87 86 # Check that the response was a 302 (redirect) 88 87 self.assertRedirects(response, '/test_client/get_view/') 88 89 client_providing_host = Client(HTTP_HOST='django.testserver') 90 response = client_providing_host.get('/test_client/redirect_view/') 91 # Check that the response was a 302 (redirect) with absolute URI 92 self.assertRedirects(response, 'http://django.testserver/test_client/get_view/') 89 93 90 94 def test_redirect_with_query(self): 91 95 "GET a URL that redirects with given GET parameters" … … 97 101 def test_permanent_redirect(self): 98 102 "GET a URL that redirects permanently elsewhere" 99 103 response = self.client.get('/test_client/permanent_redirect_view/') 100 101 104 # Check that the response was a 301 (permanent redirect) 102 105 self.assertRedirects(response, '/test_client/get_view/', status_code=301) 103 106 107 client_providing_host = Client(HTTP_HOST='django.testserver') 108 response = client_providing_host.get('/test_client/permanent_redirect_view/') 109 # Check that the response was a 301 (permanent redirect) with absolute URI 110 self.assertRedirects(response, 'http://django.testserver/test_client/get_view/', status_code=301) 111 104 112 def test_redirect_to_strange_location(self): 105 113 "GET a URL that redirects to a non-200 page" 106 114 response = self.client.get('/test_client/double_redirect_view/')