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. 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 :param url: the url to download from
:type url: string :type url: string
@ -62,6 +62,7 @@ class HTTPDownloader(client.HTTPDownloader):
self.decoder = None self.decoder = None
self.value = filename self.value = filename
self.force_filename = force_filename self.force_filename = force_filename
self.allow_compression = allow_compression
agent = "Deluge/%s (http://deluge-torrent.org)" % get_version() agent = "Deluge/%s (http://deluge-torrent.org)" % get_version()
client.HTTPDownloader.__init__(self, url, filename, headers=headers, agent=agent) client.HTTPDownloader.__init__(self, url, filename, headers=headers, agent=agent)
@ -76,7 +77,8 @@ class HTTPDownloader(client.HTTPDownloader):
else: else:
self.total_length = 0 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 32 to the wbits enables gzip & zlib decoding (with automatic header detection)
# Adding 16 just enables gzip decoding (no zlib) # Adding 16 just enables gzip decoding (no zlib)
self.decoder = zlib.decompressobj(zlib.MAX_WBITS + 32) 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" headers["accept-encoding"] = "gzip, deflate"
scheme, host, port, path = client._parse(url) 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": if scheme == "https":
from twisted.internet import ssl from twisted.internet import ssl
reactor.connectSSL(host, port, factory, ssl.ClientContextFactory()) reactor.connectSSL(host, port, factory, ssl.ClientContextFactory())

View File

@ -17,6 +17,18 @@ class DownloadFileTestCase(unittest.TestCase):
f = open(filename) f = open(filename)
try: try:
self.assertEqual(f.read(), contents) 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: finally:
f.close() f.close()
return filename return filename
@ -75,6 +87,12 @@ class DownloadFileTestCase(unittest.TestCase):
d.addCallback(self.assertContains, "success") d.addCallback(self.assertContains, "success")
return d 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): def test_page_redirect(self):
url = "http://deluge-torrent.org/httpdownloader.php?test=redirect" url = "http://deluge-torrent.org/httpdownloader.php?test=redirect"
d = download_file(url, "none") d = download_file(url, "none")