]> granicus.if.org Git - python/commitdiff
Fix for the Issue918368 - urllib doesn't correct server returned urls
authorSenthil Kumaran <orsenthil@gmail.com>
Tue, 21 Apr 2009 03:24:19 +0000 (03:24 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Tue, 21 Apr 2009 03:24:19 +0000 (03:24 +0000)
Lib/test/test_urllib.py
Lib/urllib.py

index 93921a67d64584e707f3b6ecd98dd56c9f6d4b8b..eaacc394fbe0253660016ccf2f7ae2ffdef4654c 100644 (file)
@@ -598,6 +598,18 @@ class Utility_Tests(unittest.TestCase):
         self.assertEqual(('user', 'a:b'),urllib.splitpasswd('user:a:b'))
 
 
+class URLopener_Tests(unittest.TestCase):
+    """Testcase to test the open method of URLopener class."""
+
+    def test_quoted_open(self):
+        class DummyURLopener(urllib.URLopener):
+            def open_spam(self, url):
+                return url
+
+        self.assertEqual(DummyURLopener().open(
+            'spam://example/ /'),'//example/%20/')
+
+
 # Just commented them out.
 # Can't really tell why keep failing in windows and sparc.
 # Everywhere else they work ok, but on those machines, someteimes
@@ -693,6 +705,7 @@ def test_main():
             urlencode_Tests,
             Pathname_Tests,
             Utility_Tests,
+            URLopener_Tests,
             #FTPWrapperTests,
         )
 
index 1ac997e2d82aa02749bb3a3316b60bf99d2ca243..db2b49ec99b11057030e4cdb4b003e5419497147 100644 (file)
@@ -176,6 +176,9 @@ class URLopener:
     def open(self, fullurl, data=None):
         """Use URLopener().open(file) instead of open(file, 'r')."""
         fullurl = unwrap(toBytes(fullurl))
+        # percent encode url, fixing lame server errors for e.g, like space
+        # within url paths.
+        fullurl = quote(fullurl, safe="%/:=&?~#+!$,;'@()*[]")
         if self.tempcache and fullurl in self.tempcache:
             filename, headers = self.tempcache[fullurl]
             fp = open(filename, 'rb')