Undefine #to_ary in ActionController::Live::Buffer

ActionDispatch::Response delegates #to_ary to the internal ActionDispatch::Response::Buffer,
defining #to_ary is an indicator that the response body can be buffered and/or cached by
Rack middlewares, this is not the case for Live responses so we undefine it for this Buffer subclass.

Puma raises an exception trying to call #to_ary in Live::Buffer
expecting it to return an array if defined:

188f5da192/lib/puma/request.rb (L183-L186)
This commit is contained in:
Guillermo Iguaran 2023-06-11 04:07:48 -07:00
parent f642505479
commit 0174283901
2 changed files with 15 additions and 0 deletions

@ -167,6 +167,11 @@ def initialize(response)
@ignore_disconnect = false
end
# ActionDispatch::Response delegates #to_ary to the internal ActionDispatch::Response::Buffer,
# defining #to_ary is an indicator that the response body can be buffered and/or cached by
# Rack middlewares, this is not the case for Live responses so we undefine it for this Buffer subclass.
undef_method :to_ary
def write(string)
unless @response.committed?
@response.headers["Cache-Control"] ||= "no-cache"

@ -319,6 +319,11 @@ def ignore_client_disconnect
logger.info "Work complete"
latch.count_down
end
def buffer_do_not_respond_to_to_ary
response.stream.write "response.stream.respond_to? = #{response.stream.respond_to?(:to_ary)}"
response.stream.close
end
end
tests TestController
@ -596,6 +601,11 @@ def test_stale_with_etag
get :with_stale
assert_equal 304, response.status.to_i
end
def test_response_buffer_do_not_respond_to_to_ary
get :buffer_do_not_respond_to_to_ary
assert_equal "response.stream.respond_to? = false", response.body
end
end
class BufferTest < ActionController::TestCase