Skip to content

Commit

Permalink
Better handling of multipart parsing when request is missing body.
Browse files Browse the repository at this point in the history
- Introduce `module Rack::BadRequest` for unified exception handling.
- Add `BadRequest` to query parser too.
- Make `env['rack.input']` optional.
  • Loading branch information
ioquatix committed Jan 17, 2023
1 parent 25de02a commit ecabf18
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/rack/request.rb
Expand Up @@ -507,13 +507,21 @@ def POST
end

begin
# If the form input was already memoized:
if get_header(RACK_REQUEST_FORM_INPUT) == get_header(RACK_INPUT)
return get_header(RACK_REQUEST_FORM_HASH)
rack_input = get_header(RACK_INPUT)

# If the form hash was already memoized:
if form_hash = get_header(RACK_REQUEST_FORM_HASH)
# And it was memoized from the same input:
if get_header(RACK_REQUEST_FORM_INPUT).equal?(rack_input)
return form_hash
end
end

# Otherwise, figure out how to parse the input:
if input? && (form_data? || parseable_data?)
if rack_input.nil?
set_header RACK_REQUEST_FORM_INPUT, nil
set_header(RACK_REQUEST_FORM_HASH, {})
elsif form_data? || parseable_data?
unless set_header(RACK_REQUEST_FORM_HASH, parse_multipart)
form_vars = get_header(RACK_INPUT).read

Expand Down

0 comments on commit ecabf18

Please sign in to comment.