rack 2.2.18 → 3.2.3

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.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +561 -75
  3. data/CONTRIBUTING.md +63 -55
  4. data/MIT-LICENSE +1 -1
  5. data/README.md +384 -0
  6. data/SPEC.rdoc +243 -277
  7. data/lib/rack/auth/abstract/handler.rb +3 -1
  8. data/lib/rack/auth/abstract/request.rb +5 -1
  9. data/lib/rack/auth/basic.rb +1 -3
  10. data/lib/rack/bad_request.rb +8 -0
  11. data/lib/rack/body_proxy.rb +21 -3
  12. data/lib/rack/builder.rb +108 -69
  13. data/lib/rack/cascade.rb +2 -3
  14. data/lib/rack/common_logger.rb +22 -17
  15. data/lib/rack/conditional_get.rb +20 -16
  16. data/lib/rack/constants.rb +68 -0
  17. data/lib/rack/content_length.rb +12 -16
  18. data/lib/rack/content_type.rb +8 -5
  19. data/lib/rack/deflater.rb +40 -26
  20. data/lib/rack/directory.rb +9 -3
  21. data/lib/rack/etag.rb +17 -23
  22. data/lib/rack/events.rb +25 -6
  23. data/lib/rack/files.rb +15 -17
  24. data/lib/rack/head.rb +8 -8
  25. data/lib/rack/headers.rb +238 -0
  26. data/lib/rack/lint.rb +817 -648
  27. data/lib/rack/lock.rb +2 -5
  28. data/lib/rack/media_type.rb +6 -7
  29. data/lib/rack/method_override.rb +5 -1
  30. data/lib/rack/mime.rb +14 -5
  31. data/lib/rack/mock.rb +1 -300
  32. data/lib/rack/mock_request.rb +161 -0
  33. data/lib/rack/mock_response.rb +147 -0
  34. data/lib/rack/multipart/generator.rb +7 -5
  35. data/lib/rack/multipart/parser.rb +291 -95
  36. data/lib/rack/multipart/uploaded_file.rb +45 -4
  37. data/lib/rack/multipart.rb +53 -40
  38. data/lib/rack/null_logger.rb +9 -0
  39. data/lib/rack/query_parser.rb +118 -121
  40. data/lib/rack/recursive.rb +2 -0
  41. data/lib/rack/reloader.rb +0 -2
  42. data/lib/rack/request.rb +272 -141
  43. data/lib/rack/response.rb +151 -66
  44. data/lib/rack/rewindable_input.rb +27 -5
  45. data/lib/rack/runtime.rb +7 -6
  46. data/lib/rack/sendfile.rb +68 -33
  47. data/lib/rack/show_exceptions.rb +25 -6
  48. data/lib/rack/show_status.rb +17 -9
  49. data/lib/rack/static.rb +8 -8
  50. data/lib/rack/tempfile_reaper.rb +15 -4
  51. data/lib/rack/urlmap.rb +3 -1
  52. data/lib/rack/utils.rb +228 -238
  53. data/lib/rack/version.rb +3 -15
  54. data/lib/rack.rb +13 -90
  55. metadata +14 -40
  56. data/README.rdoc +0 -347
  57. data/Rakefile +0 -130
  58. data/bin/rackup +0 -5
  59. data/contrib/rack.png +0 -0
  60. data/contrib/rack.svg +0 -150
  61. data/contrib/rack_logo.svg +0 -164
  62. data/contrib/rdoc.css +0 -412
  63. data/example/lobster.ru +0 -6
  64. data/example/protectedlobster.rb +0 -16
  65. data/example/protectedlobster.ru +0 -10
  66. data/lib/rack/auth/digest/md5.rb +0 -131
  67. data/lib/rack/auth/digest/nonce.rb +0 -53
  68. data/lib/rack/auth/digest/params.rb +0 -54
  69. data/lib/rack/auth/digest/request.rb +0 -43
  70. data/lib/rack/chunked.rb +0 -117
  71. data/lib/rack/core_ext/regexp.rb +0 -14
  72. data/lib/rack/file.rb +0 -7
  73. data/lib/rack/handler/cgi.rb +0 -59
  74. data/lib/rack/handler/fastcgi.rb +0 -100
  75. data/lib/rack/handler/lsws.rb +0 -61
  76. data/lib/rack/handler/scgi.rb +0 -71
  77. data/lib/rack/handler/thin.rb +0 -34
  78. data/lib/rack/handler/webrick.rb +0 -129
  79. data/lib/rack/handler.rb +0 -104
  80. data/lib/rack/lobster.rb +0 -70
  81. data/lib/rack/logger.rb +0 -20
  82. data/lib/rack/server.rb +0 -466
  83. data/lib/rack/session/abstract/id.rb +0 -523
  84. data/lib/rack/session/cookie.rb +0 -203
  85. data/lib/rack/session/memcache.rb +0 -10
  86. data/lib/rack/session/pool.rb +0 -90
  87. data/rack.gemspec +0 -46
