# Path: deluge/httpdownloader.py
# def download_file(url, filename, callback=None, headers=None, force_filename=False, allow_compression=True):
#     """
#     Downloads a file from a specific URL and returns a Deferred.  You can also
#     specify a callback function to be called as parts are received.
# 
#     :param url: the url to download from
#     :type url: string
#     :param filename: the filename to save the file as
#     :type filename: string
#     :param callback: a function to be called when a part of data is received,
#          it's signature should be: func(data, current_length, total_length)
#     :type callback: function
#     :param headers: any optional headers to send
#     :type headers: dictionary
#     :param force_filename: force us to use the filename specified rather than
#                            one the server may suggest
#     :type force_filename: boolean
#     :param allow_compression: allows gzip & deflate decoding
#     :type allow_compression: boolean
# 
#     :returns: the filename of the downloaded file
#     :rtype: Deferred
# 
#     :raises t.w.e.PageRedirect: when server responds with a temporary redirect
#          or permanently moved.
#     :raises t.w.e.Error: for all other HTTP response errors (besides OK)
#     """
#     url = str(url)
#     filename = str(filename)
#     if headers:
#         for key, value in headers.items():
#             headers[str(key)] = str(value)
# 
#     if allow_compression:
#         if not headers:
#             headers = {}
#         headers["accept-encoding"] = "deflate, gzip, x-gzip"
# 
#     scheme, host, port, path = client._parse(url)
#     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())
#     else:
#         reactor.connectTCP(host, port, factory)
# 
#     return factory.deferred


from twisted.trial import unittest
from twisted.python.failure import Failure
from deluge.httpdownloader import download_file
from deluge.log import setupLogger
from email.utils import formatdate
            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

    def test_download(self):
        d = download_file("http://deluge-torrent.org", "index.html")
        d.addCallback(self.assertEqual, "index.html")
        return d

    def test_download_without_required_cookies(self):
        url = "http://deluge-torrent.org/httpdownloader.php?test=cookie"
        d = download_file(url, "none")
        d.addCallback(self.fail)
        d.addErrback(self.assertIsInstance, Failure)
        return d

    def test_download_with_required_cookies(self):
        url = "http://deluge-torrent.org/httpdownloader.php?test=cookie"
        cookie = { "cookie" : "password=deluge" }

# Path: deluge/httpdownloader.py
# def download_file(url, filename, callback=None, headers=None, force_filename=False, allow_compression=True):
#     """
#     Downloads a file from a specific URL and returns a Deferred.  You can also
#     specify a callback function to be called as parts are received.
# 
#     :param url: the url to download from
#     :type url: string
#     :param filename: the filename to save the file as
#     :type filename: string
#     :param callback: a function to be called when a part of data is received,
#          it's signature should be: func(data, current_length, total_length)
#     :type callback: function
#     :param headers: any optional headers to send
#     :type headers: dictionary
#     :param force_filename: force us to use the filename specified rather than
#                            one the server may suggest
#     :type force_filename: boolean
#     :param allow_compression: allows gzip & deflate decoding
#     :type allow_compression: boolean
# 
#     :returns: the filename of the downloaded file
#     :rtype: Deferred
# 
#     :raises t.w.e.PageRedirect: when server responds with a temporary redirect
#          or permanently moved.
#     :raises t.w.e.Error: for all other HTTP response errors (besides OK)
#     """
#     url = str(url)
#     filename = str(filename)
#     if headers:
#         for key, value in headers.items():
#             headers[str(key)] = str(value)
# 
#     if allow_compression:
#         if not headers:
#             headers = {}
#         headers["accept-encoding"] = "deflate, gzip, x-gzip"
# 
#     scheme, host, port, path = client._parse(url)
#     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())
#     else:
#         reactor.connectTCP(host, port, factory)
# 
#     return factory.deferred


# Path: tests/test_httpdownloader.py
from twisted.trial import unittest
from twisted.python.failure import Failure
from deluge.httpdownloader import download_file
from deluge.log import setupLogger
from email.utils import formatdate

            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

    def test_download(self):
        d = download_file("http://deluge-torrent.org", "index.html")
        d.addCallback(self.assertEqual, "index.html")
        return d

    def test_download_without_required_cookies(self):
        url = "http://deluge-torrent.org/httpdownloader.php?test=cookie"
        d = download_file(url, "none")
        d.addCallback(self.fail)
        d.addErrback(self.assertIsInstance, Failure)
        return d

    def test_download_with_required_cookies(self):
        url = "http://deluge-torrent.org/httpdownloader.php?test=cookie"
        cookie = { "cookie" : "password=deluge" }

        d = download_file(url, "monster", headers=cookie)