Skip to content

Commit 3342f13

Browse files
committed
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.
1 parent c9075e3 commit 3342f13

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ GEM
413413
pg (>= 1.1, < 2.0)
414414
raabro (1.4.0)
415415
racc (1.8.0)
416-
rack (3.0.11)
416+
rack (3.1.3)
417417
rack-cache (1.15.0)
418418
rack (>= 0.4)
419419
rack-session (2.0.0)

actionpack/lib/action_dispatch/http/request.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ def server_software
340340
def raw_post
341341
unless has_header? "RAW_POST_DATA"
342342
set_header("RAW_POST_DATA", read_body_stream)
343-
body_stream.rewind if body_stream.respond_to?(:rewind)
344343
end
345344
get_header "RAW_POST_DATA"
346345
end
@@ -467,9 +466,27 @@ def default_session
467466
end
468467

469468
def read_body_stream
470-
body_stream.rewind if body_stream.respond_to?(:rewind)
471-
return body_stream.read if headers.key?("Transfer-Encoding") # Read body stream until EOF if "Transfer-Encoding" is present
472-
body_stream.read(content_length)
469+
reset_stream(body_stream) do
470+
if headers.key?("Transfer-Encoding")
471+
body_stream.read # Read body stream until EOF if "Transfer-Encoding" is present
472+
else
473+
body_stream.read(content_length)
474+
end
475+
end
476+
end
477+
478+
def reset_stream(body_stream)
479+
if body_stream.respond_to?(:rewind)
480+
body_stream.rewind
481+
482+
content = yield
483+
484+
body_stream.rewind
485+
486+
content
487+
else
488+
yield
489+
end
473490
end
474491
end
475492
end

0 commit comments

Comments
 (0)