rack 3.1.18 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rack might be problematic. Click here for more details.

@@ -14,12 +14,11 @@ module Rack
14
14
  # For more information on the use of media types in HTTP, see:
15
15
  # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
16
16
  def type(content_type)
17
- return nil unless content_type
18
- if type = content_type.split(SPLIT_PATTERN, 2).first
19
- type.rstrip!
20
- type.downcase!
21
- type
22
- end
17
+ return nil unless content_type && !content_type.empty?
18
+ type = content_type.split(SPLIT_PATTERN, 2).first
19
+ type.rstrip!
20
+ type.downcase!
21
+ type
23
22
  end
24
23
 
25
24
  # The media type parameters provided in CONTENT_TYPE as a Hash, or
@@ -33,7 +32,7 @@ module Rack
33
32
  # and "text/plain;charset" will return { 'charset' => '' }, similarly to
34
33
  # the query params parser (barring the latter case, which returns nil instead)).
35
34
  def params(content_type)
36
- return {} if content_type.nil?
35
+ return {} if content_type.nil? || content_type.empty?
37
36
 
38
37
  content_type.split(SPLIT_PATTERN)[1..-1].each_with_object({}) do |s, hsh|
39
38
  s.strip!
@@ -10,33 +10,27 @@ module Rack
10
10
  # MockRequest.
11
11
 
12
12
  class MockResponse < Rack::Response
13
- begin
14
- # Recent versions of the CGI gem may not provide `CGI::Cookie`.
15
- require 'cgi/cookie'
16
- Cookie = CGI::Cookie
17
- rescue LoadError
18
- class Cookie
19
- attr_reader :name, :value, :path, :domain, :expires, :secure
20
-
21
- def initialize(args)
22
- @name = args["name"]
23
- @value = args["value"]
24
- @path = args["path"]
25
- @domain = args["domain"]
26
- @expires = args["expires"]
27
- @secure = args["secure"]
28
- end
13
+ class Cookie
14
+ attr_reader :name, :value, :path, :domain, :expires, :secure
15
+
16
+ def initialize(args)
17
+ @name = args["name"]
18
+ @value = args["value"]
19
+ @path = args["path"]
20
+ @domain = args["domain"]
21
+ @expires = args["expires"]
22
+ @secure = args["secure"]
23
+ end
29
24
 
30
- def method_missing(method_name, *args, &block)
31
- @value.send(method_name, *args, &block)
32
- end
33
- # :nocov:
34
- ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
35
- # :nocov:
25
+ def method_missing(method_name, *args, &block)
26
+ @value.send(method_name, *args, &block)
27
+ end
28
+ # :nocov:
29
+ ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
30
+ # :nocov:
36
31
 
37
- def respond_to_missing?(method_name, include_all = false)
38
- @value.respond_to?(method_name, include_all) || super
39
- end
32
+ def respond_to_missing?(method_name, include_all = false)
33
+ @value.respond_to?(method_name, include_all) || super
40
34
  end
41
35
  end
42
36
 
@@ -38,6 +38,18 @@ module Rack
38
38
  MULTIPART_CONTENT_DISPOSITION = /^Content-Disposition:#{FWS}?(#{HEADER_VALUE})/ni
39
39
  MULTIPART_CONTENT_ID = /^Content-ID:#{FWS}?(#{HEADER_VALUE})/ni
40
40
 
41
+ # Rack::Multipart::Parser handles parsing of multipart/form-data requests.
42
+ #
43
+ # File Parameter Contents
44
+ #
45
+ # When processing file uploads, the parser returns a hash containing
46
+ # information about uploaded files. For +file+ parameters, the hash includes:
47
+ #
48
+ # * +:filename+ - The original filename, already URL decoded by the parser
49
+ # * +:type+ - The content type of the uploaded file
50
+ # * +:name+ - The parameter name from the form
51
+ # * +:tempfile+ - A Tempfile object containing the uploaded data
52
+ # * +:head+ - The raw header content for this part
41
53
  class Parser
42
54
  BUFSIZE = 1_048_576
43
55
  TEXT_PLAIN = "text/plain"
@@ -47,27 +59,6 @@ module Rack
47
59
  Tempfile.new(["RackMultipart", extension])
