Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #11522: Restored ability of http redirect responses to …

…correctly handle redirect locations with non-ASCII chars.

r12659 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b50b3cf75c6ac1abeaa0f19472444eb8be3b73f9 1 parent cea0374
Karen Tracey authored March 02, 2010
4  django/http/__init__.py
@@ -434,14 +434,14 @@ class HttpResponseRedirect(HttpResponse):
434 434
 
435 435
     def __init__(self, redirect_to):
436 436
         HttpResponse.__init__(self)
437  
-        self['Location'] = redirect_to
  437
+        self['Location'] = iri_to_uri(redirect_to)
438 438
 
439 439
 class HttpResponsePermanentRedirect(HttpResponse):
440 440
     status_code = 301
441 441
 
442 442
     def __init__(self, redirect_to):
443 443
         HttpResponse.__init__(self)
444  
-        self['Location'] = redirect_to
  444
+        self['Location'] = iri_to_uri(redirect_to)
445 445
 
446 446
 class HttpResponseNotModified(HttpResponse):
447 447
     status_code = 304
26  tests/regressiontests/views/tests/specials.py
@@ -5,11 +5,31 @@ class URLHandling(TestCase):
5 5
     """
6 6
     Tests for URL handling in views and responses.
7 7
     """
8  
-    def test_iri_redirect(self):
  8
+    redirect_target = "/views/%E4%B8%AD%E6%96%87/target/"
  9
+
  10
+    def test_combining_redirect(self):
9 11
         """
10 12
         Tests that redirecting to an IRI, requiring encoding before we use it
11  
-        in an HTTP response, is handled correctly.
  13
+        in an HTTP response, is handled correctly. In this case the arg to
  14
+        HttpRedirect is ASCII but the current request path contains non-ASCII
  15
+        characters so this test ensures the creation of the full path with a
  16
+        base non-ASCII part is handled correctly.
12 17
         """
13 18
         response = self.client.get(u'/views/中文/')
14  
-        self.assertRedirects(response, "/views/%E4%B8%AD%E6%96%87/target/")
  19
+        self.assertRedirects(response, self.redirect_target)
  20
+
  21
+    def test_nonascii_redirect(self):
  22
+        """
  23
+        Tests that a non-ASCII argument to HttpRedirect is handled properly.
  24
+        """
  25
+        response = self.client.get('/views/nonascii_redirect/')
  26
+        self.assertRedirects(response, self.redirect_target)
  27
+
  28
+    def test_permanent_nonascii_redirect(self):
  29
+        """
  30
+        Tests that a non-ASCII argument to HttpPermanentRedirect is handled
  31
+        properly.
  32
+        """
  33
+        response = self.client.get('/views/permanent_nonascii_redirect/')
  34
+        self.assertRedirects(response, self.redirect_target, status_code=301)
15 35
 
9  tests/regressiontests/views/urls.py
@@ -97,3 +97,12 @@
97 97
 urlpatterns += patterns('',
98 98
     (r'^raises/$', views.raises)
99 99
 )
  100
+
  101
+# rediriects, both temporary and permanent, with non-ASCII targets
  102
+urlpatterns += patterns('django.views.generic.simple',
  103
+    ('^nonascii_redirect/$', 'redirect_to',
  104
+        {'url': u'/views/中文/target/', 'permanent': False}),
  105
+    ('^permanent_nonascii_redirect/$', 'redirect_to',
  106
+        {'url': u'/views/中文/target/', 'permanent': True}),
  107
+)
  108
+

0 notes on commit b50b3cf

Please sign in to comment.
Something went wrong with that request. Please try again.