Move default content type and charset from Base to Response. Handle charset = nil.
This commit is contained in:
parent
aab2f0b353
commit
f8f077945f
|
@ -548,7 +548,6 @@ module ActionController #:nodoc:
|
||||||
@@guard.synchronize { send(method, *arguments) }
|
@@guard.synchronize { send(method, *arguments) }
|
||||||
end
|
end
|
||||||
|
|
||||||
assign_default_content_type_and_charset
|
|
||||||
response.prepare! unless component_request?
|
response.prepare! unless component_request?
|
||||||
response
|
response
|
||||||
ensure
|
ensure
|
||||||
|
@ -1219,15 +1218,6 @@ module ActionController #:nodoc:
|
||||||
@action_name = (params['action'] || 'index')
|
@action_name = (params['action'] || 'index')
|
||||||
end
|
end
|
||||||
|
|
||||||
def assign_default_content_type_and_charset
|
|
||||||
response.content_type ||= Mime::HTML
|
|
||||||
response.charset ||= self.class.default_charset unless sending_file?
|
|
||||||
end
|
|
||||||
|
|
||||||
def sending_file?
|
|
||||||
response.headers["Content-Transfer-Encoding"] == "binary"
|
|
||||||
end
|
|
||||||
|
|
||||||
def action_methods
|
def action_methods
|
||||||
self.class.action_methods
|
self.class.action_methods
|
||||||
end
|
end
|
||||||
|
|
|
@ -40,6 +40,8 @@ module ActionController # :nodoc:
|
||||||
attr_accessor :session, :cookies, :assigns, :template, :layout
|
attr_accessor :session, :cookies, :assigns, :template, :layout
|
||||||
attr_accessor :redirected_to, :redirected_to_method_params
|
attr_accessor :redirected_to, :redirected_to_method_params
|
||||||
|
|
||||||
|
delegate :default_charset, :to => 'ActionController::Base'
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], []
|
@body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], []
|
||||||
end
|
end
|
||||||
|
@ -60,19 +62,31 @@ module ActionController # :nodoc:
|
||||||
# the character set information will also be included in the content type
|
# the character set information will also be included in the content type
|
||||||
# information.
|
# information.
|
||||||
def content_type=(mime_type)
|
def content_type=(mime_type)
|
||||||
self.headers["Content-Type"] = charset ? "#{mime_type}; charset=#{charset}" : mime_type
|
self.headers["Content-Type"] =
|
||||||
|
if mime_type =~ /charset/ || (c = charset).nil?
|
||||||
|
mime_type.to_s
|
||||||
|
else
|
||||||
|
"#{mime_type}; charset=#{c}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the response's content MIME type, or nil if content type has been set.
|
# Returns the response's content MIME type, or nil if content type has been set.
|
||||||
def content_type
|
def content_type
|
||||||
content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0]
|
content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0]
|
||||||
content_type.blank? ? nil : content_type
|
content_type.blank? ? nil : content_type
|
||||||
end
|
end
|
||||||
|
|
||||||
def charset=(encoding)
|
# Set the charset of the Content-Type header. Set to nil to remove it.
|
||||||
self.headers["Content-Type"] = "#{content_type || Mime::HTML}; charset=#{encoding}"
|
# If no content type is set, it defaults to HTML.
|
||||||
|
def charset=(charset)
|
||||||
|
headers["Content-Type"] =
|
||||||
|
if charset
|
||||||
|
"#{content_type || Mime::HTML}; charset=#{charset}"
|
||||||
|
else
|
||||||
|
content_type || Mime::HTML.to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def charset
|
def charset
|
||||||
charset = String(headers["Content-Type"] || headers["type"]).split(";")[1]
|
charset = String(headers["Content-Type"] || headers["type"]).split(";")[1]
|
||||||
charset.blank? ? nil : charset.strip.split("=")[1]
|
charset.blank? ? nil : charset.strip.split("=")[1]
|
||||||
|
@ -104,7 +118,17 @@ module ActionController # :nodoc:
|
||||||
self.body = "<html><body>You are being <a href=\"#{url}\">redirected</a>.</body></html>"
|
self.body = "<html><body>You are being <a href=\"#{url}\">redirected</a>.</body></html>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sending_file?
|
||||||
|
headers["Content-Transfer-Encoding"] == "binary"
|
||||||
|
end
|
||||||
|
|
||||||
|
def assign_default_content_type_and_charset!
|
||||||
|
self.content_type ||= Mime::HTML
|
||||||
|
self.charset ||= default_charset unless sending_file?
|
||||||
|
end
|
||||||
|
|
||||||
def prepare!
|
def prepare!
|
||||||
|
assign_default_content_type_and_charset!
|
||||||
set_content_length!
|
set_content_length!
|
||||||
handle_conditional_get!
|
handle_conditional_get!
|
||||||
convert_content_type!
|
convert_content_type!
|
||||||
|
|
|
@ -19,6 +19,11 @@ class ContentTypeController < ActionController::Base
|
||||||
render :text => "hello world!"
|
render :text => "hello world!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_nil_charset_from_body
|
||||||
|
response.charset = nil
|
||||||
|
render :text => "hello world!"
|
||||||
|
end
|
||||||
|
|
||||||
def render_default_for_rhtml
|
def render_default_for_rhtml
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -85,8 +90,23 @@ class ContentTypeTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_charset_from_body
|
def test_charset_from_body
|
||||||
get :render_charset_from_body
|
get :render_charset_from_body
|
||||||
assert_equal "utf-16", @response.charset
|
|
||||||
assert_equal Mime::HTML, @response.content_type
|
assert_equal Mime::HTML, @response.content_type
|
||||||
|
assert_equal "utf-16", @response.charset
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_nil_charset_from_body
|
||||||
|
get :render_nil_charset_from_body
|
||||||
|
assert_equal Mime::HTML, @response.content_type
|
||||||
|
assert_equal "utf-8", @response.charset, @response.headers.inspect
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_nil_default_for_rhtml
|
||||||
|
ContentTypeController.default_charset = nil
|
||||||
|
get :render_default_for_rhtml
|
||||||
|
assert_equal Mime::HTML, @response.content_type
|
||||||
|
assert_nil @response.charset, @response.headers.inspect
|
||||||
|
ensure
|
||||||
|
ContentTypeController.default_charset = "utf-8"
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_default_for_rhtml
|
def test_default_for_rhtml
|
||||||
|
|
Loading…
Reference in New Issue