48
60
  }
49
61
 
50
- BOUNDARY_START_LIMIT = 16 * 1024
51
- private_constant :BOUNDARY_START_LIMIT
52
-
53
- MIME_HEADER_BYTESIZE_LIMIT = 64 * 1024
54
- private_constant :MIME_HEADER_BYTESIZE_LIMIT
55
-
56
- env_int = lambda do |key, val|
57
- if str_val = ENV[key]
58
- begin
59
- val = Integer(str_val, 10)
60
- rescue ArgumentError
61
- raise ArgumentError, "non-integer value provided for environment variable #{key}"
62
- end
63
- end
64
-
65
- val
66
- end
67
-
68
- BUFFERED_UPLOAD_BYTESIZE_LIMIT = env_int.call("RACK_MULTIPART_BUFFERED_UPLOAD_BYTESIZE_LIMIT", 16 * 1024 * 1024)
69
- private_constant :BUFFERED_UPLOAD_BYTESIZE_LIMIT
70
-
71
62
  class BoundedIO # :nodoc:
72
63
  def initialize(io, content_length)
73
64
  @io = io
@@ -227,8 +218,6 @@ module Rack
227
218
 
228
219
  @state = :FAST_FORWARD
229
220
  @mime_index = 0
230
- @body_retained = nil
231
- @retained_size = 0
232
221
  @collector = Collector.new tempfile
233
222
 
234
223
  @sbuf = StringScanner.new("".dup)
@@ -266,7 +255,8 @@ module Rack
266
255
  @collector.each do |part|
267
256
  part.get_data do |data|
268
257
  tag_multipart_encoding(part.filename, part.content_type, part.name, data)
269
- @query_parser.normalize_params(@params, part.name, data)
258
+ name, data = handle_dummy_encoding(part.name, data)
259
+ @query_parser.normalize_params(@params, name, data)
270
260
  end
271
261
  end
272
262
  MultipartInfo.new @params.to_params_hash, @collector.find_all(&:file?).map(&:body)
@@ -274,12 +264,6 @@ module Rack
274
264
 
275
265
  private
276
266
 
277
- def dequote(str) # From WEBrick::HTTPUtils
278
- ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
279
- ret.gsub!(/\\(.)/, "\\1")
280
- ret
281
- end
282
-
283
267
  def read_data(io, outbuf)
284
268
  content = io.read(@bufsize, outbuf)
285
269
  handle_empty_content!(content)
@@ -310,10 +294,6 @@ module Rack
310
294
 
311
295
  # retry for opening boundary
312
296
  else
313
- # We raise if we don't find the multipart boundary, to avoid unbounded memory
314
- # buffering. Note that the actual limit is the higher of 16KB and the buffer size (1MB by default)
315
- raise Error, "multipart boundary not found within limit" if @sbuf.string.bytesize > BOUNDARY_START_LIMIT
316
-
317
297
  # no boundary found, keep reading data
318
298
  return :want_read
319
299
  end
@@ -430,30 +410,16 @@ module Rack
430
410
  name = filename || "#{content_type || TEXT_PLAIN}[]".dup
431
411
  end
432
412
 
433
- # Mime part head data is retained for both TempfilePart and BufferPart
434
- # for the entireity of the parse, even though it isn't used for BufferPart.
435
- update_retained_size(head.bytesize)
436
-
437
- # If a filename is given, a TempfilePart will be used, so the body will
438
- # not be buffered in memory. However, if a filename is not given, a BufferPart
439
- # will be used, and the body will be buffered in memory.
440
- @body_retained = !filename
441
-
442
413
  @collector.on_mime_head @mime_index, head, filename, content_type, name
443
414
  @state = :MIME_BODY
444
415
  else
445
- # We raise if the mime part header is too large, to avoid unbounded memory
446
- # buffering. Note that the actual limit is the higher of 64KB and the buffer size (1MB by default)
447
- raise Error, "multipart mime part header too large" if @sbuf.string.bytesize > MIME_HEADER_BYTESIZE_LIMIT
448
-
449
- return :want_read
416
+ :want_read
450
417
  end
451
418
  end
452
419
 
453
420
  def handle_mime_body
