actionpack 7.2.3.1 → 8.1.4.1

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 (109) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +606 -120
  3. data/lib/abstract_controller/asset_paths.rb +4 -2
  4. data/lib/abstract_controller/base.rb +11 -5
  5. data/lib/abstract_controller/caching.rb +6 -3
  6. data/lib/abstract_controller/callbacks.rb +6 -0
  7. data/lib/abstract_controller/logger.rb +2 -1
  8. data/lib/abstract_controller/rendering.rb +0 -1
  9. data/lib/action_controller/api.rb +1 -0
  10. data/lib/action_controller/base.rb +3 -2
  11. data/lib/action_controller/caching.rb +1 -2
  12. data/lib/action_controller/form_builder.rb +4 -4
  13. data/lib/action_controller/log_subscriber.rb +22 -3
  14. data/lib/action_controller/metal/allow_browser.rb +12 -2
  15. data/lib/action_controller/metal/conditional_get.rb +30 -1
  16. data/lib/action_controller/metal/data_streaming.rb +5 -5
  17. data/lib/action_controller/metal/exceptions.rb +5 -0
  18. data/lib/action_controller/metal/flash.rb +1 -4
  19. data/lib/action_controller/metal/head.rb +3 -1
  20. data/lib/action_controller/metal/http_authentication.rb +1 -1
  21. data/lib/action_controller/metal/instrumentation.rb +1 -2
  22. data/lib/action_controller/metal/live.rb +83 -31
  23. data/lib/action_controller/metal/permissions_policy.rb +9 -0
  24. data/lib/action_controller/metal/rate_limiting.rb +39 -9
  25. data/lib/action_controller/metal/redirecting.rb +107 -14
  26. data/lib/action_controller/metal/renderers.rb +29 -9
  27. data/lib/action_controller/metal/rendering.rb +16 -3
  28. data/lib/action_controller/metal/request_forgery_protection.rb +18 -10
  29. data/lib/action_controller/metal/rescue.rb +9 -0
  30. data/lib/action_controller/metal/streaming.rb +5 -84
  31. data/lib/action_controller/metal/strong_parameters.rb +294 -98
  32. data/lib/action_controller/railtie.rb +35 -15
  33. data/lib/action_controller/structured_event_subscriber.rb +116 -0
  34. data/lib/action_controller/test_case.rb +12 -2
  35. data/lib/action_dispatch/http/cache.rb +143 -12
  36. data/lib/action_dispatch/http/content_security_policy.rb +14 -1
  37. data/lib/action_dispatch/http/filter_parameters.rb +5 -3
  38. data/lib/action_dispatch/http/mime_negotiation.rb +66 -2
  39. data/lib/action_dispatch/http/mime_types.rb +1 -0
  40. data/lib/action_dispatch/http/param_builder.rb +187 -0
  41. data/lib/action_dispatch/http/param_error.rb +26 -0
  42. data/lib/action_dispatch/http/parameters.rb +3 -3
  43. data/lib/action_dispatch/http/permissions_policy.rb +8 -1
  44. data/lib/action_dispatch/http/query_parser.rb +55 -0
  45. data/lib/action_dispatch/http/request.rb +70 -21
  46. data/lib/action_dispatch/http/response.rb +52 -17
  47. data/lib/action_dispatch/http/url.rb +112 -16
  48. data/lib/action_dispatch/journey/formatter.rb +5 -0
  49. data/lib/action_dispatch/journey/gtg/simulator.rb +33 -12
  50. data/lib/action_dispatch/journey/gtg/transition_table.rb +33 -41
  51. data/lib/action_dispatch/journey/nodes/node.rb +2 -1
  52. data/lib/action_dispatch/journey/parser.rb +99 -196
  53. data/lib/action_dispatch/journey/route.rb +45 -31
  54. data/lib/action_dispatch/journey/router/utils.rb +9 -15
  55. data/lib/action_dispatch/journey/router.rb +59 -81
  56. data/lib/action_dispatch/journey/routes.rb +7 -0
  57. data/lib/action_dispatch/journey/scanner.rb +44 -42
  58. data/lib/action_dispatch/journey/visitors.rb +55 -23
  59. data/lib/action_dispatch/journey/visualizer/fsm.js +4 -6
  60. data/lib/action_dispatch/log_subscriber.rb +7 -3
  61. data/lib/action_dispatch/middleware/cookies.rb +24 -5
  62. data/lib/action_dispatch/middleware/debug_exceptions.rb +24 -5
  63. data/lib/action_dispatch/middleware/debug_view.rb +11 -5
  64. data/lib/action_dispatch/middleware/exception_wrapper.rb +12 -12
  65. data/lib/action_dispatch/middleware/executor.rb +12 -2
  66. data/lib/action_dispatch/middleware/host_authorization.rb +3 -2
  67. data/lib/action_dispatch/middleware/public_exceptions.rb +1 -5
  68. data/lib/action_dispatch/middleware/remote_ip.rb +61 -24
  69. data/lib/action_dispatch/middleware/request_id.rb +2 -1
  70. data/lib/action_dispatch/middleware/session/abstract_store.rb +5 -0
  71. data/lib/action_dispatch/middleware/session/cache_store.rb +44 -4
  72. data/lib/action_dispatch/middleware/session/cookie_store.rb +0 -3
  73. data/lib/action_dispatch/middleware/ssl.rb +13 -3
  74. data/lib/action_dispatch/middleware/templates/rescues/_copy_button.html.erb +1 -0
  75. data/lib/action_dispatch/middleware/templates/rescues/_source.html.erb +3 -5
  76. data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +9 -5
  77. data/lib/action_dispatch/middleware/templates/rescues/blocked_host.html.erb +1 -0
  78. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +1 -0
  79. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +4 -0
  80. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +3 -0
  81. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +50 -0
  82. data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb +1 -0
  83. data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +1 -0
  84. data/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb +7 -8
  85. data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +1 -1
  86. data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +1 -0
  87. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +1 -0
  88. data/lib/action_dispatch/railtie.rb +21 -0
  89. data/lib/action_dispatch/request/session.rb +1 -0
  90. data/lib/action_dispatch/request/utils.rb +9 -3
  91. data/lib/action_dispatch/routing/inspector.rb +80 -57
  92. data/lib/action_dispatch/routing/mapper.rb +404 -223
  93. data/lib/action_dispatch/routing/polymorphic_routes.rb +2 -2
  94. data/lib/action_dispatch/routing/redirection.rb +10 -7
  95. data/lib/action_dispatch/routing/route_set.rb +21 -12
  96. data/lib/action_dispatch/routing/routes_proxy.rb +1 -0
  97. data/lib/action_dispatch/structured_event_subscriber.rb +20 -0
  98. data/lib/action_dispatch/system_test_case.rb +3 -3
  99. data/lib/action_dispatch/system_testing/browser.rb +15 -22
  100. data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +2 -2
  101. data/lib/action_dispatch/testing/assertions/response.rb +26 -2
  102. data/lib/action_dispatch/testing/assertions/routing.rb +27 -15
  103. data/lib/action_dispatch/testing/integration.rb +18 -7
  104. data/lib/action_dispatch/testing/test_helpers/page_dump_helper.rb +1 -1
  105. data/lib/action_dispatch.rb +14 -4
  106. data/lib/action_pack/gem_version.rb +3 -3
  107. metadata +18 -48
  108. data/lib/action_dispatch/journey/parser.y +0 -50
  109. data/lib/action_dispatch/journey/parser_extras.rb +0 -33
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionDispatch
4
+ class ParamBuilder
5
+ # --
6
+ # This implementation is based on Rack::QueryParser,
7
+ # Copyright (C) 2007-2021 Leah Neukirchen <http://leahneukirchen.org/infopage.html>
8
+
9
+ def self.make_default(param_depth_limit)
10
+ new param_depth_limit
11
+ end
12
+
13
+ attr_reader :param_depth_limit
14
+
15
+ def initialize(param_depth_limit)
16
+ @param_depth_limit = param_depth_limit
17
+ end
18
+
19
+ cattr_accessor :default
20
+ self.default = make_default(100)
21
+
22
+ class << self
23
+ delegate :from_query_string, :from_pairs, :from_hash, to: :default
24
+
25
+ def ignore_leading_brackets
26
+ ActionDispatch.deprecator.warn <<~MSG
27
+ ActionDispatch::ParamBuilder.ignore_leading_brackets is deprecated and have no effect and will be removed in Rails 8.2.
28
+ MSG
29
+
30
+ @ignore_leading_brackets
31
+ end
32
+
33
+ def ignore_leading_brackets=(value)
34
+ ActionDispatch.deprecator.warn <<~MSG
35
+ ActionDispatch::ParamBuilder.ignore_leading_brackets is deprecated and have no effect and will be removed in Rails 8.2.
36
+ MSG
37
+
38
+ @ignore_leading_brackets = value
39
+ end
40
+ end
41
+
42
+ def from_query_string(qs, separator: nil, encoding_template: nil)
43
+ from_pairs QueryParser.each_pair(qs, separator), encoding_template: encoding_template
44
+ end
45
+
46
+ def from_pairs(pairs, encoding_template: nil)
47
+ params = make_params
48
+
49
+ pairs.each do |k, v|
50
+ if Hash === v
51
+ v = ActionDispatch::Http::UploadedFile.new(v)
52
+ end
53
+
54
+ store_nested_param(params, k, v, 0, encoding_template)
55
+ end
56
+
57
+ params
58
+ rescue ArgumentError => e
59
+ raise InvalidParameterError, e.message, e.backtrace
60
+ end
61
+
62
+ def from_hash(hash, encoding_template: nil)
63
+ # Force encodings from encoding template
64
+ hash = Request::Utils::CustomParamEncoder.encode_for_template(hash, encoding_template)
65
+
66
+ # Assert valid encoding
67
+ Request::Utils.check_param_encoding(hash)
68
+
69
+ # Convert hashes to HWIA (or UploadedFile), and deep-munge nils
70
+ # out of arrays
71
+ hash = Request::Utils.normalize_encode_params(hash)
72
+
73
+ hash
74
+ end
75
+
76
+ private
77
+ def store_nested_param(params, name, v, depth, encoding_template = nil)
78
+ raise ParamsTooDeepError if depth >= param_depth_limit
79
+
80
+ if !name
81
+ # nil name, treat same as empty string (required by tests)
82
+ k = after = ""
83
+ elsif depth == 0
84
+ # Start of parsing, don't treat [] or [ at start of string specially
85
+ if start = name.index("[", 1)
86
+ # Start of parameter nesting, use part before brackets as key
87
+ k = name[0, start]
88
+ after = name[start, name.length]
89
+ else
90
+ # Plain parameter with no nesting
91
+ k = name
92
+ after = ""
93
+ end
94
+ elsif name.start_with?("[]")
95
+ # Array nesting
96
+ k = "[]"
97
+ after = name[2, name.length]
98
+ elsif name.start_with?("[") && (start = name.index("]", 1))
99
+ # Hash nesting, use the part inside brackets as the key
100
+ k = name[1, start - 1]
101
+ after = name[start + 1, name.length]
102
+ else
103
+ # Probably malformed input, nested but not starting with [
104
+ # treat full name as key for backwards compatibility.
105
+ k = name
106
+ after = ""
107
+ end
108
+
109
+ return if k.empty?
110
+
111
+ unless k.valid_encoding?
112
+ raise InvalidParameterError, "Invalid encoding for parameter: #{k}"
113
+ end
114
+
115
+ if depth == 0 && String === v
116
+ # We have to wait until we've found the top part of the name,
117
+ # because that's what the encoding template is configured with
118
+ if encoding_template && (designated_encoding = encoding_template[k]) && !v.frozen?
119
+ v.force_encoding(designated_encoding)
120
+ end
121
+
122
+ # ... and we can't validate the encoding until after we've
123
+ # applied any template override
124
+ unless v.valid_encoding?
125
+ raise InvalidParameterError, "Invalid encoding for parameter: #{v.scrub}"
126
+ end
127
+ end
128
+
129
+ if after == ""
130
+ if k == "[]" && depth != 0
131
+ return (v || !ActionDispatch::Request::Utils.perform_deep_munge) ? [v] : []
132
+ else
133
+ params[k] = v
134
+ end
135
+ elsif after == "["
136
+ params[name] = v
137
+ elsif after == "[]"
138
+ params[k] ||= []
139
+ raise ParameterTypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
140
+ params[k] << v if v || !ActionDispatch::Request::Utils.perform_deep_munge
141
+ elsif after.start_with?("[]")
142
+ # Recognize x[][y] (hash inside array) parameters
143
+ unless after[2] == "[" && after.end_with?("]") && (child_key = after[3, after.length - 4]) && !child_key.empty? && !child_key.index("[") && !child_key.index("]")
144
+ # Handle other nested array parameters
145
+ child_key = after[2, after.length]
146
+ end
147
+ params[k] ||= []
148
+ raise ParameterTypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
149
+ if params_hash_type?(params[k].last) && !params_hash_has_key?(params[k].last, child_key)
150
+ store_nested_param(params[k].last, child_key, v, depth + 1)
151
+ else
152
+ params[k] << store_nested_param(make_params, child_key, v, depth + 1)
153
+ end
154
+ else
155
+ params[k] ||= make_params
156
+ raise ParameterTypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params_hash_type?(params[k])
157
+ params[k] = store_nested_param(params[k], after, v, depth + 1)
158
+ end
159
+
160
+ params
161
+ end
162
+
163
+ def make_params
164
+ ActiveSupport::HashWithIndifferentAccess.new
165
+ end
166
+
167
+ def new_depth_limit(param_depth_limit)
168
+ self.class.new @params_class, param_depth_limit
169
+ end
170
+
171
+ def params_hash_type?(obj)
172
+ Hash === obj
173
+ end
174
+
175
+ def params_hash_has_key?(hash, key)
176
+ return false if key.include?("[]")
177
+
178
+ key.split(/[\[\]]+/).inject(hash) do |h, part|
179
+ next h if part == ""
180
+ return false unless params_hash_type?(h) && h.key?(part)
181
+ h[part]
182
+ end
183
+
184
+ true
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionDispatch
4
+ class ParamError < ActionDispatch::Http::Parameters::ParseError
5
+ def initialize(message = nil)
6
+ super
7
+ end
8
+
9
+ def self.===(other)
10
+ super || (
11
+ defined?(Rack::Utils::ParameterTypeError) && Rack::Utils::ParameterTypeError === other ||
12
+ defined?(Rack::Utils::InvalidParameterError) && Rack::Utils::InvalidParameterError === other ||
13
+ defined?(Rack::QueryParser::ParamsTooDeepError) && Rack::QueryParser::ParamsTooDeepError === other
14
+ )
15
+ end
16
+ end
17
+
18
+ class ParameterTypeError < ParamError
19
+ end
20
+
21
+ class InvalidParameterError < ParamError
22
+ end
23
+
24
+ class ParamsTooDeepError < ParamError
25
+ end
26
+ end
@@ -65,14 +65,14 @@ module ActionDispatch
65
65
  alias :params :parameters
