Permalink
Browse files

Accept header mime parser can now deal with empty fields

  • Loading branch information...
1 parent f122ca5 commit f48e89931f3dfe0571f62f926c01cb7783814347 @tobi tobi committed May 1, 2008
Showing with 13 additions and 3 deletions.
  1. +6 −3 actionpack/lib/action_controller/mime_type.rb
  2. +7 −0 actionpack/test/controller/mime_type_test.rb
@@ -71,8 +71,11 @@ def parse(accept_header)
# keep track of creation order to keep the subsequent sort stable
list = []
accept_header.split(/,/).each_with_index do |header, index|
- params = header.split(/;\s*q=/)
- list << AcceptItem.new(index, *params) unless params.empty?
+ params, q = header.split(/;\s*q=/)
+ if params
+ params.strip!
+ list << AcceptItem.new(index, params, q) unless params.empty?
+ end
end
list.sort!
@@ -145,7 +148,7 @@ def ===(list)
end
def ==(mime_type)
- return false unless mime_type
+ return false if mime_type.blank?
(@synonyms + [ self ]).any? do |synonym|
synonym.to_s == mime_type.to_s || synonym.to_sym == mime_type.to_sym
end
@@ -28,6 +28,13 @@ def test_parse_crappy_broken_acceptlines
expect = [Mime::HTML, Mime::XML, "image/*", Mime::TEXT, Mime::ALL]
assert_equal expect, Mime::Type.parse(accept).collect { |c| c.to_s }
end
+
+ # Accept header send with user HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1)
+ def test_parse_crappy_broken_acceptlines2
+ accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, , pronto/1.00.00, sslvpn/1.00.00.00, */*"
+ expect = ['image/gif', 'image/x-xbitmap', 'image/jpeg','image/pjpeg', 'application/x-shockwave-flash', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/msword', 'pronto/1.00.00', 'sslvpn/1.00.00.00', 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)

0 comments on commit f48e899

Please sign in to comment.