From 1310231c15742bf7d99e2f143d88b383c32782d3 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 14 Aug 2009 22:32:40 -0700 Subject: Got tests to pass with some more changes. * request.formats is much simpler now * For XHRs or Accept headers with a single item, we use the Accept header * For other requests, we use params[:format] or fallback to HTML * This is primarily to work around the fact that browsers provide completely broken Accept headers, so we have to whitelist the few cases we can specifically isolate and treat other requests as coming from the browser * For APIs, we can support single-item Accept headers, which disambiguates from the browsers * Requests to an action that only has an XML template from the browser will no longer find the template. This worked previously because most browsers provide a catch-all */*, but this was mostly accidental behavior. If you want to serve XML, either use the :xml format in links, or explicitly specify the XML template: render "template.xml". --- actionpack/lib/action_dispatch/http/request.rb | 36 +++++++++----------------- 1 file changed, 12 insertions(+), 24 deletions(-) (limited to 'actionpack/lib/action_dispatch/http') diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index b23306af62..bff030f0e4 100755 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -106,16 +106,10 @@ module ActionDispatch @env["action_dispatch.request.accepts"] ||= begin header = @env['HTTP_ACCEPT'].to_s.strip - fallback = xhr? ? Mime::JS : Mime::HTML - if header.empty? - [content_type, fallback, Mime::ALL].compact + [content_type] else - ret = Mime::Type.parse(header) - if ret.last == Mime::ALL - ret.insert(-2, fallback) - end - ret + Mime::Type.parse(header) end end end @@ -163,26 +157,20 @@ module ActionDispatch # GET /posts/5 | request.format => Mime::HTML or MIME::JS, or request.accepts.first depending on the value of ActionController::Base.use_accept_header # def format(view_path = []) - @env["action_dispatch.request.format"] ||= - if parameters[:format] - Mime[parameters[:format]] - elsif ActionController::Base.use_accept_header && !(accepts == ONLY_ALL) - accepts.first - elsif xhr? then Mime::JS - else Mime::HTML - end + formats.first end def formats - if ActionController::Base.use_accept_header - if param = parameters[:format] - Array.wrap(Mime[param]) + accept = @env['HTTP_ACCEPT'] + + @env["action_dispatch.request.formats"] ||= + if parameters[:format] + [Mime[parameters[:format]]] + elsif xhr? || (accept && !accept.include?(?,)) + accepts else - accepts.dup + [Mime::HTML] end - else - [format] - end end # Sets the \format by string extension, which can be used to force custom formats @@ -198,7 +186,7 @@ module ActionDispatch # end def format=(extension) parameters[:format] = extension.to_s - @env["action_dispatch.request.format"] = Mime::Type.lookup_by_extension(parameters[:format]) + @env["action_dispatch.request.formats"] = [Mime::Type.lookup_by_extension(parameters[:format])] end # Returns a symbolized version of the :format parameter of the request. -- cgit v1.2.3