454
421
  if (body_with_boundary = @sbuf.check_until(@body_regex)) # check but do not advance the pointer yet
455
422
  body = body_with_boundary.sub(@body_regex_at_end, '') # remove the boundary from the string
456
- update_retained_size(body.bytesize) if @body_retained
457
423
  @collector.on_mime_body @mime_index, body
458
424
  @sbuf.pos += body.length + 2 # skip \r\n after the content
459
425
  @state = :CONSUME_TOKEN
@@ -462,9 +428,7 @@ module Rack
462
428
  # Save what we have so far
463
429
  if @rx_max_size < @sbuf.rest_size
464
430
  delta = @sbuf.rest_size - @rx_max_size
465
- body = @sbuf.peek(delta)
466
- update_retained_size(body.bytesize) if @body_retained
467
- @collector.on_mime_body @mime_index, body
431
+ @collector.on_mime_body @mime_index, @sbuf.peek(delta)
468
432
  @sbuf.pos += delta
469
433
  @sbuf.string = @sbuf.rest
470
434
  end
@@ -472,13 +436,6 @@ module Rack
472
436
  end
473
437
  end
474
438
 
475
- def update_retained_size(size)
476
- @retained_size += size
477
- if @retained_size > BUFFERED_UPLOAD_BYTESIZE_LIMIT
478
- raise Error, "multipart data over retained size limit"
479
- end
480
- end
481
-
482
439
  # Scan until the we find the start or end of the boundary.
483
440
  # If we find it, return the appropriate symbol for the start or
484
441
  # end of the boundary. If we don't find the start or end of the
@@ -544,6 +501,25 @@ module Rack
544
501
  Encoding::BINARY
545
502
  end
546
503
 
504
+ REENCODE_DUMMY_ENCODINGS = {
505
+ # ISO-2022-JP is a legacy but still widely used encoding in Japan
506
+ # Here we convert ISO-2022-JP to UTF-8 so that it can be handled.
507
+ Encoding::ISO_2022_JP => true
508
+
509
+ # Other dummy encodings are rarely used and have not been supported yet.
510
+ # Adding support for them will require careful considerations.
511
+ }
512
+
513
+ def handle_dummy_encoding(name, body)
514
+ # A string object with a 'dummy' encoding does not have full functionality and can cause errors.
515
+ # So here we covert it to UTF-8 so that it can be handled properly.
516
+ if name.encoding.dummy? && REENCODE_DUMMY_ENCODINGS[name.encoding]
517
+ name = name.encode(Encoding::UTF_8)
518
+ body = body.encode(Encoding::UTF_8)
519
+ end
520
+ return name, body
521
+ end
522
+
547
523
  def handle_empty_content!(content)
548
524
  if content.nil? || content.empty?
549
525
  raise EmptyContentError
@@ -5,14 +5,47 @@ require 'fileutils'
5
5
 
6
6
  module Rack
7
7
  module Multipart
8
+ # Despite the misleading name, UploadedFile is designed for use for
9
+ # preparing multipart file upload bodies, generally for use in tests.
10
+ # It is not designed for and should not be used for handling uploaded
11
+ # files (there is no need for that, since Rack's multipart parser
12
+ # already creates Tempfiles for that). Using this with non-trusted
13
+ # filenames can create a security vulnerability.
14
+ #
15
+ # You should only use this class if you plan on passing the instances
16
+ # to Rack::MockRequest for use in creating multipart request bodies.
17
+ #
18
+ # UploadedFile delegates most methods to the tempfile it contains.
8
19
  class UploadedFile
9
-
10
- # The filename, *not* including the path, of the "uploaded" file
20
+ # The provided name of the file. This generally is the basename of
21
+ # path provided during initialization, but it can contain slashes if they
22
+ # were present in the filename argument when the instance was created.
11
23
  attr_reader :original_filename
12
24
 
13
- # The content type of the "uploaded" file
25
+ # The content type of the instance.
14
26
  attr_accessor :content_type
15
27
 
