Skip to content

Commit cd3d94a

Browse files
skipkayhilrafaelfranca
authored andcommitted
Fix raw_post raising when rack.input is nil
Starting in Rack 3.1, rack.input is optional, so `read_body_stream` (used by `raw_post`) can no longer call `read` unconditionally.
1 parent dcd4e10 commit cd3d94a

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix `Request#raw_post` raising `NoMethodError` when `rack.input` is `nil`.
2+
3+
*Hartley McGuire*
4+
15
* Remove `racc` dependency by manually writing `ActionDispatch::Journey::Scanner`.
26

37
*Gannon McGibbon*

actionpack/lib/action_dispatch/http/request.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,13 @@ def default_session
468468
end
469469

470470
def read_body_stream
471-
reset_stream(body_stream) do
472-
if has_header?(TRANSFER_ENCODING)
473-
body_stream.read # Read body stream until EOF if "Transfer-Encoding" is present
474-
else
475-
body_stream.read(content_length)
471+
if body_stream
472+
reset_stream(body_stream) do
473+
if has_header?(TRANSFER_ENCODING)
474+
body_stream.read # Read body stream until EOF if "Transfer-Encoding" is present
475+
else
476+
body_stream.read(content_length)
477+
end
476478
end
477479
end
478480
end

actionpack/test/dispatch/request_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,13 @@ class RequestParameters < BaseRequestTest
11991199
assert_not_nil e.cause
12001200
assert_equal e.cause.backtrace, e.backtrace
12011201
end
1202+
1203+
test "raw_post does not raise when rack.input is nil" do
1204+
request = stub_request
1205+
1206+
# "" on Rack < 3.1, nil on Rack 3.1+
1207+
assert_predicate request.raw_post, :blank?
1208+
end
12021209
end
12031210

12041211
class RequestParameterFilter < BaseRequestTest

0 commit comments

Comments
 (0)