Class: Roda::RodaPlugins::Chunked::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/chunked.rb

Overview

Rack response body instance for chunked responses

Constant Summary collapse

CHUNK_SIZE =
"%x\r\n".freeze
CRLF =
"\r\n".freeze
FINISH =
"0\r\n\r\n".freeze

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ Body

Save the scope of the current request handling.



184
185
186
# File 'lib/roda/plugins/chunked.rb', line 184

def initialize(scope)
  @scope = scope
end

Instance Method Details

#eachObject

For each response chunk yielded by the scope, yield it it to the caller in chunked format, starting with the size of the request in ASCII hex format, then the chunk. After all chunks have been yielded, yield a 0 sized chunk to finish the response.



193
194
195
196
197
198
199
200
201
202
# File 'lib/roda/plugins/chunked.rb', line 193

def each
  @scope.each_chunk do |chunk|
    next if !chunk || chunk.empty?
    yield("%x\r\n" % chunk.bytesize)
    yield(chunk)
    yield("\r\n")
  end
ensure
  yield("0\r\n\r\n")
end