28
+ # Create a new UploadedFile. For backwards compatibility, this accepts
29
+ # both positional and keyword versions of the same arguments:
30
+ #
31
+ # filepath/path :: The path to the file
32
+ # ct/content_type :: The content_type of the file
33
+ # bin/binary :: Whether to set binmode on the file before copying data into it.
34
+ #
35
+ # If both positional and keyword arguments are present, the keyword arguments
36
+ # take precedence.
37
+ #
38
+ # The following keyword-only arguments are also accepted:
39
+ #
40
+ # filename :: Override the filename to use for the file. This is so the
41
+ # filename for the upload does not need to match the basename of
42
+ # the file path. This should not contain slashes, unless you are
43
+ # trying to test how an application handles invalid filenames in
44
+ # multipart upload bodies.
45
+ # io :: Use the given IO-like instance as the tempfile, instead of creating
46
+ # a Tempfile instance. This is useful for building multipart file
47
+ # upload bodies without a file being present on the filesystem. If you are
48
+ # providing this, you should also provide the filename argument.
16
49
  def initialize(filepath = nil, ct = "text/plain", bin = false,
17
50
  path: filepath, content_type: ct, binary: bin, filename: nil, io: nil)
18
51
  if io
@@ -28,15 +61,19 @@ module Rack
28
61
  @content_type = content_type
29
62
  end
30
63
 
64
+ # The path of the tempfile for the instance, if the tempfile has a path.
65
+ # nil if the tempfile does not have a path.
31
66
  def path
32
67
  @tempfile.path if @tempfile.respond_to?(:path)
33
68
  end
34
69
  alias_method :local_path, :path
35
70
 
36
- def respond_to?(*args)
37
- super or @tempfile.respond_to?(*args)
71
+ # Return true if the tempfile responds to the method.
72
+ def respond_to_missing?(*args)
73
+ @tempfile.respond_to?(*args)
38
74
  end
39
75
 
76
+ # Delegate method missing calls to the tempfile.
40
77
  def method_missing(method_name, *args, &block) #:nodoc:
41
78
  @tempfile.__send__(method_name, *args, &block)
42
79
  end
@@ -57,8 +57,6 @@ module Rack
57
57
  PARAMS_LIMIT = env_int.call("RACK_QUERY_PARSER_PARAMS_LIMIT", 4096)
58
58
  private_constant :PARAMS_LIMIT
59
59
 
60
- attr_reader :bytesize_limit
61
-
62
60
  def initialize(params_class, param_depth_limit, bytesize_limit: BYTESIZE_LIMIT, params_limit: PARAMS_LIMIT)
63
61
  @params_class = params_class
64
62
  @param_depth_limit = param_depth_limit
@@ -71,14 +69,9 @@ module Rack
71
69
  # to parse cookies by changing the characters used in the second parameter
72
70
  # (which defaults to '&').
73
71
  def parse_query(qs, separator = nil, &unescaper)
74
- unescaper ||= method(:unescape)
75
-
76
72
  params = make_params
77
73
 
78
- check_query_string(qs, separator).split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP).each do |p|
79
- next if p.empty?
80
- k, v = p.split('=', 2).map!(&unescaper)
81
-
74
+ each_query_pair(qs, separator, unescaper) do |k, v|
82
75
  if cur = params[k]
83
76
  if cur.class == Array
84
77
  params[k] << v
@@ -93,6 +86,19 @@ module Rack
93
86
  return params.to_h
94
87
  end
95
88
 
89
+ # Parses a query string by breaking it up at the '&', returning all key-value
90
+ # pairs as an array of [key, value] arrays. Unlike parse_query, this preserves
91
+ # all duplicate keys rather than collapsing them.
92
+ def parse_query_pairs(qs, separator = nil)
93
+ pairs = []
94
+
95
+ each_query_pair(qs, separator) do |k, v|
96
+ pairs << [k, v]
97
+ end
98
+
99
+ pairs
100
+ end
101
+
96
102
  # parse_nested_query expands a query string into structural types. Supported
97
103
  # types are Arrays, Hashes and basic value types. It is possible to supply
98
104
  # query strings with parameters of conflicting types, in this case a
@@ -101,17 +107,11 @@ module Rack
101
107
  def parse_nested_query(qs, separator = nil)
102
108
  params = make_params
103
109
 