data/lib/rack/request.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'constants'
4
+ require_relative 'utils'
5
+ require_relative 'media_type'
6
+
3
7
  module Rack
4
8
  # Rack::Request provides a convenient interface to a Rack
5
9
  # environment. It is stateless, the environment +env+ passed to the
@@ -10,22 +14,59 @@ module Rack
10
14
  # req.params["data"]
11
15
 
12
16
  class Request
13
- (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4'
14
-
15
17
  class << self
16
18
  attr_accessor :ip_filter
17
- end
18
19
 
19
- self.ip_filter = lambda { |ip| /\A127\.0\.0\.1\Z|\A(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|\A::1\Z|\Afd[0-9a-f]{2}:.+|\Alocalhost\Z|\Aunix\Z|\Aunix:/i.match?(ip) }
20
- ALLOWED_SCHEMES = %w(https http).freeze
21
- SCHEME_WHITELIST = ALLOWED_SCHEMES
22
- if Object.respond_to?(:deprecate_constant)
23
- deprecate_constant :SCHEME_WHITELIST
20
+ # The priority when checking forwarded headers. The default
21
+ # is <tt>[:forwarded, :x_forwarded]</tt>, which means, check the
22
+ # +Forwarded+ header first, followed by the appropriate
23
+ # <tt>X-Forwarded-*</tt> header. You can revert the priority by
24
+ # reversing the priority, or remove checking of either
25
+ # or both headers by removing elements from the array.
26
+ #
27
+ # This should be set as appropriate in your environment
28
+ # based on what reverse proxies are in use. If you are not
29
+ # using reverse proxies, you should probably use an empty
30
+ # array.
31
+ attr_accessor :forwarded_priority
32
+
33
+ # The priority when checking either the <tt>X-Forwarded-Proto</tt>
34
+ # or <tt>X-Forwarded-Scheme</tt> header for the forwarded protocol.
35
+ # The default is <tt>[:proto, :scheme]</tt>, to try the
36
+ # <tt>X-Forwarded-Proto</tt> header before the
37
+ # <tt>X-Forwarded-Scheme</tt> header. Rack 2 had behavior
38
+ # similar to <tt>[:scheme, :proto]</tt>. You can remove either or
39
+ # both of the entries in array to ignore that respective header.
40
+ attr_accessor :x_forwarded_proto_priority
24
41
  end
25
42
 
43
+ @forwarded_priority = [:forwarded, :x_forwarded]
44
+ @x_forwarded_proto_priority = [:proto, :scheme]
45
+
46
+ valid_ipv4_octet = /\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])/
47
+
48
+ trusted_proxies = Regexp.union(
49
+ /\A127#{valid_ipv4_octet}{3}\z/, # localhost IPv4 range 127.x.x.x, per RFC-3330
50
+ /\A::1\z/, # localhost IPv6 ::1
51
+ /\Af[cd][0-9a-f]{2}(?::[0-9a-f]{0,4}){0,7}\z/i, # private IPv6 range fc00 .. fdff
52
+ /\A10#{valid_ipv4_octet}{3}\z/, # private IPv4 range 10.x.x.x
53
+ /\A172\.(1[6-9]|2[0-9]|3[01])#{valid_ipv4_octet}{2}\z/, # private IPv4 range 172.16.0.0 .. 172.31.255.255
54
+ /\A192\.168#{valid_ipv4_octet}{2}\z/, # private IPv4 range 192.168.x.x
55
+ /\Alocalhost\z|\Aunix(\z|:)/i, # localhost hostname, and unix domain sockets
56
+ )
57
+
58
+ self.ip_filter = lambda { |ip| trusted_proxies.match?(ip) }
59
+
60
+ ALLOWED_SCHEMES = %w(https http wss ws).freeze
61
+
26
62
  def initialize(env)
