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 0c10e7a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/rack/request.rb
Expand Up @@ -459,11 +459,6 @@ def content_charset
media_type_params['charset']
end

# Whether the request potentially has input.
def input?
!!get_header(RACK_INPUT)
end

# Determine whether the request body contains form-data by checking
# the request content-type for one of the media-types:
# "application/x-www-form-urlencoded" or "multipart/form-data". The
Expand Down Expand Up @@ -507,13 +502,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 0c10e7a

Please sign in to comment.