104
- unless qs.nil? || qs.empty?
105
- check_query_string(qs, separator).split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP).each do |p|
106
- k, v = p.split('=', 2).map! { |s| unescape(s) }
107
-
108
- _normalize_params(params, k, v, 0)
109
- end
110
+ each_query_pair(qs, separator) do |k, v|
111
+ _normalize_params(params, k, v, 0)
110
112
  end
111
113
 
112
114
  return params.to_h
113
- rescue ArgumentError => e
114
- raise InvalidParameterError, e.message, e.backtrace
115
115
  end
116
116
 
117
117
  # normalize_params recursively expands parameters into structural types. If
@@ -217,20 +217,35 @@ module Rack
217
217
  true
218
218
  end
219
219
 
220
- def check_query_string(qs, sep)
221
- if qs
222
- if qs.bytesize > @bytesize_limit
223
- raise QueryLimitError, "total query size exceeds limit (#{@bytesize_limit})"
224
- end
220
+ def each_query_pair(qs, separator, unescaper = nil)
221
+ return if !qs || qs.empty?
225
222
 
226
- if (param_count = qs.count(sep.is_a?(String) ? sep : '&')) >= @params_limit
227
- raise QueryLimitError, "total number of query parameters (#{param_count+1}) exceeds limit (#{@params_limit})"
228
- end
223
+ if qs.bytesize > @bytesize_limit
224
+ raise QueryLimitError, "total query size (#{qs.bytesize}) exceeds limit (#{@bytesize_limit})"
225
+ end
226
+
227
+ pairs = qs.split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP, @params_limit + 1)
228
+
229
+ if pairs.size > @params_limit
230
+ param_count = pairs.size + pairs.last.count(separator || "&")
231
+ raise QueryLimitError, "total number of query parameters (#{param_count}) exceeds limit (#{@params_limit})"
232
+ end
229
233
 
230
- qs
234
+ if unescaper
235
+ pairs.each do |p|
236
+ next if p.empty?
237
+ k, v = p.split('=', 2).map!(&unescaper)
238
+ yield k, v
239
+ end
231
240
  else
232
- ''
241
+ pairs.each do |p|
242
+ next if p.empty?
243
+ k, v = p.split('=', 2).map! { |s| unescape(s) }
244
+ yield k, v
245
+ end
233
246
  end
247
+ rescue ArgumentError => e
248
+ raise InvalidParameterError, e.message, e.backtrace
234
249
  end
235
250
 
236
251
  def unescape(string, encoding = Encoding::UTF_8)
data/lib/rack/request.rb CHANGED
@@ -61,9 +61,14 @@ module Rack
61
61
 
62
62
  def initialize(env)
63
63
  @env = env
64
+ @ip = nil
64
65
  @params = nil
65
66
  end
66
67
 
68
+ def ip
69
+ @ip ||= super
70
+ end
71
+
67
72
  def params
68
73
  @params ||= super
69
74
  end
@@ -398,8 +403,8 @@ module Rack
398
403
  return forwarded.last
399
404
  end
400
405
  when :x_forwarded
401
- if value = get_header(HTTP_X_FORWARDED_HOST)
402
- return wrap_ipv6(split_header(value).last)
406
+ if (value = get_header(HTTP_X_FORWARDED_HOST)) && (x_forwarded_host = split_header(value).last)
407
+ return wrap_ipv6(x_forwarded_host)
403
408
  end
404
409
  end
405
410
  end
@@ -413,10 +418,9 @@ module Rack
413
418
 
414
419
  def ip
415
420
  remote_addresses = split_header(get_header('REMOTE_ADDR'))
416
- external_addresses = reject_trusted_ip_addresses(remote_addresses)
417
421
 
418
- unless external_addresses.empty?
419
- return external_addresses.last
422
+ remote_addresses.reverse_each do |ip|
423
+ return ip unless trusted_proxy?(ip)
420
424
  end
421
425
 
422
426
  if (forwarded_for = self.forwarded_for) && !forwarded_for.empty?
@@ -424,7 +428,10 @@ module Rack
424
428
  # So we reject all the trusted addresses (proxy*) and return the
425
429
  # last client. Or if we trust everyone, we just return the first
426
430
  # address.
