Permalink
Browse files

Merge pull request #4918 from scottwb/fix-single-accept-with-q

Fix MIME::Type.parse handling of single media with a q value
  • Loading branch information...
2 parents ed9aeec + 3b203f7 commit dd9b428cbcb6453e01f25a35479a5ab5e19c03fc @josevalim josevalim committed Feb 7, 2012
Showing with 9 additions and 1 deletion.
  1. +3 −1 actionpack/lib/action_dispatch/http/mime_type.rb
  2. +6 −0 actionpack/test/dispatch/mime_type_test.rb
@@ -82,6 +82,7 @@ def ==(item)
class << self
TRAILING_STAR_REGEXP = /(text|application)\/\*/
+ Q_SEPARATOR_REGEXP = /;\s*q=/
def lookup(string)
LOOKUP[string]
@@ -108,6 +109,7 @@ def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], s
def parse(accept_header)
if accept_header !~ /,/
+ accept_header = accept_header.split(Q_SEPARATOR_REGEXP).first
if accept_header =~ TRAILING_STAR_REGEXP
parse_data_with_trailing_star($1)
else
@@ -117,7 +119,7 @@ def parse(accept_header)
# keep track of creation order to keep the subsequent sort stable
list, index = [], 0
accept_header.split(/,/).each do |header|
- params, q = header.split(/;\s*q=/)
+ params, q = header.split(Q_SEPARATOR_REGEXP)
if params.present?
params.strip!
@@ -69,6 +69,12 @@ class MimeTypeTest < ActiveSupport::TestCase
assert_equal expect, Mime::Type.parse(accept)
end
+ test "parse single media range with q" do
+ accept = "text/html;q=0.9"
+ expect = [Mime::HTML]
+ 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"

1 comment on commit dd9b428

Contributor

gtd commented on dd9b428 Feb 12, 2012

This applies cleanly to 3-2-stable I think, it should be applied there, no?

Please sign in to comment.