Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only default the response charset when it is first set #27408

Merged
merged 1 commit into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions actionpack/lib/action_controller/metal/data_streaming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def send_file(path, options = {}) #:doc:
send_file_headers! options

self.status = options[:status] || 200
self.content_type = options[:type] if options.key?(:type)
self.content_type = options[:content_type] if options.key?(:content_type)
response.send_file path
end
Expand Down Expand Up @@ -113,6 +112,9 @@ def send_data(data, options = {}) #:doc:
def send_file_headers!(options)
type_provided = options.has_key?(:type)

self.content_type = DEFAULT_SEND_FILE_TYPE
response.sending_file = true

content_type = options.fetch(:type, DEFAULT_SEND_FILE_TYPE)
raise ArgumentError, ":type option required" if content_type.nil?

Expand All @@ -137,8 +139,6 @@ def send_file_headers!(options)

headers["Content-Transfer-Encoding"] = "binary"

response.sending_file = true

# Fix a problem with IE 6.0 on opening downloaded files:
# If Cache-Control: no-cache is set (which Rails does by default),
# IE removes the file it just downloaded from its cache immediately
Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_dispatch/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ def content_type=(content_type)
return unless content_type
new_header_info = parse_content_type(content_type.to_s)
prev_header_info = parsed_content_type_header
set_content_type new_header_info.mime_type, new_header_info.charset || prev_header_info.charset || self.class.default_charset
charset = new_header_info.charset || prev_header_info.charset
charset ||= self.class.default_charset unless prev_header_info.mime_type
set_content_type new_header_info.mime_type, charset
end

# Sets the HTTP response's content MIME type. For example, in the controller
Expand Down
9 changes: 8 additions & 1 deletion actionpack/test/controller/send_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,17 @@ def test_send_file_charset_with_type_options_key
assert_equal "text/calendar; charset=utf-8", response.headers["Content-Type"]
end

def test_send_file_charset_with_type_options_key_without_charset
@controller = SendFileWithActionControllerLive.new
@controller.options = { type: "image/png" }
response = process("file")
assert_equal "image/png", response.headers["Content-Type"]
end

def test_send_file_charset_with_content_type_options_key
@controller = SendFileWithActionControllerLive.new
@controller.options = { content_type: "text/calendar" }
response = process("file")
assert_equal "text/calendar; charset=utf-8", response.headers["Content-Type"]
assert_equal "text/calendar", response.headers["Content-Type"]
end
end