427
- return reject_trusted_ip_addresses(forwarded_for).last || forwarded_for.first
431
+ forwarded_for.reverse_each do |ip|
432
+ return ip unless trusted_proxy?(ip)
433
+ end
434
+ return forwarded_for.first
428
435
  end
429
436
 
430
437
  # If all the addresses are trusted, and we aren't forwarded, just return
@@ -482,70 +489,42 @@ module Rack
482
489
 
483
490
  # Returns the data received in the query string.
484
491
  def GET
485
- rr_query_string = get_header(RACK_REQUEST_QUERY_STRING)
486
- query_string = self.query_string
487
- if rr_query_string == query_string
488
- get_header(RACK_REQUEST_QUERY_HASH)
489
- else
490
- if rr_query_string
491
- warn "query string used for GET parsing different from current query string. Starting in Rack 3.2, Rack will used the cached GET value instead of parsing the current query string.", uplevel: 1
492
- end
493
- query_hash = parse_query(query_string, '&')
494
- set_header(RACK_REQUEST_QUERY_STRING, query_string)
495
- set_header(RACK_REQUEST_QUERY_HASH, query_hash)
496
- end
492
+ get_header(RACK_REQUEST_QUERY_HASH) || set_header(RACK_REQUEST_QUERY_HASH, parse_query(query_string, '&'))
497
493
  end
498
494
 
499
- # Returns the data received in the request body.
495
+ # Returns the form data pairs received in the request body.
500
496
  #
501
497
  # This method support both application/x-www-form-urlencoded and
502
498
  # multipart/form-data.
503
- def POST
504
- if error = get_header(RACK_REQUEST_FORM_ERROR)
499
+ def form_pairs
500
+ if pairs = get_header(RACK_REQUEST_FORM_PAIRS)
501
+ return pairs
502
+ elsif error = get_header(RACK_REQUEST_FORM_ERROR)
505
503
  raise error.class, error.message, cause: error.cause
506
504
  end
507
505
 
508
506
  begin
509
507
  rack_input = get_header(RACK_INPUT)
510
508
 
511
- # If the form hash was already memoized:
512
- if form_hash = get_header(RACK_REQUEST_FORM_HASH)
513
- form_input = get_header(RACK_REQUEST_FORM_INPUT)
514
- # And it was memoized from the same input:
515
- if form_input.equal?(rack_input)
516
- return form_hash
517
- elsif form_input
518
- warn "input stream used for POST parsing different from current input stream. Starting in Rack 3.2, Rack will used the cached POST value instead of parsing the current input stream.", uplevel: 1
519
- end
520
- end
521
-
522
509
  # Otherwise, figure out how to parse the input:
523
510
  if rack_input.nil?
524
- set_header RACK_REQUEST_FORM_INPUT, nil
525
- set_header(RACK_REQUEST_FORM_HASH, {})
511
+ set_header(RACK_REQUEST_FORM_PAIRS, [])
526
512
  elsif form_data? || parseable_data?
527
513
  if pairs = Rack::Multipart.parse_multipart(env, Rack::Multipart::ParamList)
528
514
  set_header RACK_REQUEST_FORM_PAIRS, pairs
529
- set_header RACK_REQUEST_FORM_HASH, expand_param_pairs(pairs)
530
515
  else
531
- # Add 2 bytes. One to check whether it is over the limit, and a second
532
- # in case the slice! call below removes the last byte
533
- # If read returns nil, use the empty string
534
- form_vars = get_header(RACK_INPUT).read(query_parser.bytesize_limit + 2) || ''
516
+ form_vars = get_header(RACK_INPUT).read
535
517
 
536
518
  # Fix for Safari Ajax postings that always append \0
537
519
  # form_vars.sub!(/\0\z/, '') # performance replacement:
538
520
  form_vars.slice!(-1) if form_vars.end_with?("\0")
539
521
 
540
522
  set_header RACK_REQUEST_FORM_VARS, form_vars
541
- set_header RACK_REQUEST_FORM_HASH, parse_query(form_vars, '&')
523
+ pairs = query_parser.parse_query_pairs(form_vars, '&')
524
+ set_header(RACK_REQUEST_FORM_PAIRS, pairs)
542
525
  end
