class Bundler::Fetcher::Downloader

def fetch(uri, headers = {}, counter = 0)

def fetch(uri, headers = {}, counter = 0)
  raise HTTPError, "Too many redirects" if counter >= redirect_limit
  filtered_uri = URICredentialsFilter.credential_filtered_uri(uri)
  response = request(uri, headers)
  Bundler.ui.debug("HTTP #{response.code} #{response.message} #{filtered_uri}")
  case response
  when Net::HTTPSuccess, Net::HTTPNotModified
    response
  when Net::HTTPRedirection
    new_uri = Bundler::URI.parse(response["location"])
    if new_uri.host == uri.host
      new_uri.user = uri.user
      new_uri.password = uri.password
    end
    fetch(new_uri, headers, counter + 1)
  when Net::HTTPRequestedRangeNotSatisfiable
    new_headers = headers.dup
    new_headers.delete("Range")
    new_headers["Accept-Encoding"] = "gzip"
    fetch(uri, new_headers)
  when Net::HTTPRequestEntityTooLarge
    raise FallbackError, response.body
  when Net::HTTPTooManyRequests
    raise TooManyRequestsError, response.body
  when Net::HTTPUnauthorized
    raise BadAuthenticationError, uri.host if uri.userinfo
    raise AuthenticationRequiredError, uri.host
  when Net::HTTPNotFound
    raise FallbackError, "Net::HTTPNotFound: #{filtered_uri}"
  else
    raise HTTPError, "#{response.class}#{": #{response.body}" unless response.body.empty?}"
  end
end