]> granicus.if.org Git - python/commitdiff
Fix closes Issue12576 - fix urlopen behavior on sites which do not send (or obsfuscat...
authorSenthil Kumaran <senthil@uthcode.com>
Wed, 27 Jul 2011 00:05:58 +0000 (08:05 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Wed, 27 Jul 2011 00:05:58 +0000 (08:05 +0800)
Lib/test/test_urllib2net.py
Lib/urllib/request.py
Misc/NEWS

index a475f56402d688601c5f90e925ec0acf0de5b34d..cd225c90fd59175c990851ed57aa50258f826d4f 100644 (file)
@@ -174,6 +174,22 @@ class OtherNetworkTests(unittest.TestCase):
             opener.open(request)
             self.assertEqual(request.get_header('User-agent'),'Test-Agent')
 
+    def test_sites_no_connection_close(self):
+        # Some sites do not send Connection: close header.
+        # Verify that those work properly. (#issue12576)
+
+        try:
+            with urllib.request.urlopen('http://www.imdb.com') as res:
+                pass
+        except ValueError as e:
+            self.fail("urlopen failed for sites not sending Connection:close")
+        else:
+            self.assertTrue(res)
+
+        req = urllib.request.urlopen('http://www.imdb.com')
+        res = req.read()
+        self.assertTrue(res)
+
     def _test_urls(self, urls, handlers, retry=True):
         import time
         import logging
index a09a35348c9d6135525864e799cc5243e93bcf43..534408d815c5bd9b5d0a18640bbc944be98bab1f 100644 (file)
@@ -1134,11 +1134,14 @@ class AbstractHTTPHandler(BaseHandler):
 
         try:
             h.request(req.get_method(), req.selector, req.data, headers)
-            r = h.getresponse()  # an HTTPResponse instance
-        except socket.error as err:
+        except socket.error as err: # timeout error
             raise URLError(err)
         finally:
-            h.close()
+            try:
+                r = h.getresponse() # an HTTPResponse instance
+            except Exception as exp:
+                h.close()
+                raise exp
 
         r.url = req.get_full_url()
         # This line replaces the .msg attribute of the HTTPResponse
index d91a9932054ba604f34629047957c4ff90891d71..2d6d3158dcc140b3aba789c9bc7ec6496335ffc5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -37,6 +37,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #12576: Fix urlopen behavior on sites which do not send (or obfuscates)
+  Connection:close header.
+
 - Issue #12102: Document that buffered files must be flushed before being used
   with mmap. Patch by Steffen Daode Nurpmeso.