Skip to content

Commit 2bb6782

Browse files
authored
Merge pull request rails#51616 from notchairmk/notchairmk/accept-params
Fix `Mime::Type.parse` for HTTP Accept with parameters
2 parents c10eb93 + 5889b86 commit 2bb6782

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

actionpack/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix `Mime::Type.parse` handling type parameters for HTTP Accept headers.
2+
3+
*Taylor Chaparro*
4+
15
* Fix the error page that is displayed when a view template is missing to account for nested controller paths in the
26
suggested correct location for the missing template.
37

actionpack/lib/action_dispatch/http/mime_type.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,11 @@ def register_callback(&block)
165165
end
166166

167167
def lookup(string)
168+
return LOOKUP[string] if LOOKUP.key?(string)
169+
168170
# fallback to the media-type without parameters if it was not found
169-
LOOKUP[string] || LOOKUP[string.split(";", 2)[0]&.rstrip] || Type.new(string)
171+
string = string.split(";", 2)[0]&.rstrip
172+
LOOKUP[string] || Type.new(string)
170173
end
171174

172175
def lookup_by_extension(extension)

actionpack/test/dispatch/mime_type_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class MimeTypeTest < ActiveSupport::TestCase
6868
assert_equal expect.map(&:to_s), Mime::Type.parse(accept).map(&:to_s)
6969
end
7070

71+
test "parse with q and media type parameters" do
72+
accept = "text/xml,application/xhtml+xml,text/yaml; q=0.3,application/xml,text/html; q=0.8,image/png,text/plain; q=0.5,application/pdf,*/*; encoding=UTF-8; q=0.2"
73+
expect = [Mime[:html], Mime[:xml], Mime[:png], Mime[:pdf], Mime[:text], Mime[:yaml], "*/*"]
74+
assert_equal expect.map(&:to_s), Mime::Type.parse(accept).map(&:to_s)
75+
end
76+
7177
test "parse single media range with q" do
7278
accept = "text/html;q=0.9"
7379
expect = [Mime[:html]]
@@ -92,6 +98,12 @@ class MimeTypeTest < ActiveSupport::TestCase
9298
assert_equal expect, Mime::Type.parse(accept)
9399
end
94100

101+
test "parse wildcard with arbitrary media type parameters" do
102+
accept = '*/*; boundary="simple"'
103+
expect = ["*/*"]
104+
assert_equal expect, Mime::Type.parse(accept)
105+
end
106+
95107
# Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP)
96108
test "parse broken acceptlines" do
97109
accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5"

0 commit comments

Comments
 (0)