提交 53a115ae 编写于 作者: R Ryuta Kamizono

Merge pull request #37017 from kamipo/fix_content_type

Fix `content_type=` to not discard extra part
上级 da98bd8f
......@@ -82,7 +82,6 @@ def each(&block)
SET_COOKIE = "Set-Cookie"
LOCATION = "Location"
NO_CONTENT_CODES = [100, 101, 102, 204, 205, 304]
CONTENT_TYPE_PARSER = /\A(?<type>[^;\s]+)?\s*(?:;\s*(?<extra>.+))?(?:;\s*charset=(?<quote>"?)(?<charset>[^;\s]+)\k<quote>)/ # :nodoc:
cattr_accessor :default_charset, default: "utf-8"
cattr_accessor :default_headers
......@@ -420,14 +419,20 @@ def cookies
end
private
ContentTypeHeader = Struct.new :mime_type, :extra, :charset
NullContentTypeHeader = ContentTypeHeader.new nil, nil, nil
ContentTypeHeader = Struct.new :mime_type, :charset
NullContentTypeHeader = ContentTypeHeader.new nil, nil
CONTENT_TYPE_PARSER = /
\A
(?<mime_type>[^;\s]+\s*(?:;\s*(?:(?!charset)[^;\s])+)*)?
(?:;\s*charset=(?<quote>"?)(?<charset>[^;\s]+)\k<quote>)?
/x # :nodoc:
def parse_content_type(content_type)
if content_type && match = CONTENT_TYPE_PARSER.match(content_type)
ContentTypeHeader.new(match[:type], match[:extra], match[:charset])
ContentTypeHeader.new(match[:mime_type], match[:charset])
else
ContentTypeHeader.new(content_type, nil)
NullContentTypeHeader
end
end
......
......@@ -50,6 +50,11 @@ def render_default_content_types_for_respond_to
format.rss { render body: "hello world!", content_type: Mime[:xml] }
end
end
def render_content_type_with_charset
response.content_type = "text/html; fragment; charset=utf-16"
render body: "hello world!"
end
end
class ContentTypeTest < ActionController::TestCase
......@@ -131,6 +136,12 @@ def test_change_for_builder
assert_equal "utf-8", @response.charset
end
def test_content_type_with_charset
get :render_content_type_with_charset
assert_equal "text/html; fragment", @response.media_type
assert_equal "utf-16", @response.charset
end
private
def with_default_charset(charset)
......
......@@ -572,7 +572,7 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest
assert_equal("text/csv; header=present; charset=utf-16", @response.headers["Content-Type"])
assert_equal("text/csv; header=present; charset=utf-16", @response.content_type)
assert_equal("text/csv", @response.media_type)
assert_equal("text/csv; header=present", @response.media_type)
assert_equal("utf-16", @response.charset)
end
......@@ -590,7 +590,7 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest
assert_equal('text/csv; header=present; charset="utf-16"', @response.headers["Content-Type"])
assert_equal('text/csv; header=present; charset="utf-16"', @response.content_type)
assert_equal("text/csv", @response.media_type)
assert_equal("text/csv; header=present", @response.media_type)
assert_equal("utf-16", @response.charset)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册