From 2caf4d5a9337591c2728c7db826526febc5b2158 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 17 Sep 2006 16:20:32 +0000 Subject: Added proper getters and setters for content type and charset [DHH] Added utf-8 as the default charset for all renders. You can change this default using ActionController::Base.default_charset=(encoding) [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5129 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/base.rb | 16 +++++++++++++--- actionpack/lib/action_controller/caching.rb | 2 +- actionpack/lib/action_controller/rescue.rb | 2 +- actionpack/lib/action_controller/response.rb | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) (limited to 'actionpack/lib/action_controller') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index c971989582..e9d415085a 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -272,6 +272,10 @@ module ActionController #:nodoc: @@param_parsers = { Mime::XML => :xml_simple } cattr_accessor :param_parsers + # Controls the default charset for all renders. + @@default_charset = "utf-8" + cattr_accessor :default_charset + # Template root determines the base from which template references will be made. So a call to render("test/template") # will be converted to "#{template_root}/test/template.rhtml". class_inheritable_accessor :template_root @@ -420,6 +424,7 @@ module ActionController #:nodoc: log_processing send(method, *arguments) + assign_default_content_type_and_charset response ensure process_cleanup @@ -703,7 +708,7 @@ module ActionController #:nodoc: end if content_type = options[:content_type] - headers["Content-Type"] = content_type.to_s + response.content_type = content_type.to_s end if text = options[:text] @@ -793,12 +798,12 @@ module ActionController #:nodoc: end def render_javascript(javascript, status = nil) #:nodoc: - response.headers['Content-Type'] = 'text/javascript; charset=UTF-8' + response.content_type = Mime::JS render_text(javascript, status) end def render_xml(xml, status = nil) #:nodoc: - response.headers['Content-Type'] = 'application/xml' + response.content_type = Mime::XML render_text(xml, status) end @@ -1034,6 +1039,11 @@ module ActionController #:nodoc: @action_name = (params['action'] || 'index') end + def assign_default_content_type_and_charset + response.content_type ||= Mime::HTML + response.charset ||= self.class.default_charset + end + def action_methods self.class.action_methods end diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb index cc3d9daaa3..491d7d7d0c 100644 --- a/actionpack/lib/action_controller/caching.rb +++ b/actionpack/lib/action_controller/caching.rb @@ -210,7 +210,7 @@ module ActionController #:nodoc: def set_content_type!(action_cache_path) if extention = action_cache_path.extension content_type = Mime::EXTENSION_LOOKUP[extention] - action_cache_path.controller.headers['Content-Type'] = content_type.to_s + action_cache_path.controller.content_type = content_type.to_s end end diff --git a/actionpack/lib/action_controller/rescue.rb b/actionpack/lib/action_controller/rescue.rb index f9b831f18b..b7df9fd125 100644 --- a/actionpack/lib/action_controller/rescue.rb +++ b/actionpack/lib/action_controller/rescue.rb @@ -71,7 +71,7 @@ module ActionController #:nodoc: @template.instance_variable_set("@contents", @template.render_file(template_path_for_local_rescue(exception), false)) - @headers["Content-Type"] = "text/html" + response.content_type = Mime::HTML render_file(rescues_path("layout"), response_code_for_rescue(exception)) end diff --git a/actionpack/lib/action_controller/response.rb b/actionpack/lib/action_controller/response.rb index 2bec477647..d6795e37a0 100755 --- a/actionpack/lib/action_controller/response.rb +++ b/actionpack/lib/action_controller/response.rb @@ -7,6 +7,24 @@ module ActionController @body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], [] end + def content_type=(mime_type) + @headers["Content-Type"] = charset ? "#{mime_type}; charset=#{charset}" : mime_type + end + + def content_type + content_type = String(@headers["Content-Type"]).split(";")[0] + content_type.blank? ? nil : content_type + end + + def charset=(encoding) + @headers["Content-Type"] = "#{content_type || "text/html"}; charset=#{encoding}" + end + + def charset + charset = String(@headers["Content-Type"]).split(";")[1] + charset.blank? ? nil : charset.strip.split("=")[1] + end + def redirect(to_url, permanently = false) @headers["Status"] = "302 Found" unless @headers["Status"] == "301 Moved Permanently" @headers["location"] = to_url -- cgit v1.2.3