diff options
-rw-r--r-- | actionpack/lib/action_controller/mime_type.rb | 10 | ||||
-rw-r--r-- | actionpack/test/controller/mime_type_test.rb | 7 |
2 files changed, 13 insertions, 4 deletions
diff --git a/actionpack/lib/action_controller/mime_type.rb b/actionpack/lib/action_controller/mime_type.rb index b179fe2f89..44a60487b2 100644 --- a/actionpack/lib/action_controller/mime_type.rb +++ b/actionpack/lib/action_controller/mime_type.rb @@ -63,10 +63,12 @@ module Mime def parse(accept_header) # keep track of creation order to keep the subsequent sort stable - index = 0 - list = accept_header.split(/,/).map! do |i| - AcceptItem.new(index += 1, *i.split(/;\s*q=/)) - end.sort! + list = [] + accept_header.split(/,/).each_with_index do |header, index| + params = header.split(/;\s*q=/) + list << AcceptItem.new(index, *params) unless params.empty? + end + list.sort! # Take care of the broken text/xml entry by renaming or deleting it text_xml = list.index("text/xml") diff --git a/actionpack/test/controller/mime_type_test.rb b/actionpack/test/controller/mime_type_test.rb index 0425b034c5..1c944697d4 100644 --- a/actionpack/test/controller/mime_type_test.rb +++ b/actionpack/test/controller/mime_type_test.rb @@ -22,6 +22,13 @@ class MimeTypeTest < Test::Unit::TestCase assert_equal expect, Mime::Type.parse(accept) end + # Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP) + def test_parse_crappy_broken_acceptlines + accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5" + expect = [Mime::HTML, Mime::XML, "image/*", Mime::TEXT, Mime::ALL] + assert_equal expect, Mime::Type.parse(accept).collect { |c| c.to_s } + end + def test_custom_type Mime::Type.register("image/gif", :gif) assert_nothing_raised do |