66
66
 
67
67
  def path_parameters=(parameters) # :nodoc:
68
- delete_header("action_dispatch.request.parameters")
68
+ @env.delete("action_dispatch.request.parameters")
69
69
 
70
70
  parameters = Request::Utils.set_binary_encoding(self, parameters, parameters[:controller], parameters[:action])
71
71
  # If any of the path parameters has an invalid encoding then raise since it's
72
72
  # likely to trigger errors further on.
73
73
  Request::Utils.check_param_encoding(parameters)
74
74
 
75
- set_header PARAMETERS_KEY, parameters
75
+ @env[PARAMETERS_KEY] = parameters
76
76
  rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError => e
77
77
  raise ActionController::BadRequest.new("Invalid path parameters: #{e.message}")
78
78
  end
@@ -82,7 +82,7 @@ module ActionDispatch
82
82
  #
83
83
  # { action: "my_action", controller: "my_controller" }
84
84
  def path_parameters
85
- get_header(PARAMETERS_KEY) || set_header(PARAMETERS_KEY, {})
85
+ @env[PARAMETERS_KEY] ||= {}
86
86
  end
87
87
 
88
88
  private
@@ -86,12 +86,14 @@ module ActionDispatch # :nodoc:
86
86
  ambient_light_sensor: "ambient-light-sensor",
