Improved the handling of broken accept headers

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6484 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Tobias Lütke 2007-03-28 16:42:21 +00:00
parent 0228975a4e
commit df3ee41126
2 changed files with 13 additions and 4 deletions

@ -63,10 +63,12 @@ def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [])
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")

@ -22,6 +22,13 @@ def test_parse_with_q
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