63
+ @env = env
64
+ @ip = nil
27
65
  @params = nil
28
- super(env)
66
+ end
67
+
68
+ def ip
69
+ @ip ||= super
29
70
  end
30
71
 
31
72
  def params
@@ -49,6 +90,8 @@ module Rack
49
90
 
50
91
  def initialize(env)
51
92
  @env = env
93
+ # This module is included at least in `ActionDispatch::Request`
94
+ # The call to `super()` allows additional mixed-in initializers are called
52
95
  super()
53
96
  end
54
97
 
@@ -135,6 +178,8 @@ module Rack
135
178
  # The contents of the host/:authority header sent to the proxy.
136
179
  HTTP_X_FORWARDED_HOST = 'HTTP_X_FORWARDED_HOST'
137
180
 
181
+ HTTP_FORWARDED = 'HTTP_FORWARDED'
182
+
138
183
  # The value of the scheme sent to the proxy.
139
184
  HTTP_X_FORWARDED_SCHEME = 'HTTP_X_FORWARDED_SCHEME'
140
185
 
@@ -144,7 +189,7 @@ module Rack
144
189
  # The port used to connect to the proxy.
145
190
  HTTP_X_FORWARDED_PORT = 'HTTP_X_FORWARDED_PORT'
146
191
 
147
- # Another way for specifing https scheme was used.
192
+ # Another way for specifying https scheme was used.
148
193
  HTTP_X_FORWARDED_SSL = 'HTTP_X_FORWARDED_SSL'
149
194
 
150
195
  def body; get_header(RACK_INPUT) end
@@ -159,7 +204,6 @@ module Rack
159
204
  def content_length; get_header('CONTENT_LENGTH') end
160
205
  def logger; get_header(RACK_LOGGER) end
161
206
  def user_agent; get_header('HTTP_USER_AGENT') end
162
- def multithread?; get_header(RACK_MULTITHREAD) end
163
207
 
164
208
  # the referer of the client
165
209
  def referer; get_header('HTTP_REFERER') end
@@ -248,9 +292,7 @@ module Rack
248
292
  end
249
293
 
250
294
  def server_port
251
- if port = get_header(SERVER_PORT)
252
- Integer(port)
253
- end
295
+ get_header(SERVER_PORT)
254
296
  end
255
297
 
256
298
  def cookies
@@ -307,44 +349,67 @@ module Rack
307
349
 
308
350
  def port
309
351
  if authority = self.authority
310
- _, _, port = split_authority(self.authority)
311
-
312
- if port
313
- return port
314
- end
315
- end
316
-
317
- if forwarded_port = self.forwarded_port
318
- return forwarded_port.first
319
- end
320
-
321
- if scheme = self.scheme
322
- if port = DEFAULT_PORTS[self.scheme]
323
- return port
324
- end
352
+ _, _, port = split_authority(authority)
325
353
  end
326
354
 
327
- self.server_port
355
+ port || forwarded_port&.last || DEFAULT_PORTS[scheme] || server_port
328
356
  end
329
357
 
330
358
  def forwarded_for
331
- if value = get_header(HTTP_X_FORWARDED_FOR)
332
- split_header(value).map do |authority|
333
- split_authority(wrap_ipv6(authority))[1]
359
+ forwarded_priority.each do |type|
360
+ case type
361
+ when :forwarded
362
+ if forwarded_for = get_http_forwarded(:for)
363
+ return(forwarded_for.map! do |authority|
364
+ split_authority(authority)[1]
365
+ end)
366
+ end
367
+ when :x_forwarded
368
+ if value = get_header(HTTP_X_FORWARDED_FOR)
369
+ return(split_header(value).map do |authority|
370
+ split_authority(wrap_ipv6(authority))[1]
371
+ end)
372
+ end
334
373
  end
