From a4cb744ef1ce15a18a9a60608d2e01a30d2d000b Mon Sep 17 00:00:00 2001 From: Joel Chippindale Date: Mon, 18 Oct 2010 12:04:54 +0100 Subject: [PATCH] Fix Mime::Type#parse for single media range with accepts params [#5833 state:resolved] --- actionpack/lib/action_controller/mime_type.rb | 8 ++++---- actionpack/test/controller/mime_type_test.rb | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/actionpack/lib/action_controller/mime_type.rb b/actionpack/lib/action_controller/mime_type.rb index 017626b..c5fda94 100644 --- a/actionpack/lib/action_controller/mime_type.rb +++ b/actionpack/lib/action_controller/mime_type.rb @@ -87,14 +87,14 @@ module Mime def parse(accept_header) if accept_header !~ /,/ - [Mime::Type.lookup(accept_header)] + [Mime::Type.lookup(accept_header.split(';').first)] else # keep track of creation order to keep the subsequent sort stable list = [] - accept_header.split(/,/).each_with_index do |header, index| - params, q = header.split(/;\s*q=/) + accept_header.split(/,/).each_with_index do |header, index| + params, q = header.split(/;\s*q=/) if params - params.strip! + params.strip! list << AcceptItem.new(index, params, q) unless params.empty? end end diff --git a/actionpack/test/controller/mime_type_test.rb b/actionpack/test/controller/mime_type_test.rb index c7faa62..aa1a4a5 100644 --- a/actionpack/test/controller/mime_type_test.rb +++ b/actionpack/test/controller/mime_type_test.rb @@ -19,9 +19,15 @@ class MimeTypeTest < Test::Unit::TestCase def test_parse_with_q 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,*/*; q=0.2" expect = [Mime::HTML, Mime::XML, Mime::PNG, Mime::PDF, Mime::TEXT, Mime::YAML, Mime::ALL] + end + + # Accept header (which is valid even if it is pointless) send with user HTTP_USER_AGENT: searchdnabot/Nutch-1.0 + def test_parse_single_with_q + accept = "*/*;q=0.1" + expect = [Mime::ALL] 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" -- 1.7.3