87
87
  autoplay: "autoplay",
88
88
  camera: "camera",
89
+ display_capture: "display-capture",
89
90
  encrypted_media: "encrypted-media",
90
91
  fullscreen: "fullscreen",
91
92
  geolocation: "geolocation",
92
93
  gyroscope: "gyroscope",
93
94
  hid: "hid",
94
95
  idle_detection: "idle-detection",
96
+ keyboard_map: "keyboard-map",
95
97
  magnetometer: "magnetometer",
96
98
  microphone: "microphone",
97
99
  midi: "midi",
@@ -177,11 +179,16 @@ module ActionDispatch # :nodoc:
177
179
  if context.nil?
178
180
  raise RuntimeError, "Missing context for the dynamic permissions policy source: #{source.inspect}"
179
181
  else
180
- context.instance_exec(&source)
182
+ resolved = context.instance_exec(&source)
183
+ apply_mappings(Array.wrap(resolved))
181
184
  end
182
185
  else
183
186
  raise RuntimeError, "Unexpected permissions policy source: #{source.inspect}"
184
187
  end
185
188
  end
186
189
  end
190
+
191
+ ActiveSupport.on_load(:action_dispatch_request) do
192
+ include ActionDispatch::PermissionsPolicy::Request
193
+ end
187
194
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "rack"
5
+
6
+ module ActionDispatch
7
+ class QueryParser
8
+ DEFAULT_SEP = /& */n
9
+ COMMON_SEP = { ";" => /; */n, ";," => /[;,] */n, "&" => /& */n, "&;" => /[&;] */n }
10
+
11
+ def self.strict_query_string_separator
12
+ ActionDispatch.deprecator.warn <<~MSG
13
+ The `strict_query_string_separator` configuration is deprecated have no effect and will be removed in Rails 8.2.
14
+ MSG
15
+ @strict_query_string_separator
16
+ end
17
+
18
+ def self.strict_query_string_separator=(value)
19
+ ActionDispatch.deprecator.warn <<~MSG
20
+ The `strict_query_string_separator` configuration is deprecated have no effect and will be removed in Rails 8.2.
21
+ MSG
22
+ @strict_query_string_separator = value
23
+ end
24
+
25
+ #--
26
+ # Note this departs from WHATWG's specified parsing algorithm by
27
+ # giving a nil value for keys that do not use '='. Callers that need
28
+ # the standard's interpretation can use `v.to_s`.
29
+ def self.each_pair(s, separator = nil)
30
+ return enum_for(:each_pair, s, separator) unless block_given?
31
+
32
+ s ||= ""
33
+
34
+ splitter =
35
+ if separator
36
+ COMMON_SEP[separator] || /[#{separator}] */n
37
+ else
38
+ DEFAULT_SEP
39
+ end
40
+
41
+ s.split(splitter).each do |part|
42
+ next if part.empty?
43
+
44
+ k, v = part.split("=", 2)
45
+
46
+ k = URI.decode_www_form_component(k)
47
+ v &&= URI.decode_www_form_component(v)
48
+
49
+ yield k, v
50
+ end
51
+
52
+ nil
53
+ end
54
+ end
55
+ end
@@ -25,7 +25,6 @@ module ActionDispatch
25
25
  include ActionDispatch::Http::FilterParameters
26
26
  include ActionDispatch::Http::URL
27
27
  include ActionDispatch::ContentSecurityPolicy::Request
28
- include ActionDispatch::PermissionsPolicy::Request
29
28
  include Rack::Request::Env
30
29
 
31
30
  autoload :Session, "action_dispatch/request/session"
@@ -55,12 +54,17 @@ module ActionDispatch
55
54
  METHOD
56
55
  end
57
56
 
57
+ TRANSFER_ENCODING = "HTTP_TRANSFER_ENCODING" # :nodoc:
58
+
58
59
  def self.empty
59
60
  new({})
60
61
  end
61
62
 
62
63
  def initialize(env)
63
64
  super
65
+
66
+ @rack_request = Rack::Request.new(env)
67
+
64
68
  @method = nil
65
69
  @request_method = nil
66
70
  @remote_ip = nil
@@ -69,6 +73,8 @@ module ActionDispatch
69
73
  @ip = nil
70
74
  end
71
75
 
76
+ attr_reader :rack_request
77
+
72
78
  def commit_cookie_jar! # :nodoc:
73
79
  end
74
80
 
@@ -132,7 +138,7 @@ module ActionDispatch
132
138
 
133
139
  # Populate the HTTP method lookup cache.
134
140
  HTTP_METHODS.each { |method|
135
- HTTP_METHOD_LOOKUP[method] = method.downcase.underscore.to_sym
141
+ HTTP_METHOD_LOOKUP[method] = method.downcase.tap { |m| m.tr!("-", "_") }.to_sym
136
142
  }
137
143
 
138
144
  alias raw_request_method request_method # :nodoc:
@@ -151,11 +157,17 @@ module ActionDispatch
151
157
  #
152
158
  # request.route_uri_pattern # => "/:controller(/:action(/:id))(.:format)"
153
159
  def route_uri_pattern
154
- get_header("action_dispatch.route_uri_pattern")
160
+ unless pattern = get_header("action_dispatch.route_uri_pattern")
161
+ route = get_header("action_dispatch.route")
162
+ return if route.nil?
163
+ pattern = route.path.spec.to_s
164
+ set_header("action_dispatch.route_uri_pattern", pattern)
165
+ end
166
+ pattern
155
167
  end
156
168
 
157
- def route_uri_pattern=(pattern) # :nodoc:
158
- set_header("action_dispatch.route_uri_pattern", pattern)
169
+ def route=(route) # :nodoc:
170
+ @env["action_dispatch.route"] = route
159
171
  end
160
172
 
161
173
  def routes # :nodoc:
@@ -283,7 +295,7 @@ module ActionDispatch
283
295
 
284
296
  # Returns the content length of the request as an integer.
285
297
  def content_length
286
- return raw_post.bytesize if headers.key?("Transfer-Encoding")
298
+ return raw_post.bytesize if has_header?(TRANSFER_ENCODING)
287
299
  super.to_i
288
300
  end
289
301
 
@@ -387,15 +399,12 @@ module ActionDispatch
387
399
  # Override Rack's GET method to support indifferent access.
388
400
  def GET
389
401
  fetch_header("action_dispatch.request.query_parameters") do |k|
390
- rack_query_params = super || {}
391
- controller = path_parameters[:controller]
392
- action = path_parameters[:action]
393
- rack_query_params = Request::Utils.set_binary_encoding(self, rack_query_params, controller, action)
394
- # Check for non UTF-8 parameter values, which would cause errors later
395
- Request::Utils.check_param_encoding(rack_query_params)
396
- set_header k, Request::Utils.normalize_encode_params(rack_query_params)
402
+ encoding_template = Request::Utils::CustomParamEncoder.action_encoding_template(self, path_parameters[:controller], path_parameters[:action])
403
+ rack_query_params = ActionDispatch::ParamBuilder.from_query_string(rack_request.query_string, encoding_template: encoding_template)
404
+
405
+ set_header k, rack_query_params
397
406
  end
398
- rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError, Rack::QueryParser::ParamsTooDeepError => e
407
+ rescue ActionDispatch::ParamError => e
399
408
  raise ActionController::BadRequest.new("Invalid query parameters: #{e.message}")
400
409
  end
401
410
  alias :query_parameters :GET
@@ -403,18 +412,54 @@ module ActionDispatch
403
412
  # Override Rack's POST method to support indifferent access.
404
413
  def POST
405
414
  fetch_header("action_dispatch.request.request_parameters") do
406
- pr = parse_formatted_parameters(params_parsers) do |params|
407
- super || {}
415
+ encoding_template = Request::Utils::CustomParamEncoder.action_encoding_template(self, path_parameters[:controller], path_parameters[:action])
416
+
417
+ param_list = nil
418
+ pr = parse_formatted_parameters(params_parsers) do
419
+ if param_list = request_parameters_list
420
+ ActionDispatch::ParamBuilder.from_pairs(param_list, encoding_template: encoding_template)
421
+ else
422
+ # We're not using a version of Rack that provides raw form
423
+ # pairs; we must use its hash (and thus post-process it below).
424
+ fallback_request_parameters
425
+ end
408
426
  end
409
- pr = Request::Utils.set_binary_encoding(self, pr, path_parameters[:controller], path_parameters[:action])
410
- Request::Utils.check_param_encoding(pr)
411
- self.request_parameters = Request::Utils.normalize_encode_params(pr)
427
+
428
+ # If the request body was parsed by a custom parser like JSON
429
+ # (and thus the above block was not run), we need to
430
+ # post-process the result hash.
431
+ if param_list.nil?
432
+ pr = ActionDispatch::ParamBuilder.from_hash(pr, encoding_template: encoding_template)
433
+ end
434
+
435
+ self.request_parameters = pr
412
436
  end
413
- rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError, Rack::QueryParser::ParamsTooDeepError, EOFError => e
437
+ rescue ActionDispatch::ParamError, EOFError => e
414
438
  raise ActionController::BadRequest.new("Invalid request parameters: #{e.message}")
415
439
  end
416
440
  alias :request_parameters :POST
417
441
 
442
+ def request_parameters_list
443
+ # We don't use Rack's parse result, but we must call it so Rack
444
+ # can populate the rack.request.* keys we need.
445
+ rack_post = rack_request.POST
446
+
447
+ if form_pairs = get_header("rack.request.form_pairs")
448
+ # Multipart
449
+ form_pairs
450
+ elsif form_vars = get_header("rack.request.form_vars")
451
+ # URL-encoded
452
+ ActionDispatch::QueryParser.each_pair(form_vars)
453
+ elsif rack_post && !rack_post.empty?
454
+ # It was multipart, but Rack did not preserve a pair list
455
+ # (probably too old). Flat parameter list is not available.
456
+ nil
457
+ else
458
+ # No request body, or not a format Rack knows
459
+ []
460
+ end
461
+ end
462
+
418
463
  # Returns the authorization header regardless of whether it was specified
419
464
  # directly or through one of the proxy alternatives.
420
465
  def authorization
@@ -469,7 +514,7 @@ module ActionDispatch
469
514
  def read_body_stream
470
515
  if body_stream
471
516
  reset_stream(body_stream) do
472
- if headers.key?("Transfer-Encoding")
517
+ if has_header?(TRANSFER_ENCODING)
473
518
  body_stream.read # Read body stream until EOF if "Transfer-Encoding" is present
474
519
  else
475
520
  body_stream.read(content_length)
@@ -491,6 +536,10 @@ module ActionDispatch
491
536
  yield
492
537
  end
493
538
  end
539
+
540
+ def fallback_request_parameters
541
+ rack_request.POST
542
+ end
494
543
  end
495
544
  end
496
545
 
@@ -119,10 +119,22 @@ module ActionDispatch # :nodoc:
119
119
  @str_body = nil
120
120
  end
121
121
 
122
+ BODY_METHODS = { to_ary: true }
123
+
124
+ def respond_to?(method, include_private = false)
125
+ if BODY_METHODS.key?(method)
126
+ @buf.respond_to?(method)
127
+ else
128
+ super
129
+ end
130
+ end
131
+
122
132
  def to_ary
123
- @buf.respond_to?(:to_ary) ?
124
- @buf.to_ary :
125
- @buf.each
133
+ if @str_body
134
+ [body]
135
+ else
136
+ @buf = @buf.to_ary
137
+ end
126
138
  end
127
139
 
128
140
  def body
@@ -138,7 +150,8 @@ module ActionDispatch # :nodoc:
138
150
 
139
151
  @str_body = nil
140
152
  @response.commit!
141
- @buf.push string
153
+ @buf.push string.frozen? ? string : string.dup
154
+ string.bytesize
142
155
  end
143
156
  alias_method :<<, :write
144
157
 
@@ -265,14 +278,27 @@ module ActionDispatch # :nodoc:
265
278
  # Sets the HTTP response's content MIME type. For example, in the controller you
266
279
  # could write this:
267
280
  #
268
- # response.content_type = "text/plain"
281
+ # response.content_type = "text/html"
282
+ #
283
+ # This method also accepts a symbol with the extension of the MIME type:
284
+ #
285
+ # response.content_type = :html
269
286
  #
270
287
  # If a character set has been defined for this response (see #charset=) then the
271
288
  # character set information will also be included in the content type
272
289
  # information.
273
290
  def content_type=(content_type)
274
- return unless content_type
275
- new_header_info = parse_content_type(content_type.to_s)
291
+ case content_type
292
+ when NilClass
293
+ return
294
+ when Symbol
295
+ mime_type = Mime[content_type]
296
+ raise ArgumentError, "Unknown MIME type #{content_type}" unless mime_type
297
+ new_header_info = ContentTypeHeader.new(mime_type.to_s)
298
+ else
299
+ new_header_info = parse_content_type(content_type.to_s)
300
+ end
301
+
276
302
  prev_header_info = parsed_content_type_header
277
303
  charset = new_header_info.charset || prev_header_info.charset
278
304
  charset ||= self.class.default_charset unless prev_header_info.mime_type
@@ -342,7 +368,13 @@ module ActionDispatch # :nodoc:
342
368
  # Returns the content of the response as a string. This contains the contents of
343
369
  # any calls to `render`.
344
370
  def body
345
- @stream.body
371
+ if @stream.respond_to?(:to_ary)
372
+ @stream.to_ary.join
373
+ elsif @stream.respond_to?(:body)
374
+ @stream.body
375
+ else
376
+ @stream
377
+ end
346
378
  end
347
379
 
348
380
  def write(string)
@@ -351,11 +383,16 @@ module ActionDispatch # :nodoc:
351
383
 
352
384
  # Allows you to manually set or override the response body.
353
385
  def body=(body)
354
- if body.respond_to?(:to_path)
355
- @stream = body
356
- else
357
- synchronize do
358
- @stream = build_buffer self, munge_body_object(body)
386
+ # Prevent ActionController::Metal::Live::Response from committing the response prematurely.
387
+ synchronize do
388
+ if body.respond_to?(:to_str)
389
+ @stream = build_buffer(self, [body])
390
+ elsif body.respond_to?(:to_path)
391
+ @stream = body
392
+ elsif body.respond_to?(:to_ary)
393
+ @stream = build_buffer(self, body)
394
+ else
395
+ @stream = body
359
396
  end
360
397
  end
361
398
  end
@@ -496,10 +533,6 @@ module ActionDispatch # :nodoc:
496
533
  Buffer.new response, body
497
534
  end
498
535
 
499
- def munge_body_object(body)
500
- body.respond_to?(:each) ? body : [body]
501
- end
502
-
503
536
  def assign_default_content_type_and_charset!
504
537
  return if media_type
505
538
 
@@ -513,6 +546,8 @@ module ActionDispatch # :nodoc:
513
546
  @response = response
514
547
  end
515
548
 
549
+ attr :response
550
+
516
551
  def close
517
552
  # Rack "close" maps to Response#abort, and **not** Response#close (which is used
518
553
  # when the controller's finished writing)