Class: ZipKit::RackChunkedBody

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_kit/rack_chunked_body.rb

Overview

A body wrapper that emits chunked responses, creating valid Transfer-Encoding::Chunked HTTP response body. This is copied from Rack::Chunked::Body, because Rack is not going to include that class after version 3.x Rails has a substitute class for this inside ActionController::Streaming, but that module is a private constant in the Rails codebase, and is thus considered "private" from the Rails standpoint. It is not that much code to carry, so we copy it into our code.

Constant Summary collapse

TERM =
"\r\n"
TAIL =
"0#{TERM}"

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ RackChunkedBody

Returns a new instance of RackChunkedBody.

Parameters:

  • body (#each)

    the enumerable that yields bytes, usually a OutputEnumerator



15
16
17
# File 'lib/zip_kit/rack_chunked_body.rb', line 15

def initialize(body)
  @body = body
end

Instance Method Details

#each {|TAIL| ... } ⇒ Object

For each string yielded by the response body, yield the element in chunked encoding - and finish off with a terminator

Yields:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zip_kit/rack_chunked_body.rb', line 21

def each
  term = TERM
  @body.each do |chunk|
    size = chunk.bytesize
    next if size == 0

    yield [size.to_s(16), term, chunk.b, term].join
  end
  yield TAIL
  yield term
end