]> granicus.if.org Git - python/commitdiff
Issue #22278: Fix urljoin problem with relative urls, a regression observed
authorSenthil Kumaran <senthil@uthcode.com>
Mon, 22 Sep 2014 07:49:16 +0000 (15:49 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Mon, 22 Sep 2014 07:49:16 +0000 (15:49 +0800)
after changes to issue22118 were submitted.

Patch contributed by Demian Brecht and reviewed by Antoine Pitrou.

Lib/test/test_urlparse.py
Lib/urllib/parse.py
Misc/NEWS

index 24c1856fbc15a145a34d1403b771577190cbd090..cb323d3423bea699a53b9a26724e1ed8681a6ac1 100644 (file)
@@ -380,6 +380,18 @@ class UrlParseTestCase(unittest.TestCase):
         # self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
         # self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
 
+        # test for issue22118 duplicate slashes
+        self.checkJoin(SIMPLE_BASE + '/', 'foo', SIMPLE_BASE + '/foo')
+
+        # Non-RFC-defined tests, covering variations of base and trailing
+        # slashes
+        self.checkJoin('http://a/b/c/d/e/', '../../f/g/', 'http://a/b/c/f/g/')
+        self.checkJoin('http://a/b/c/d/e', '../../f/g/', 'http://a/b/f/g/')
+        self.checkJoin('http://a/b/c/d/e/', '/../../f/g/', 'http://a/f/g/')
+        self.checkJoin('http://a/b/c/d/e', '/../../f/g/', 'http://a/f/g/')
+        self.checkJoin('http://a/b/c/d/e/', '../../f/g', 'http://a/b/c/f/g')
+        self.checkJoin('http://a/b/', '../../f/g/', 'http://a/f/g/')
+
     def test_RFC2732(self):
         str_cases = [
             ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
index b6ac414dfdfb5e0da036641f25583461c278f86c..8bbeab62007b085c3d56cbd784ea38b753cc5c79 100644 (file)
@@ -443,6 +443,10 @@ def urljoin(base, url, allow_fragments=True):
         segments = path.split('/')
     else:
         segments = base_parts + path.split('/')
+        # filter out elements that would cause redundant slashes on re-joining
+        # the resolved_path
+        segments = segments[0:1] + [
+            s for s in segments[1:-1] if len(s) > 0] + segments[-1:]
 
     resolved_path = []
 
@@ -465,7 +469,7 @@ def urljoin(base, url, allow_fragments=True):
         resolved_path.append('')
 
     return _coerce_result(urlunparse((scheme, netloc, '/'.join(
-        resolved_path), params, query, fragment)))
+        resolved_path) or '/', params, query, fragment)))
 
 
 def urldefrag(url):
index 72408424ec269ec5cc86d4b0939c2300df7df1db..c26eaa6f757e44bea9f37cad2ed99ee6339ee1f3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -137,6 +137,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #22278: Fix urljoin problem with relative urls, a regression observed
+  after changes to issue22118 were submitted.
+
 - Issue #22415: Fixed debugging output of the GROUPREF_EXISTS opcode in the re
   module.  Removed trailing spaces in debugging output.