5 # Middleware that applies chunked transfer encoding to response bodies
6 # when the response does not include a Content-Length header.
15 status
, headers
, body
= @app.call(env)
16 headers
= HeaderHash
.new(headers
)
18 if env['HTTP_VERSION'] == 'HTTP/1.0' ||
19 STATUS_WITH_NO_ENTITY_BODY
.include?(status
) ||
20 headers
['Content-Length'] ||
21 headers
['Transfer-Encoding']
22 [status
, headers
.to_hash
, body
]
24 dup
.chunk(status
, headers
, body
)
28 def chunk(status
, headers
, body
)
30 headers
.delete('Content-Length')
31 headers
['Transfer-Encoding'] = 'chunked'
32 [status
, headers
.to_hash
, self]
38 size
= bytesize(chunk
)
40 yield [size
.to_s(16), term
, chunk
, term
].join
42 yield ["0", term
, "", term
].join
46 @body.close
if @body.respond_to
?(:close)