Project

General

Profile

Feature #14896

Expose Net::HTTPResponse socket for integration with libraries expecting an IO

Added by mudge (Paul Mucur) about 2 years ago. Updated about 2 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:87789]

Description

As several stdlib libraries support reading from IO-compatible objects (e.g. Zlib::GzipReader, Gem::Package::TarReader), would you be open to making the socket of a Net::HTTPResponse public?

This would enable such use cases as streaming a .tar.gz file with the following code:

uri = URI('http://www.example.com/huge.tar.gz')

Net::HTTP.start(uri.host, uri.port) do |http|
  http.request_get(uri) do |response|
    Zlib::GzipReader.wrap(response.socket) do |gz|
      Gem::Package::TarReader.new(gz) do |tar|
        tar.each do |entry|
          yield entry.read
        end
      end
    end
  end
end

We could document this with the same caveat as when passing a block for streaming bodies: noting that the response will not contain a "(meaningful) body" if the user does anything with the underlying socket.


Files

response-socket.patch (617 Bytes) response-socket.patch mudge (Paul Mucur), 07/04/2018 08:42 PM

Also available in: Atom PDF