335
374
  end
375
+
376
+ nil
336
377
  end
337
378
 
338
379
  def forwarded_port
339
- if value = get_header(HTTP_X_FORWARDED_PORT)
340
- split_header(value).map(&:to_i)
380
+ forwarded_priority.each do |type|
381
+ case type
382
+ when :forwarded
383
+ if forwarded = get_http_forwarded(:for)
384
+ return(forwarded.map do |authority|
385
+ split_authority(authority)[2]
386
+ end.compact)
387
+ end
388
+ when :x_forwarded
389
+ if value = get_header(HTTP_X_FORWARDED_PORT)
390
+ return split_header(value).map(&:to_i)
391
+ end
392
+ end
341
393
  end
394
+
395
+ nil
342
396
  end
343
397
 
344
398
  def forwarded_authority
345
- if value = get_header(HTTP_X_FORWARDED_HOST)
346
- wrap_ipv6(split_header(value).first)
399
+ forwarded_priority.each do |type|
400
+ case type
401
+ when :forwarded
402
+ if forwarded = get_http_forwarded(:host)
403
+ return forwarded.last
404
+ end
405
+ when :x_forwarded
406
+ if (value = get_header(HTTP_X_FORWARDED_HOST)) && (x_forwarded_host = split_header(value).last)
407
+ return wrap_ipv6(x_forwarded_host)
408
+ end
409
+ end
347
410
  end
411
+
412
+ nil
348
413
  end
349
414
 
350
415
  def ssl?
@@ -353,20 +418,20 @@ module Rack
353
418
 
354
419
  def ip
355
420
  remote_addresses = split_header(get_header('REMOTE_ADDR'))
356
- external_addresses = reject_trusted_ip_addresses(remote_addresses)
357
421
 
358
- unless external_addresses.empty?
359
- return external_addresses.first
422
+ remote_addresses.reverse_each do |ip|
423
+ return ip unless trusted_proxy?(ip)
360
424
  end
361
425
 
362
- if forwarded_for = self.forwarded_for
363
- unless forwarded_for.empty?
364
- # The forwarded for addresses are ordered: client, proxy1, proxy2.
365
- # So we reject all the trusted addresses (proxy*) and return the
366
- # last client. Or if we trust everyone, we just return the first
367
- # address.
368
- return reject_trusted_ip_addresses(forwarded_for).last || forwarded_for.first
426
+ if (forwarded_for = self.forwarded_for) && !forwarded_for.empty?
427
+ # The forwarded for addresses are ordered: client, proxy1, proxy2.
428
+ # So we reject all the trusted addresses (proxy*) and return the
429
+ # last client. Or if we trust everyone, we just return the first
430
+ # address.
431
+ forwarded_for.reverse_each do |ip|
432
+ return ip unless trusted_proxy?(ip)
369
433
  end
434
+ return forwarded_for.first
370
435
  end
371
436
 
372
437
  # If all the addresses are trusted, and we aren't forwarded, just return
@@ -402,13 +467,13 @@ module Rack
402
467
  end
403
468
 
404
469
  # Determine whether the request body contains form-data by checking
405
- # the request Content-Type for one of the media-types:
470
+ # the request content-type for one of the media-types:
406
471
  # "application/x-www-form-urlencoded" or "multipart/form-data". The
407
472
  # list of form-data media types can be modified through the
408
473
  # +FORM_DATA_MEDIA_TYPES+ array.
409
474
  #
410
475
  # A request body is also assumed to contain form-data when no
411
- # Content-Type header is provided and the request_method is POST.
476
+ # content-type header is provided and the request_method is POST.
412
477
  def form_data?
413
478
  type = media_type
414
479
  meth = get_header(RACK_METHODOVERRIDE_ORIGINAL_METHOD) || get_header(REQUEST_METHOD)
@@ -424,12 +489,49 @@ module Rack
424
489
 
425
490
  # Returns the data received in the query string.
426
491
  def GET
