Ticket #987: redirect_absoluteuri.2.patch
File redirect_absoluteuri.2.patch, 8.1 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 BaseCompulsaryMiddleware(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 compulsary_middleware = BaseCompulsaryMiddleware 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.compulsary_middleware] 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
2 2 from Cookie import SimpleCookie 3 3 from pprint import pformat 4 4 from urllib import urlencode, quote 5 from urlparse import urljoin 5 6 from django.utils.datastructures import MultiValueDict 6 7 7 8 RESERVED_CHARS="!*'();:@&=+$,/?%#[]" … … 39 40 def get_full_path(self): 40 41 return '' 41 42 43 def build_absolute_uri(self, location=None): 44 """ 45 Builds an absolute URI from the location and the variables available in 46 this request. If no location is specified, the absolute URI is built on 47 ``request.get_full_path()``. 48 """ 49 if not location: 50 location = request.get_full_path() 51 if not ':' in location: 52 current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http', 53 get_host(self), self.path) 54 location = urljoin(current_uri, location) 55 return location 56 42 57 def is_secure(self): 43 58 return os.environ.get("HTTPS") == "on" 44 59 -
django/test/testcases.py
57 57 self._pre_setup() 58 58 super(TestCase, self).__call__(result) 59 59 60 def assertRedirects(self, response, expected_ path, status_code=302, target_status_code=200):60 def assertRedirects(self, response, expected_url, status_code=302, target_status_code=200): 61 61 """Assert that a response redirected to a specific URL, and that the 62 62 redirect URL can be loaded. 63 63 … … 65 65 self.assertEqual(response.status_code, status_code, 66 66 "Response didn't redirect as expected: Reponse code was %d (expected %d)" % 67 67 (response.status_code, status_code)) 68 scheme, netloc, path, params, query, fragment = urlparse(response['Location']) 69 self.assertEqual(path, expected_path, 70 "Response redirected to '%s', expected '%s'" % (path, expected_path)) 68 url = response['Location'] 69 scheme, netloc, path, params, query, fragment = urlparse(url) 70 self.assertEqual(url, expected_url, 71 "Response redirected to '%s', expected '%s'" % (url, expected_url)) 71 72 redirect_response = self.client.get(path) 72 73 self.assertEqual(redirect_response.status_code, target_status_code, 73 74 "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" % -
docs/request_response.txt
151 151 152 152 Example: ``"/music/bands/the_beatles/?print=true"`` 153 153 154 ``build_absolute_uri(location)`` 155 Returns an absolute URI from the location. If no location is provided, the 156 location will be set to ``request.get_full_path()``. 157 158 If the location is already an absolute URI, it will not be altered, 159 otherwise the absolute URI is built using the server variables available in 160 this request. 161 162 Example: ``"http://example.com/music/bands/the_beatles/?print=true"`` 163 154 164 ``is_secure()`` 155 165 Returns ``True`` if the request is secure; that is, if it was made with 156 166 HTTPS. -
tests/modeltests/test_client/models.py
78 78 79 79 def test_redirect(self): 80 80 "GET a URL that redirects elsewhere" 81 response = self.client.get('/test_client/redirect_view/') 82 81 response = self.client.get('/test_client/redirect_view/') 83 82 # Check that the response was a 302 (redirect) 84 83 self.assertRedirects(response, '/test_client/get_view/') 84 85 client_providing_host = Client(HTTP_HOST='django.testserver') 86 response = client_providing_host.get('/test_client/redirect_view/') 87 # Check that the response was a 302 (redirect) with absolute URI 88 self.assertRedirects(response, 'http://django.testserver/test_client/get_view/') 85 89 86 90 def test_permanent_redirect(self): 87 91 "GET a URL that redirects permanently elsewhere" 88 92 response = self.client.get('/test_client/permanent_redirect_view/') 89 90 93 # Check that the response was a 301 (permanent redirect) 91 94 self.assertRedirects(response, '/test_client/get_view/', status_code=301) 92 95 96 client_providing_host = Client(HTTP_HOST='django.testserver') 97 response = client_providing_host.get('/test_client/permanent_redirect_view/') 98 # Check that the response was a 301 (permanent redirect) with absolute URI 99 self.assertRedirects(response, 'http://django.testserver/test_client/get_view/', status_code=301) 100 93 101 def test_redirect_to_strange_location(self): 94 102 "GET a URL that redirects to a non-200 page" 95 103 response = self.client.get('/test_client/double_redirect_view/') … … 208 216 209 217 # Get the page without logging in. Should result in 302. 210 218 response = self.client.get('/test_client/login_protected_view/') 211 self.assertRedirects(response, '/accounts/login/ ')219 self.assertRedirects(response, '/accounts/login/?next=/test_client/login_protected_view/') 212 220 213 221 # Log in 214 222 self.client.login(username='testclient', password='password')