diff options
author | Georg Brandl <georg@python.org> | 2005-08-24 18:46:39 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2005-08-24 18:46:39 (GMT) |
commit | b925602f169d47270a064cf9eb03e21706ed25c3 (patch) | |
tree | eb4e791e8253f66340e95dd7f19f57a0d9112f5c /Lib/urllib.py | |
parent | 568973181aa523bbcf7f827b3a2eb2affd96ea67 (diff) | |
download | cpython-b925602f169d47270a064cf9eb03e21706ed25c3.zip cpython-b925602f169d47270a064cf9eb03e21706ed25c3.tar.gz cpython-b925602f169d47270a064cf9eb03e21706ed25c3.tar.bz2 |
Patch [ 1062060 ] fix for 1016880 urllib.urlretrieve silently truncates dwnld
Diffstat (limited to 'Lib/urllib.py')
-rw-r--r-- | Lib/urllib.py | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py index 74b2aec..4f1ebdd 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -86,6 +86,11 @@ def urlcleanup(): if _urlopener: _urlopener.cleanup() +# exception raised when downloaded size does not match content-length +class ContentTooShortError(IOError): + def __init__(self, message, content): + IOError.__init__(self, message) + self.content = content ftpcache = {} class URLopener: @@ -228,24 +233,33 @@ class URLopener: self.tempcache[url] = result bs = 1024*8 size = -1 + read = 0 blocknum = 1 if reporthook: if "content-length" in headers: size = int(headers["Content-Length"]) reporthook(0, bs, size) block = fp.read(bs) + read += len(block) if reporthook: reporthook(1, bs, size) while block: tfp.write(block) block = fp.read(bs) - blocknum = blocknum + 1 + read += len(block) + blocknum += 1 if reporthook: reporthook(blocknum, bs, size) fp.close() tfp.close() del fp del tfp + + # raise exception if actual size does not match content-length header + if size >= 0 and read < size: + raise ContentTooShortError("retrieval incomplete: got only %i out " + "of %i bytes" % (read, size), result) + return result # Each method named open_<type> knows how to open that type of URL |