From 0c10e7ab0a79ebaf37626851ed93aff7cc6c2e84 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 17 Jan 2023 20:11:24 +1300 Subject: [PATCH] Better handling of multipart parsing when request is missing body. - Introduce `module Rack::BadRequest` for unified exception handling. - Add `BadRequest` to query parser too. - Make `env['rack.input']` optional. --- lib/rack/request.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/rack/request.rb b/lib/rack/request.rb index 02e27a377..c364cca2f 100644 --- a/lib/rack/request.rb +++ b/lib/rack/request.rb @@ -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 @@ -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