Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6484

Show
Ignore:
Timestamp:
03/28/07 16:42:21 (1 year ago)
Author:
xal
Message:

Improved the handling of broken accept headers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/mime_type.rb

    r6350 r6484  
    6464      def parse(accept_header) 
    6565        # keep track of creation order to keep the subsequent sort stable 
    66         index = 0 
    67         list = accept_header.split(/,/).map! do |i|  
    68           AcceptItem.new(index += 1, *i.split(/;\s*q=/)) 
    69         end.sort! 
     66        list = [] 
     67        accept_header.split(/,/).each_with_index do |header, index|  
     68          params = header.split(/;\s*q=/) 
     69          list << AcceptItem.new(index, *params) unless params.empty? 
     70        end 
     71        list.sort! 
    7072 
    7173        # Take care of the broken text/xml entry by renaming or deleting it 
  • trunk/actionpack/test/controller/mime_type_test.rb

    r6153 r6484  
    2121    expect = [Mime::HTML, Mime::XML, Mime::PNG, Mime::PDF, Mime::TEXT, Mime::YAML, Mime::ALL] 
    2222    assert_equal expect, Mime::Type.parse(accept) 
     23  end 
     24   
     25  # Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP) 
     26  def test_parse_crappy_broken_acceptlines 
     27    accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5" 
     28    expect = [Mime::HTML, Mime::XML, "image/*", Mime::TEXT, Mime::ALL] 
     29    assert_equal expect, Mime::Type.parse(accept).collect { |c| c.to_s } 
    2330  end 
    2431