From 75c7025098750477d0447e1b434590c770b9e0bd Mon Sep 17 00:00:00 2001 From: Manfred Stienstra Date: Mon, 21 Sep 2009 14:24:50 +0200 Subject: [PATCH] Prefix Internet Explorer's accepted mime types with sensible defaults. --- actionpack/lib/action_controller/request.rb | 15 ++++++++-- actionpack/test/controller/request_test.rb | 37 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb index 1c3c1c8..a5fe383 100755 --- a/actionpack/lib/action_controller/request.rb +++ b/actionpack/lib/action_controller/request.rb @@ -99,7 +99,15 @@ module ActionController content_type.to_s end - # Returns the accepted MIME type for the request. + # Regular expression to match the Internet Explorer user agent string. + INTERNET_EXPLORER = /MSIE\s[\d\.]+;/ + + # Returns true if the user agent is Internet Explorer. + def internet_explorer? + user_agent =~ INTERNET_EXPLORER + end + + # Returns the accepted MIME types for the request. def accepts @accepts ||= begin header = @env['HTTP_ACCEPT'].to_s.strip @@ -107,7 +115,9 @@ module ActionController if header.empty? [content_type, Mime::ALL].compact else - Mime::Type.parse(header) + accepts = Mime::Type.parse(header) + accepts.unshift(Mime::HTML, Mime::XML) if internet_explorer? + accepts end end end @@ -164,7 +174,6 @@ module ActionController end end - # Sets the \format by string extension, which can be used to force custom formats # that are not controlled by the extension. # diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb index c4cc63e..b41224a 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -371,6 +371,43 @@ class RequestTest < ActiveSupport::TestCase assert_equal Mime::XML, request.content_type end + def test_internet_explorer_user_agent_check + assert([ + "curl 7.16.1 (i386-portbld-freebsd6.2) libcurl/7.16.1 OpenSSL/0.9.7m zlib/1.2.3", + "Opera/9.80 (Macintosh; Intel Mac OS X; U; en) Presto/2.2.15 Version/10.00", + "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_1; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9", + "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en; rv:1.8.1.23) Gecko/20090815 Camino/1.6.9 (like Firefox/2.0.0.23)", + "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5" + ].all? do |user_agent| + !stub_request('HTTP_USER_AGENT' => user_agent).internet_explorer? + end) + + assert([ + "Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)", + "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; )", + "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Win64; x64; Trident/4.0)" + ].all? do |user_agent| + stub_request('HTTP_USER_AGENT' => user_agent).internet_explorer? + end) + end + + def test_accepts_without_user_agent + request = stub_request 'HTTP_ACCEPT' => 'text/html, text/csv, */*' + assert_equal [Mime::HTML, Mime::CSV, Mime::ALL], request.accepts + end + + def test_accepts_for_regular_user_agent + request = stub_request 'HTTP_USER_AGENT' => "curl 7.16.1 (i386-portbld-freebsd6.2) libcurl/7.16.1 OpenSSL/0.9.7m zlib/1.2.3", 'HTTP_ACCEPT' => 'text/html, text/csv, */*' + assert_equal [Mime::HTML, Mime::CSV, Mime::ALL], request.accepts + end + + def test_accepts_for_internet_explorer + request = stub_request 'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Win64; x64)', + 'HTTP_ACCEPT' => "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/x-shockwave-flash, application/x-silverlight, */*" + assert_equal 16, request.accepts.length + assert_equal [Mime::HTML, Mime::XML], request.accepts[0..1] + end + def test_user_agent request = stub_request 'HTTP_USER_AGENT' => 'TestAgent' assert_equal 'TestAgent', request.user_agent -- 1.6.4.2