From f66b6039c11fb8f9162867afb7e6574652637a7b Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 14 Mar 2007 08:27:52 +0000 Subject: Bug #767111: fix long-standing bug in urllib which caused an AttributeError instead of an IOError when the server's response didn't contain a valid HTTP status line. --- Lib/test/test_urllib.py | 9 +++++++++ Lib/urllib.py | 8 ++++++++ Misc/NEWS | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 4579c47..294ed5e 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -122,6 +122,15 @@ class urlopen_HttpTests(unittest.TestCase): finally: self.unfakehttp() + def test_empty_socket(self): + """urlopen() raises IOError if the underlying socket does not send any + data. (#1680230) """ + self.fakehttp('') + try: + self.assertRaises(IOError, urllib.urlopen, 'http://something') + finally: + self.unfakehttp() + class urlretrieve_FileTests(unittest.TestCase): """Test urllib.urlretrieve() on local files""" diff --git a/Lib/urllib.py b/Lib/urllib.py index 0fc9b28..c000f11 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -326,6 +326,10 @@ class URLopener: if data is not None: h.send(data) errcode, errmsg, headers = h.getreply() + if errcode == -1: + # something went wrong with the HTTP status line + raise IOError, ('http protocol error', 0, + 'got a bad status line', None) fp = h.getfile() if errcode == 200: return addinfourl(fp, headers, "http:" + url) @@ -413,6 +417,10 @@ class URLopener: if data is not None: h.send(data) errcode, errmsg, headers = h.getreply() + if errcode == -1: + # something went wrong with the HTTP status line + raise IOError, ('http protocol error', 0, + 'got a bad status line', None) fp = h.getfile() if errcode == 200: return addinfourl(fp, headers, "https:" + url) diff --git a/Misc/NEWS b/Misc/NEWS index 355a827..be9e972 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -170,6 +170,10 @@ Core and builtins Library ------- +- Bug #767111: fix long-standing bug in urllib which caused an + AttributeError instead of an IOError when the server's response didn't + contain a valid HTTP status line. + - Patch #957650: "%var%" environment variable references are now properly expanded in ntpath.expandvars(), also "~user" home directory references are recognized and handled on Windows. -- cgit v0.12