543
-
544
- set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)
545
- get_header RACK_REQUEST_FORM_HASH
546
526
  else
547
- set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)
548
- set_header(RACK_REQUEST_FORM_HASH, {})
527
+ set_header(RACK_REQUEST_FORM_PAIRS, [])
549
528
  end
550
529
  rescue => error
551
530
  set_header(RACK_REQUEST_FORM_ERROR, error)
@@ -553,6 +532,21 @@ module Rack
553
532
  end
554
533
  end
555
534
 
535
+ # Returns the data received in the request body.
536
+ #
537
+ # This method support both application/x-www-form-urlencoded and
538
+ # multipart/form-data.
539
+ def POST
540
+ if form_hash = get_header(RACK_REQUEST_FORM_HASH)
541
+ return form_hash
542
+ elsif error = get_header(RACK_REQUEST_FORM_ERROR)
543
+ raise error.class, error.message, cause: error.cause
544
+ end
545
+
546
+ pairs = form_pairs
547
+ set_header RACK_REQUEST_FORM_HASH, expand_param_pairs(pairs)
548
+ end
549
+
556
550
  # The union of GET and POST data.
557
551
  #
558
552
  # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
@@ -560,6 +554,10 @@ module Rack
560
554
  self.GET.merge(self.POST)
561
555
  end
562
556
 
557
+ # Allow overriding the query parser that the receiver will use.
558
+ # By default Rack::Utils.default_query_parser is used.
559
+ attr_writer :query_parser
560
+
563
561
  # Destructively update a parameter, whether it's in GET and/or POST. Returns nil.
564
562
  #
565
563
  # The parameter is updated wherever it was previous defined, so GET, POST, or both. If it wasn't previously defined, it's inserted into GET.
@@ -619,13 +617,6 @@ module Rack
619
617
  Rack::Request.ip_filter.call(ip)
620
618
  end
621
619
 
622
- # like Hash#values_at
623
- def values_at(*keys)
624
- warn("Request#values_at is deprecated and will be removed in a future version of Rack. Please use request.params.values_at instead", uplevel: 1)
625
-
626
- keys.map { |key| params[key] }
627
- end
628
-
629
620
  private
630
621
 
631
622
  def default_session; {}; end
@@ -673,7 +664,7 @@ module Rack
673
664
  end
674
665
 
675
666
  def query_parser
676
- Utils.default_query_parser
667
+ @query_parser || Utils.default_query_parser
677
668
  end
678
669
 
679
670
  def parse_query(qs, d = '&')
@@ -681,6 +672,7 @@ module Rack
681
672
  end
682
673
 
683
674
  def parse_multipart
675
+ warn "Rack::Request#parse_multipart is deprecated and will be removed in a future version of Rack.", uplevel: 1
684
676
  Rack::Multipart.extract_multipart(self, query_parser)
685
677
  end
686
678
 
@@ -695,7 +687,7 @@ module Rack
695
687
  end
696
688
 
697
689
  def split_header(value)
698
- value ? value.strip.split(/[,\s]+/) : []
690
+ value ? value.strip.split(/[, \t]+/) : []
699
691
  end
700
692
 
701
693
  # ipv6 extracted from resolv stdlib, simplified
@@ -743,10 +735,6 @@ module Rack
743
735
  return match[:host], match[:address], match[:port]&.to_i
744
736
  end
745
737
 
746
- def reject_trusted_ip_addresses(ip_addresses)
747
- ip_addresses.reject { |ip| trusted_proxy?(ip) }
748
- end
749
-
750
738
  FORWARDED_SCHEME_HEADERS = {
751
739
  proto: HTTP_X_FORWARDED_PROTO,
752
740
  scheme: HTTP_X_FORWARDED_SCHEME
@@ -21,7 +21,10 @@ module Rack
21
21
  end
22
22
 
23
23
  def call(env)
24
- env[RACK_INPUT] = RewindableInput.new(env[RACK_INPUT])
24
+ if (input = env[RACK_INPUT])
25
+ env[RACK_INPUT] = RewindableInput.new(input)
26
+ end
27
+
25
28
  @app.call(env)
26
29
  end
27
30
  end