Code

Ticket #18314: 18314.diff

File 18314.diff, 5.5 KB (added by SmileyChris, 2 years ago)
Line 
1diff --git a/django/http/__init__.py b/django/http/__init__.py
2index 2bad146..0c752e6 100644
3--- a/django/http/__init__.py
4+++ b/django/http/__init__.py
5@@ -10,7 +10,7 @@ import warnings
6 from io import BytesIO
7 from pprint import pformat
8 from urllib import urlencode, quote
9-from urlparse import urljoin, parse_qsl
10+from urlparse import urljoin, urlsplit, parse_qsl
11 
12 import Cookie
13 # Some versions of Python 2.7 and later won't need this encoding bug fix:
14@@ -86,8 +86,6 @@ from django.utils import timezone
15 
16 RESERVED_CHARS="!*'();:@&=+$,/?%#[]"
17 
18-absolute_http_url_re = re.compile(r"^https?://", re.I)
19-
20 class Http404(Exception):
21     pass
22 
23@@ -205,15 +203,30 @@ class HttpRequest(object):
24 
25     def build_absolute_uri(self, location=None):
26         """
27-        Builds an absolute URI from the location and the variables available in
28-        this request. If no location is specified, the absolute URI is built on
29-        ``request.get_full_path()``.
30+        Builds an absolute URI, using location and/or variables available in
31+        the request, and returns it.
32+
33+        If ``location`` is provided and is absolute, it is simply converted to
34+        an RFC 3987 compliant URI and returned.
35+
36+        If ``location`` is provided but is does not provide both a scheme and
37+        domain, it is urljoined to a base URL constructed from the request
38+        variables.
39         """
40         if not location:
41-            location = self.get_full_path()
42-        if not absolute_http_url_re.match(location):
43-            current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http',
44-                                         self.get_host(), self.path)
45+            # Make it an absolute url (but schemeless and domainless) for the
46+            # edge case that the path starts with '//'.
47+            location = '//%s' % self.get_full_path()
48+        bits = urlsplit(location)
49+        if not bits.scheme or not bits.netloc:
50+            current_uri = '%(scheme)s://%(host)s%(path)s' % {
51+                'scheme': 'https' if self.is_secure() else 'http',
52+                'host': self.get_host(),
53+                'path': self.path
54+            }
55+            # Join the constructed URL with the provided location, which will
56+            # allow the provided ``location`` to apply query strings to the
57+            # base path as well as override the host, if it begins with //
58             location = urljoin(current_uri, location)
59         return iri_to_uri(location)
60 
61@@ -750,4 +763,3 @@ def str_to_unicode(s, encoding):
62         return unicode(s, encoding, 'replace')
63     else:
64         return s
65-
66diff --git a/tests/regressiontests/http/__init__.py b/tests/regressiontests/http/__init__.py
67new file mode 100644
68index 0000000..e69de29
69diff --git a/tests/regressiontests/http/models.py b/tests/regressiontests/http/models.py
70new file mode 100644
71index 0000000..e69de29
72diff --git a/tests/regressiontests/http/tests.py b/tests/regressiontests/http/tests.py
73new file mode 100644
74index 0000000..8da954a
75--- /dev/null
76+++ b/tests/regressiontests/http/tests.py
77@@ -0,0 +1,65 @@
78+from django.utils import unittest
79+from django.test.client import RequestFactory
80+
81+
82+class HttpRequestTestCase(unittest.TestCase):
83+    """
84+    Regression tests for ticket #18314.
85+    """
86+    def setUp(self):
87+        """
88+        Attaches a request factory.
89+        """
90+        self.factory = RequestFactory()
91+
92+    def test_build_absolute_uri_no_location(self):
93+        """
94+        Ensures that ``request.build_absolute_uri()`` returns the proper value
95+        when the ``location`` argument is not provided, and ``request.path``
96+        begins with //.
97+        """
98+        # //// is needed to create a request with a path beginning with //
99+        request = self.factory.get('////django-ate-my-baby')
100+        self.assertEqual(
101+            request.build_absolute_uri(),
102+            'http://testserver//django-ate-my-baby'
103+        )
104+
105+    def test_build_absolute_uri_absolute_location(self):
106+        """
107+        Ensures that ``request.build_absolute_uri()`` returns the proper value
108+        when an absolute URL ``location`` argument is provided, and
109+        ``request.path`` begins with //.
110+        """
111+        # //// is needed to create a request with a path beginning with //
112+        request = self.factory.get('////django-ate-my-baby')
113+        self.assertEqual(
114+            request.build_absolute_uri(location='http://example.com/?foo=bar'),
115+            'http://example.com/?foo=bar'
116+        )
117+
118+    def test_build_absolute_uri_schema_relative_location(self):
119+        """
120+        Ensures that ``request.build_absolute_uri()`` returns the proper value
121+        when a schema-relative URL ``location`` argument is provided, and
122+        ``request.path`` begins with //.
123+        """
124+        # //// is needed to create a request with a path beginning with //
125+        request = self.factory.get('////django-ate-my-baby')
126+        self.assertEqual(
127+            request.build_absolute_uri(location='//example.com/?foo=bar'),
128+            'http://example.com/?foo=bar'
129+        )
130+
131+    def test_build_absolute_uri_relative_location(self):
132+        """
133+        Ensures that ``request.build_absolute_uri()`` returns the proper value
134+        when a relative URL ``location`` argument is provided, and
135+        ``request.path`` begins with //.
136+        """
137+        # //// is needed to create a request with a path beginning with //
138+        request = self.factory.get('////django-ate-my-baby')
139+        self.assertEqual(
140+            request.build_absolute_uri(location='/foo/bar/'),
141+            'http://testserver/foo/bar/'
142+        )