class Webmachine::ChunkedBody

{ChunkedBody} is used to wrap an {Enumerable} object (like an enumerable {Response#body}) so it yields proper chunks for chunked transfer encoding.

case response.body
when String
  socket.write(response.body)
when Enumerable
  Webmachine::ChunkedBody.new(response.body).each do |chunk|
    socket.write(chunk)
  end
end

This is needed for Ruby webservers which don’t do the chunking themselves.

Constants

FINAL_CHUNK

Final chunk in any chunked-encoding response

Public Class Methods

new(body) click to toggle source

Creates a new {ChunkedBody} from the given {Enumerable}. @param [Enumerable] body the enumerable response body @return [ChunkedBody] the wrapped response body

# File lib/webmachine/chunked_body.rb, line 24
def initialize(body)
  @body = body
end

Public Instance Methods

each() { |[size, CRLF, chunk, CRLF].join| ... } click to toggle source

Calls the given block once for each chunk, passing that chunk as a parameter. Returns an {Enumerator} if no block is given.

# File lib/webmachine/chunked_body.rb, line 31
def each
  return to_enum unless block_given?

  @body.each do |chunk|
    size = chunk.bytesize
    next if size == 0
    yield([size.to_s(16), CRLF, chunk, CRLF].join)
  end
  yield(FINAL_CHUNK)
end