diff options
author | José Valim <jose.valim@gmail.com> | 2009-07-28 21:29:29 +0200 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-07-29 12:06:02 -0700 |
commit | 3e8ba616efdd453dcfdd8fba78ed35fc3c1885de (patch) | |
tree | 9cf3530db032879cadb93cd9ed2f51fff6afab27 /actionpack/lib/action_dispatch/http/request.rb | |
parent | 3f445b316d34a49a8b6f27bde72979828baefaa8 (diff) | |
download | rails-3e8ba616efdd453dcfdd8fba78ed35fc3c1885de.tar.gz rails-3e8ba616efdd453dcfdd8fba78ed35fc3c1885de.tar.bz2 rails-3e8ba616efdd453dcfdd8fba78ed35fc3c1885de.zip |
Refactor even more Responder. Move mime negotiation to request and added respond_to class method.
Signed-off-by: Yehuda Katz <wycats@gmail.com>
Diffstat (limited to 'actionpack/lib/action_dispatch/http/request.rb')
-rwxr-xr-x | actionpack/lib/action_dispatch/http/request.rb | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index 3f23a5af7a..5f9463eb91 100755 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -161,7 +161,7 @@ module ActionDispatch # GET /posts/5.xml | request.format => Mime::XML # GET /posts/5.xhtml | request.format => Mime::HTML # GET /posts/5 | request.format => Mime::HTML or MIME::JS, or request.accepts.first depending on the value of <tt>ActionController::Base.use_accept_header</tt> - + # def format(view_path = []) @env["action_dispatch.request.format"] ||= if parameters[:format] @@ -173,13 +173,11 @@ module ActionDispatch end end + # Expand raw_formats by converting Mime::ALL to the Mime::SET. + # def formats if ActionController::Base.use_accept_header - if param = parameters[:format] - Array.wrap(Mime[param]) - else - accepts.dup - end.tap do |ret| + raw_formats.tap do |ret| if ret == ONLY_ALL ret.replace Mime::SET elsif all = ret.index(Mime::ALL) @@ -187,7 +185,7 @@ module ActionDispatch end end else - [format] + Mime::SET + raw_formats + Mime::SET end end @@ -232,7 +230,7 @@ module ActionDispatch def xml_http_request? !(@env['HTTP_X_REQUESTED_WITH'] !~ /XMLHttpRequest/i) end - alias xhr? :xml_http_request? + alias :xhr? :xml_http_request? # Which IP addresses are "trusted proxies" that can be stripped from # the right-hand-side of X-Forwarded-For @@ -485,7 +483,35 @@ EOM session['flash'] || {} end + # Receives an array of mimes and return the first user sent mime that + # matches the order array. + # + def negotiate_mime(order) + raw_formats.each do |priority| + if priority == Mime::ALL + return order.first + elsif order.include?(priority) + return priority + end + end + + order.include?(Mime::ALL) ? formats.first : nil + end + private + + def raw_formats + if ActionController::Base.use_accept_header + if param = parameters[:format] + Array.wrap(Mime[param]) + else + accepts.dup + end + else + [format] + end + end + def named_host?(host) !(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) end |