Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify use of @buffered and only update content-length when #finish. #2149

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 19 additions & 8 deletions lib/rack/response.rb
Expand Up @@ -99,7 +99,7 @@ def no_entity_body?
# The response body is an enumerable body and it is not allowed to have an entity body.
@body.respond_to?(:each) && STATUS_WITH_NO_ENTITY_BODY[@status]
end

# Generate a response array consistent with the requirements of the SPEC.
# @return [Array] a 3-tuple suitable of `[status, headers, body]`
# which is suitable to be returned from the middleware `#call(env)` method.
Expand All @@ -110,6 +110,10 @@ def finish(&block)
close
return [@status, @headers, []]
else
if @length && @length > 0 && !chunked?
set_header CONTENT_LENGTH, @length.to_s
end

if block_given?
@block = block
return [@status, @headers, self]
Expand Down Expand Up @@ -313,27 +317,37 @@ def etag=(value)

protected

# Convert the body of this response into an internally buffered Array if possible.
#
# `@buffered` is a ternary value which indicates whether the body is buffered. It can be:
# * `nil` - The body has not been buffered yet.
# * `true` - The body is buffered as an Array instance.
# * `false` - The body is not buffered and cannot be buffered.
#
# @return [Boolean] whether the body is buffered as an Array instance.
def buffered_body!
if @buffered.nil?
if @body.is_a?(Array)
# The user supplied body was an array:
@body = @body.compact
@body.each do |part|
@length += part.to_s.bytesize
end
@length = @body.sum{|part| part.bytesize}
@buffered = true
elsif @body.respond_to?(:each)
# Turn the user supplied body into a buffered array:
body = @body
@body = Array.new
@length = 0

body.each do |part|
@writer.call(part.to_s)
end

body.close if body.respond_to?(:close)

# We have converted the body into an Array:
@buffered = true
else
# We don't know how to buffer the user-supplied body:
@buffered = false
end
end
Expand All @@ -345,10 +359,7 @@ def append(chunk)
chunk = chunk.dup unless chunk.frozen?
@body << chunk

unless chunked?
@length += chunk.bytesize
set_header(CONTENT_LENGTH, @length.to_s)
end
@length += chunk.bytesize

return chunk
end
Expand Down
10 changes: 0 additions & 10 deletions test/spec_response.rb
Expand Up @@ -572,16 +572,6 @@ def obj.each
res.headers["content-length"].must_equal "10"
end

it "updates content-length when body appended to using #write" do
res = Rack::Response.new
res.status = 200
res.headers["content-length"].must_be_nil
res.write "Hi"
res.headers["content-length"].must_equal "2"
res.write " there"
res.headers["content-length"].must_equal "8"
end

it "does not wrap body" do
body = Object.new
res = Rack::Response.new(body)
Expand Down