]> granicus.if.org Git - python/commitdiff
Merged revisions 86676 via svnmerge from
authorSenthil Kumaran <orsenthil@gmail.com>
Mon, 22 Nov 2010 05:04:33 +0000 (05:04 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Mon, 22 Nov 2010 05:04:33 +0000 (05:04 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r86676 | senthil.kumaran | 2010-11-22 12:48:26 +0800 (Mon, 22 Nov 2010) | 4 lines

  Fix Issue4493 - urllib2 adds '/' to the path component of url, when it does not
  starts with one. This behavior is exhibited by browser and other clients.
........

Lib/test/test_urllib2.py
Lib/urllib.py
Misc/NEWS

index 46db6b4257721143eb9d6bb21d0e2a0007bb2665..b74320d2accf2305de56908f56d86c51f07ab364 100644 (file)
@@ -838,6 +838,25 @@ class HandlerTests(unittest.TestCase):
             p_ds_req = h.do_request_(ds_req)
             self.assertEqual(p_ds_req.unredirected_hdrs["Host"],"example.com")
 
+    def test_fixpath_in_weirdurls(self):
+        # Issue4493: urllib2 to supply '/' when to urls where path does not
+        # start with'/'
+
+        h = urllib2.AbstractHTTPHandler()
+        o = h.parent = MockOpener()
+
+        weird_url = 'http://www.python.org?getspam'
+        req = Request(weird_url)
+        newreq = h.do_request_(req)
+        self.assertEqual(newreq.get_host(),'www.python.org')
+        self.assertEqual(newreq.get_selector(),'/?getspam')
+
+        url_without_path = 'http://www.python.org'
+        req = Request(url_without_path)
+        newreq = h.do_request_(req)
+        self.assertEqual(newreq.get_host(),'www.python.org')
+        self.assertEqual(newreq.get_selector(),'')
+
     def test_errors(self):
         h = urllib2.HTTPErrorProcessor()
         o = h.parent = MockOpener()
index d85dedbb647ac984da63ce864d641eaad623897f..1553f9dcb2d48a75f1dc2b7be131da0fc0fa487f 100644 (file)
@@ -1052,7 +1052,12 @@ def splithost(url):
         _hostprog = re.compile('^//([^/?]*)(.*)$')
 
     match = _hostprog.match(url)
-    if match: return match.group(1, 2)
+    if match:
+        host_port = match.group(1)
+        path = match.group(2)
+        if path and not path.startswith('/'):
+            path = '/' + path
+        return host_port, path
     return None, url
 
 _userprog = None
index 315b55694b8f9d38138a6a23fc1998d873d58ee3..cb9e489716393434a9d8a434ef874f28bc59f533 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #4493: urllib2 adds '/' in front of path components which does not
+  start with '/. Common behavior exhibited by browsers and other clients.
+
 - Issue #6378: idle.bat now runs with the appropriate Python version rather than
   the system default. Patch by Sridhar Ratnakumar.