427
- if get_header(RACK_REQUEST_QUERY_STRING) == query_string
428
- get_header(RACK_REQUEST_QUERY_HASH)
429
- else
430
- query_hash = parse_query(query_string, '&;')
431
- set_header(RACK_REQUEST_QUERY_STRING, query_string)
432
- set_header(RACK_REQUEST_QUERY_HASH, query_hash)
492
+ get_header(RACK_REQUEST_QUERY_HASH) || set_header(RACK_REQUEST_QUERY_HASH, parse_query(query_string, '&'))
493
+ end
494
+
495
+ # Returns the form data pairs received in the request body.
496
+ #
497
+ # This method support both application/x-www-form-urlencoded and
498
+ # multipart/form-data.
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)
503
+ raise error.class, error.message, cause: error.cause
504
+ end
505
+
506
+ begin
507
+ rack_input = get_header(RACK_INPUT)
508
+
509
+ # Otherwise, figure out how to parse the input:
510
+ if rack_input.nil?
511
+ set_header(RACK_REQUEST_FORM_PAIRS, [])
512
+ elsif form_data? || parseable_data?
513
+ if pairs = Rack::Multipart.parse_multipart(env, Rack::Multipart::ParamList)
514
+ set_header RACK_REQUEST_FORM_PAIRS, pairs
515
+ else
516
+ # Add 2 bytes. One to check whether it is over the limit, and a second
517
+ # in case the slice! call below removes the last byte
518
+ # If read returns nil, use the empty string
519
+ form_vars = get_header(RACK_INPUT).read(query_parser.bytesize_limit + 2) || ''
520
+
521
+ # Fix for Safari Ajax postings that always append \0
522
+ # form_vars.sub!(/\0\z/, '') # performance replacement:
523
+ form_vars.slice!(-1) if form_vars.end_with?("\0")
524
+
525
+ set_header RACK_REQUEST_FORM_VARS, form_vars
526
+ pairs = query_parser.parse_query_pairs(form_vars, '&')
527
+ set_header(RACK_REQUEST_FORM_PAIRS, pairs)
528
+ end
529
+ else
530
+ set_header(RACK_REQUEST_FORM_PAIRS, [])
531
+ end
532
+ rescue => error
533
+ set_header(RACK_REQUEST_FORM_ERROR, error)
534
+ raise
433
535
  end
434
536
  end
435
537
 
@@ -438,28 +540,14 @@ module Rack
438
540
  # This method support both application/x-www-form-urlencoded and
439
541
  # multipart/form-data.
440
542
  def POST
441
- if get_header(RACK_INPUT).nil?
442
- raise "Missing rack.input"
443
- elsif get_header(RACK_REQUEST_FORM_INPUT) == get_header(RACK_INPUT)
444
- get_header(RACK_REQUEST_FORM_HASH)
445
- elsif form_data? || parseable_data?
446
- unless set_header(RACK_REQUEST_FORM_HASH, parse_multipart)
447
- form_vars = get_header(RACK_INPUT).read
448
-
449
- # Fix for Safari Ajax postings that always append \0
450
- # form_vars.sub!(/\0\z/, '') # performance replacement:
451
- form_vars.slice!(-1) if form_vars.end_with?("\0")
452
-
453
- set_header RACK_REQUEST_FORM_VARS, form_vars
454
- set_header RACK_REQUEST_FORM_HASH, parse_query(form_vars, '&')
455
-
456
- get_header(RACK_INPUT).rewind
457
- end
458
- set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)
459
- get_header RACK_REQUEST_FORM_HASH
460
- else
461
- {}
543
+ if form_hash = get_header(RACK_REQUEST_FORM_HASH)
544
+ return form_hash
545
+ elsif error = get_header(RACK_REQUEST_FORM_ERROR)
546
+ raise error.class, error.message, cause: error.cause
462
547
  end
548
+
549
+ pairs = form_pairs
550
+ set_header RACK_REQUEST_FORM_HASH, expand_param_pairs(pairs)
463
551
  end
464
552
 
465
553
  # The union of GET and POST data.
@@ -469,6 +557,10 @@ module Rack
469
557
  self.GET.merge(self.POST)
470
558
  end
471
559
 
