lib/rack/chunked.rb in rack-2.2.10 vs lib/rack/chunked.rb in rack-3.0.0.beta1

- old
+ new

@@ -1,24 +1,28 @@ # frozen_string_literal: true +require_relative 'constants' +require_relative 'utils' + module Rack + warn "Rack::Chunked is deprecated and will be removed in Rack 3.1", uplevel: 1 # Middleware that applies chunked transfer encoding to response bodies - # when the response does not include a Content-Length header. + # when the response does not include a content-length header. # - # This supports the Trailer response header to allow the use of trailing + # This supports the trailer response header to allow the use of trailing # headers in the chunked encoding. However, using this requires you manually # specify a response body that supports a +trailers+ method. Example: # - # [200, { 'Trailer' => 'Expires'}, ["Hello", "World"]] + # [200, { 'trailer' => 'expires'}, ["Hello", "World"]] # # error raised # # body = ["Hello", "World"] # def body.trailers - # { 'Expires' => Time.now.to_s } + # { 'expires' => Time.now.to_s } # end - # [200, { 'Trailer' => 'Expires'}, body] + # [200, { 'trailer' => 'expires'}, body] # # No exception raised class Chunked include Rack::Utils # A body wrapper that emits chunked responses. @@ -90,28 +94,27 @@ true end end # If the rack app returns a response that should have a body, - # but does not have Content-Length or Transfer-Encoding headers, - # modify the response to use chunked Transfer-Encoding. + # but does not have content-length or transfer-encoding headers, + # modify the response to use chunked transfer-encoding. def call(env) - status, headers, body = @app.call(env) - headers = HeaderHash[headers] + status, headers, body = response = @app.call(env) if chunkable_version?(env[SERVER_PROTOCOL]) && !STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) && !headers[CONTENT_LENGTH] && !headers[TRANSFER_ENCODING] headers[TRANSFER_ENCODING] = 'chunked' - if headers['Trailer'] - body = TrailerBody.new(body) + if headers['trailer'] + response[2] = TrailerBody.new(body) else - body = Body.new(body) + response[2] = Body.new(body) end end - [status, headers, body] + response end end end