Completely disable gzip decoding when allow_compression=False. Add extra test.

This commit is contained in:
John Garland 2010-01-16 04:50:01 +00:00
parent aaa7dae18e
commit d9b9f22998
2 changed files with 23 additions and 3 deletions

View File

@ -45,7 +45,7 @@ class HTTPDownloader(client.HTTPDownloader):
"""
Factory class for downloading files and keeping track of progress.
"""
def __init__(self, url, filename, part_callback=None, headers=None, force_filename=False):
def __init__(self, url, filename, part_callback=None, headers=None, force_filename=False, allow_compression=True):
"""
:param url: the url to download from
:type url: string
@ -62,6 +62,7 @@ class HTTPDownloader(client.HTTPDownloader):
self.decoder = None
self.value = filename
self.force_filename = force_filename
self.allow_compression = allow_compression
agent = "Deluge/%s (http://deluge-torrent.org)" % get_version()
client.HTTPDownloader.__init__(self, url, filename, headers=headers, agent=agent)
@ -76,7 +77,8 @@ class HTTPDownloader(client.HTTPDownloader):
else:
self.total_length = 0
if "content-encoding" in headers and headers["content-encoding"][0] in ("gzip", "x-gzip", "deflate"):
if self.allow_compression and "content-encoding" in headers and \
headers["content-encoding"][0] in ("gzip", "x-gzip", "deflate"):
# Adding 32 to the wbits enables gzip & zlib decoding (with automatic header detection)
# Adding 16 just enables gzip decoding (no zlib)
self.decoder = zlib.decompressobj(zlib.MAX_WBITS + 32)
@ -186,7 +188,7 @@ def download_file(url, filename, callback=None, headers=None, force_filename=Fal
headers["accept-encoding"] = "gzip, deflate"
scheme, host, port, path = client._parse(url)
factory = HTTPDownloader(url, filename, callback, headers, force_filename)
factory = HTTPDownloader(url, filename, callback, headers, force_filename, allow_compression)
if scheme == "https":
from twisted.internet import ssl
reactor.connectSSL(host, port, factory, ssl.ClientContextFactory())

View File

@ -17,6 +17,18 @@ class DownloadFileTestCase(unittest.TestCase):
f = open(filename)
try:
self.assertEqual(f.read(), contents)
except Exception, e:
self.fail(e)
finally:
f.close()
return filename
def failIfContains(self, filename, contents):
f = open(filename)
try:
self.failIfEqual(f.read(), contents)
except Exception, e:
self.fail(e)
finally:
f.close()
return filename
@ -75,6 +87,12 @@ class DownloadFileTestCase(unittest.TestCase):
d.addCallback(self.assertContains, "success")
return d
def test_download_with_gzip_encoding_disabled(self):
url = "http://deluge-torrent.org/httpdownloader.php?test=gzip&msg=fail"
d = download_file(url, "gzip_encoded", allow_compression=False)
d.addCallback(self.failIfContains, "fail")
return d
def test_page_redirect(self):
url = "http://deluge-torrent.org/httpdownloader.php?test=redirect"
d = download_file(url, "none")