560
+ # Allow overriding the query parser that the receiver will use.
561
+ # By default Rack::Utils.default_query_parser is used.
562
+ attr_writer :query_parser
563
+
472
564
  # Destructively update a parameter, whether it's in GET and/or POST. Returns nil.
473
565
  #
474
566
  # 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.
@@ -528,31 +620,6 @@ module Rack
528
620
  Rack::Request.ip_filter.call(ip)
529
621
  end
530
622
 
531
- # shortcut for <tt>request.params[key]</tt>
532
- def [](key)
533
- if $VERBOSE
534
- warn("Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead")
535
- end
536
-
537
- params[key.to_s]
538
- end
539
-
540
- # shortcut for <tt>request.params[key] = value</tt>
541
- #
542
- # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
543
- def []=(key, value)
544
- if $VERBOSE
545
- warn("Request#[]= is deprecated and will be removed in a future version of Rack. Please use request.params[]= instead")
546
- end
547
-
548
- params[key.to_s] = value
549
- end
550
-
551
- # like Hash#values_at
552
- def values_at(*keys)
553
- keys.map { |key| params[key] }
554
- end
555
-
556
623
  private
557
624
 
558
625
  def default_session; {}; end
@@ -572,18 +639,35 @@ module Rack
572
639
  end
573
640
 
574
641
  def parse_http_accept_header(header)
575
- header.to_s.split(",").each(&:strip!).map do |part|
576
- attribute, parameters = part.split(";", 2).each(&:strip!)
642
+ # It would be nice to use filter_map here, but it's Ruby 2.7+
643
+ parts = header.to_s.split(',')
644
+
645
+ parts.map! do |part|
646
+ part.strip!
647
+ next if part.empty?
648
+
649
+ attribute, parameters = part.split(';', 2)
650
+ attribute.strip!
651
+ parameters&.strip!
577
652
  quality = 1.0
578
653
  if parameters and /\Aq=([\d.]+)/ =~ parameters
579
654
  quality = $1.to_f
580
655
  end
581
656
  [attribute, quality]
582
657
  end
658
+
659
+ parts.compact!
660
+
661
+ parts
662
+ end
663
+
664
+ # Get an array of values set in the RFC 7239 `Forwarded` request header.
665
+ def get_http_forwarded(token)
666
+ Utils.forwarded_values(get_header(HTTP_FORWARDED))&.[](token)
583
667
  end
584
668
 
585
669
  def query_parser
586
- Utils.default_query_parser
670
+ @query_parser || Utils.default_query_parser
587
671
  end
588
672
 
589
673
  def parse_query(qs, d = '&')
@@ -591,65 +675,108 @@ module Rack
591
675
  end
592
676
 
593
677
  def parse_multipart
678
+ warn "Rack::Request#parse_multipart is deprecated and will be removed in a future version of Rack.", uplevel: 1
594
679
  Rack::Multipart.extract_multipart(self, query_parser)
595
680
  end
596
681
 
597
- def split_header(value)
598
- value ? value.strip.split(/[,\s]+/) : []
682
+ def expand_param_pairs(pairs, query_parser = query_parser())
683
+ params = query_parser.make_params
684
+
685
+ pairs.each do |k, v|
686
+ query_parser.normalize_params(params, k, v)
687
+ end
688
+
689
+ params.to_params_hash
599
690
  end
600
691
 
