mirror of https://github.com/rails/rails
Fix `Mime::Type.parse` for HTTP Accept with parameters
Fixes MIME parsing raising errors on valid parameters #51594. Mime type lookups were updated to handle custom registered types as part of #48397. This fix the strips out custom media range parameters before falling back to the default type creation.
This commit is contained in:
parent
d462fb54b4
commit
5889b8659e
|
@ -1,3 +1,7 @@
|
|||
* Fix `Mime::Type.parse` handling type parameters for HTTP Accept headers.
|
||||
|
||||
*Taylor Chaparro*
|
||||
|
||||
* Fix the error page that is displayed when a view template is missing to account for nested controller paths in the
|
||||
suggested correct location for the missing template.
|
||||
|
||||
|
|
|
@ -165,8 +165,11 @@ module Mime
|
|||
end
|
||||
|
||||
def lookup(string)
|
||||
return LOOKUP[string] if LOOKUP.key?(string)
|
||||
|
||||
# fallback to the media-type without parameters if it was not found
|
||||
LOOKUP[string] || LOOKUP[string.split(";", 2)[0]&.rstrip] || Type.new(string)
|
||||
string = string.split(";", 2)[0]&.rstrip
|
||||
LOOKUP[string] || Type.new(string)
|
||||
end
|
||||
|
||||
def lookup_by_extension(extension)
|
||||
|
|
|
@ -68,6 +68,12 @@ class MimeTypeTest < ActiveSupport::TestCase
|
|||
assert_equal expect.map(&:to_s), Mime::Type.parse(accept).map(&:to_s)
|
||||
end
|
||||
|
||||
test "parse with q and media type parameters" do
|
||||
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"
|
||||
expect = [Mime[:html], Mime[:xml], Mime[:png], Mime[:pdf], Mime[:text], Mime[:yaml], "*/*"]
|
||||
assert_equal expect.map(&:to_s), Mime::Type.parse(accept).map(&:to_s)
|
||||
end
|
||||
|
||||
test "parse single media range with q" do
|
||||
accept = "text/html;q=0.9"
|
||||
expect = [Mime[:html]]
|
||||
|
@ -92,6 +98,12 @@ class MimeTypeTest < ActiveSupport::TestCase
|
|||
assert_equal expect, Mime::Type.parse(accept)
|
||||
end
|
||||
|
||||
test "parse wildcard with arbitrary media type parameters" do
|
||||
accept = '*/*; boundary="simple"'
|
||||
expect = ["*/*"]
|
||||
assert_equal expect, Mime::Type.parse(accept)
|
||||
end
|
||||
|
||||
# Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP)
|
||||
test "parse broken acceptlines" do
|
||||
accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5"
|
||||
|
|
Loading…
Reference in New Issue