code: def download(self, f=None, pbar=None, size=None):
      response = self.response
      # content_gzipped = 'gzip' in response.headers.get('content-encoding', '').split(',')
      # if content_gzipped:
      #     raise NotImplemented("We do not support (yet) gzipped content")
      #     # see https://rationalpie.wordpress.com/2010/06/02/python-streaming-gzip-decompression/
      #     # for ways to implement in python 2 and 3.2's gzip is working better with streams
  
      total = 0
      return_content = f is None
      if f is None:
          # no file to download to
          # TODO: actually strange since it should have been decoded then...
          f = io.BytesIO()
  
      # must use .raw to be able avoiding decoding/decompression while downloading
      # to a file
      chunk_size_ = (
          min(self.chunk_size, size)
          if size is not None
          else self.chunk_size
      )
  
      # XXX With requests_ftp BytesIO is provided as response.raw for ftp urls,
      # which has no .stream, so let's do ducktyping and provide our custom stream
      # via BufferedReader for such cases, while maintaining the rest of code
      # intact.  TODO: figure it all out, since doesn't scale for any sizeable download
      # This code is tested by tests/test_http.py:test_download_ftp BUT
      # it causes 503 on travis,  but not always so we allow to skip that test
      # in such cases. That causes fluctuating coverage
      if not hasattr(response.raw, "stream"):  # pragma: no cover
  
          def _stream():
              buf = io.BufferedReader(response.raw)
              v = True
              while v:
                  v = buf.read(chunk_size_)
                  yield v
  
          stream = _stream()
      else:
          # XXX TODO -- it must be just a dirty workaround
          # As we discovered with downloads from NITRC all headers come with
          # Content-Encoding: gzip which leads  requests to decode them.  But the point
          # is that ftp links (yoh doesn't think) are gzip compressed for the transfer
          decode_content = not response.url.startswith("ftp://")
          stream = response.raw.stream(
              chunk_size_, decode_content=decode_content
          )
  
      for chunk in stream:
          if chunk:  # filter out keep-alive new chunks
              chunk_len = len(chunk)
              if size is not None and total + chunk_len > size:
                  # trim the download to match target size
                  chunk = chunk[: size - total]
                  chunk_len = len(chunk)
              total += chunk_len
              f.write(chunk)
              try:
                  # TODO: pbar is not robust ATM against > 100% performance ;)
                  if pbar:
                      pbar.update(total)
              except Exception as e:
                  ce = CapturedException(e)
                  lgr.warning("Failed to update progressbar: %s", ce)
              # TEMP
              # see https://github.com/niltonvolpato/python-progressbar/pull/44
              ui.out.flush()
              if size is not None and total >= size:  # pragma: no cover
                  break  # we have done as much as we were asked
  
      if return_content:
          out = f.getvalue()
          return out
  
imports: import re
  import requests
  import requests.auth
  from requests.utils import parse_dict_header
  import io
  from time import sleep
  from ..None import __version__
  from ..utils import ensure_list_from_str, ensure_dict_from_str, ensure_bytes
  from ..dochelpers import borrowkwargs
  from ..ui import ui
  from ..utils import auto_repr
  from ..support.network import get_url_filename
  from ..support.network import get_response_disposition_filename
  from ..support.network import rfc2822_to_epoch
  from ..support.cookies import cookies_db
  from ..support.status import FileStatus
  from ..support.exceptions import AccessDeniedError, AccessFailedError, CapturedException, DownloadError, UnhandledRedirectError
  from .base import Authenticator
  from .base import BaseDownloader, DownloaderSession
  from logging import getLogger
  from ..log import LoggerHelper
label_desc: Use image node_id 2 for calling the HTTPDownloaderSession obj's underlying member method code with example usage: obj.download(f, pbar, size) and returns: out