diff options
author | Georg Brandl <georg@python.org> | 2007-03-14 08:27:57 +0000 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-03-14 08:27:57 +0000 |
commit | be62df2fd64156874e6484ce78c336dd017e56e6 (patch) | |
tree | c9f02b7b0d2b1035d3325002bd81dfbbead6450f /Lib | |
parent | 950ba3a65084e13cb862d82badf769d748ff374d (diff) | |
download | cpython-be62df2fd64156874e6484ce78c336dd017e56e6.tar.gz |
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.
(backport from rev. 54376)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_urllib.py | 9 | ||||
-rw-r--r-- | Lib/urllib.py | 8 |
2 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 4579c479d1..294ed5e06a 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 9fbb19a419..963187cfb2 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) |