601
- AUTHORITY = /^
602
- # The host:
692
+ def split_header(value)
693
+ value ? value.strip.split(/[, \t]+/) : []
694
+ end
695
+
696
+ # ipv6 extracted from resolv stdlib, simplified
697
+ # to remove numbered match group creation.
698
+ ipv6 = Regexp.union(
699
+ /(?:[0-9A-Fa-f]{1,4}:){7}
700
+ [0-9A-Fa-f]{1,4}/x,
701
+ /(?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)? ::
702
+ (?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?/x,
703
+ /(?:[0-9A-Fa-f]{1,4}:){6,6}
704
+ \d+\.\d+\.\d+\.\d+/x,
705
+ /(?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)? ::
706
+ (?:[0-9A-Fa-f]{1,4}:)*
707
+ \d+\.\d+\.\d+\.\d+/x,
708
+ /[Ff][Ee]80
709
+ (?::[0-9A-Fa-f]{1,4}){7}
710
+ %[-0-9A-Za-z._~]+/x,
711
+ /[Ff][Ee]80:
712
+ (?:
713
+ (?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)? ::
714
+ (?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?
715
+ |
716
+ :(?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?
717
+ )?
718
+ :[0-9A-Fa-f]{1,4}%[-0-9A-Za-z._~]+/x)
719
+
720
+ AUTHORITY = /
721
+ \A
603
722
  (?<host>
604
- # An IPv6 address:
605
- (\[(?<ip6>.*)\])
723
+ # Match IPv6 as a string of hex digits and colons in square brackets
724
+ \[(?<address>#{ipv6})\]
606
725
  |
607
- # An IPv4 address:
608
- (?<ip4>[\d\.]+)
609
- |
610
- # A hostname:
611
- (?<name>[a-zA-Z0-9\.\-_]+)
726
+ # Match any other printable string (except square brackets) as a hostname
727
+ (?<address>[[[:graph:]&&[^\[\]]]]*?)
612
728
  )
613
- # The optional port:
614
729
  (:(?<port>\d+))?
615
- $/x
730
+ \z
731
+ /x
616
732
 
617
733
  private_constant :AUTHORITY
618
734
 
619
735
  def split_authority(authority)
620
- if match = AUTHORITY.match(authority)
621
- if address = match[:ip6]
622
- return match[:host], address, match[:port]&.to_i
623
- else
624
- return match[:host], match[:host], match[:port]&.to_i
625
- end
626
- end
627
-
628
- # Give up!
629
- return authority, authority, nil
630
- end
631
-
632
- def reject_trusted_ip_addresses(ip_addresses)
633
- ip_addresses.reject { |ip| trusted_proxy?(ip) }
736
+ return [] if authority.nil?
737
+ return [] unless match = AUTHORITY.match(authority)
738
+ return match[:host], match[:address], match[:port]&.to_i
634
739
  end
635
740
 
741
+ FORWARDED_SCHEME_HEADERS = {
742
+ proto: HTTP_X_FORWARDED_PROTO,
743
+ scheme: HTTP_X_FORWARDED_SCHEME
744
+ }.freeze
745
+ private_constant :FORWARDED_SCHEME_HEADERS
636
746
  def forwarded_scheme
637
- allowed_scheme(get_header(HTTP_X_FORWARDED_SCHEME)) ||
638
- allowed_scheme(extract_proto_header(get_header(HTTP_X_FORWARDED_PROTO)))
747
+ forwarded_priority.each do |type|
748
+ case type
749
+ when :forwarded
750
+ if (forwarded_proto = get_http_forwarded(:proto)) &&
751
+ (scheme = allowed_scheme(forwarded_proto.last))
752
+ return scheme
753
+ end
754
+ when :x_forwarded
755
+ x_forwarded_proto_priority.each do |x_type|
756
+ if header = FORWARDED_SCHEME_HEADERS[x_type]
757
+ split_header(get_header(header)).reverse_each do |scheme|
758
+ if allowed_scheme(scheme)
759
+ return scheme
760
+ end
761
+ end
762
+ end
763
+ end
764
+ end
765
+ end
766
+
767
+ nil
639
768
  end
640
769
 
641
770
  def allowed_scheme(header)
642
771
  header if ALLOWED_SCHEMES.include?(header)
643
772
  end
644
773
 
645
- def extract_proto_header(header)
646
- if header
647
- if (comma_index = header.index(','))
648
- header[0, comma_index]
649
- else
650
- header
651
- end
652
- end
774
+ def forwarded_priority
775
+ Request.forwarded_priority
776
+ end
777
+
778
+ def x_forwarded_proto_priority
779
+ Request.x_forwarded_proto_priority
653
780
  end
654
781
  end
655
782
 
@@ -657,3 +784,7 @@ module Rack
657
784
  include Helpers
658
785
  end
659
786
  end
787
+
788
+ # :nocov:
789
+ require_relative 'multipart' unless defined?(Rack::Multipart)
790
+ # :nocov: