Move the rewind code closer to the reason why we need to rewind

We only need to rewind because we call `read_body_stream`. Since
that method is only called in one place, move the rewing to inside it.
This commit is contained in:
Rafael Mendonça França 2024-06-14 18:56:36 +00:00
parent c9075e3643
commit 3342f13d12
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
2 changed files with 22 additions and 5 deletions

@ -413,7 +413,7 @@ GEM
pg (>= 1.1, < 2.0)
raabro (1.4.0)
racc (1.8.0)
rack (3.0.11)
rack (3.1.3)
rack-cache (1.15.0)
rack (>= 0.4)
rack-session (2.0.0)

@ -340,7 +340,6 @@ def server_software
def raw_post
unless has_header? "RAW_POST_DATA"
set_header("RAW_POST_DATA", read_body_stream)
body_stream.rewind if body_stream.respond_to?(:rewind)
end
get_header "RAW_POST_DATA"
end
@ -467,9 +466,27 @@ def default_session
end
def read_body_stream
body_stream.rewind if body_stream.respond_to?(:rewind)
return body_stream.read if headers.key?("Transfer-Encoding") # Read body stream until EOF if "Transfer-Encoding" is present
body_stream.read(content_length)
reset_stream(body_stream) do
if headers.key?("Transfer-Encoding")
body_stream.read # Read body stream until EOF if "Transfer-Encoding" is present
else
body_stream.read(content_length)
end
end
end
def reset_stream(body_stream)
if body_stream.respond_to?(:rewind)
body_stream.rewind
content = yield
body_stream.rewind
content
else
yield
end
end
end
end