Class: Net::HTTPResponse::Inflater

Inherits:
Object
  • Object
show all
Defined in:
lib/net/http/response.rb

Overview

Inflater is a wrapper around Net::BufferedIO that transparently inflates zlib and gzip streams.

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Inflater

Creates a new Inflater wrapping socket



347
348
349
350
351
# File 'lib/net/http/response.rb', line 347

def initialize socket
  @socket = socket
  # zlib with automatic gzip detection
  @inflate = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
end

Instance Method Details

#finishObject

Finishes the inflate stream.



356
357
358
# File 'lib/net/http/response.rb', line 356

def finish
  @inflate.finish
end

#inflate_adapter(dest) ⇒ Object

Returns a Net::ReadAdapter that inflates each read chunk into dest.

This allows a large response body to be inflated without storing the entire body in memory.



366
367
368
369
370
371
372
373
374
# File 'lib/net/http/response.rb', line 366

def inflate_adapter(dest)
  block = proc do |compressed_chunk|
    @inflate.inflate(compressed_chunk) do |chunk|
      dest << chunk
    end
  end

  Net::ReadAdapter.new(block)
end

#read(clen, dest, ignore_eof = false) ⇒ Object

Reads clen bytes from the socket, inflates them, then writes them to dest. ignore_eof is passed down to Net::BufferedIO#read

Unlike Net::BufferedIO#read, this method returns more than clen bytes. At this time there is no way for a user of Net::HTTPResponse to read a specific number of bytes from the HTTP response body, so this internal API does not return the same number of bytes as were requested.

See bugs.ruby-lang.org/issues/6492 for further discussion.



387
388
389
390
391
# File 'lib/net/http/response.rb', line 387

def read clen, dest, ignore_eof = false
  temp_dest = inflate_adapter(dest)

  @socket.read clen, temp_dest, ignore_eof
end

#read_all(dest) ⇒ Object

Reads the rest of the socket, inflates it, then writes it to dest.



396
397
398
399
400
# File 'lib/net/http/response.rb', line 396

def read_all dest
  temp_dest = inflate_adapter(dest)

  @socket.read_all temp_dest
end