Skip to content

Commit 553271d

Browse files
committed
Merge pull request rails#27408 from matthewd/charset-fix
Only default the response charset when it is first set
1 parent 0ef292c commit 553271d

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

actionpack/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Restored correct `charset` behavior on `send_data` and `send_file`: while
2+
they should pass along any supplied value, they should not add a default.
3+
4+
Fixes #27344.
5+
6+
*Matthew Draper*
7+
8+
19
## Rails 5.0.1.rc2 (December 10, 2016) ##
210

311
* Move `cookies`, `flash`, and `session` methods back to

actionpack/lib/action_controller/metal/data_streaming.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ def send_file(path, options = {}) #:doc:
7070
send_file_headers! options
7171

7272
self.status = options[:status] || 200
73-
self.content_type = options[:type] if options.key?(:type)
7473
self.content_type = options[:content_type] if options.key?(:content_type)
7574
response.send_file path
7675
end
@@ -113,6 +112,9 @@ def send_data(data, options = {}) #:doc:
113112
def send_file_headers!(options)
114113
type_provided = options.has_key?(:type)
115114

115+
self.content_type = DEFAULT_SEND_FILE_TYPE
116+
response.sending_file = true
117+
116118
content_type = options.fetch(:type, DEFAULT_SEND_FILE_TYPE)
117119
raise ArgumentError, ":type option required" if content_type.nil?
118120

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

138140
headers['Content-Transfer-Encoding'] = 'binary'
139141

140-
response.sending_file = true
141-
142142
# Fix a problem with IE 6.0 on opening downloaded files:
143143
# If Cache-Control: no-cache is set (which Rails does by default),
144144
# IE removes the file it just downloaded from its cache immediately

actionpack/lib/action_dispatch/http/response.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ def content_type=(content_type)
227227
return unless content_type
228228
new_header_info = parse_content_type(content_type.to_s)
229229
prev_header_info = parsed_content_type_header
230-
set_content_type new_header_info.mime_type, new_header_info.charset || prev_header_info.charset || self.class.default_charset
230+
charset = new_header_info.charset || prev_header_info.charset
231+
charset ||= self.class.default_charset unless prev_header_info.mime_type
232+
set_content_type new_header_info.mime_type, charset
231233
end
232234

233235
# Sets the HTTP response's content MIME type. For example, in the controller

actionpack/test/controller/send_file_test.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,17 @@ def test_send_file_charset_with_type_options_key
242242
assert_equal "text/calendar; charset=utf-8", response.headers["Content-Type"]
243243
end
244244

245+
def test_send_file_charset_with_type_options_key_without_charset
246+
@controller = SendFileWithActionControllerLive.new
247+
@controller.options = { type: "image/png" }
248+
response = process("file")
249+
assert_equal "image/png", response.headers["Content-Type"]
250+
end
251+
245252
def test_send_file_charset_with_content_type_options_key
246253
@controller = SendFileWithActionControllerLive.new
247254
@controller.options = { content_type: "text/calendar" }
248255
response = process("file")
249-
assert_equal "text/calendar; charset=utf-8", response.headers["Content-Type"]
256+
assert_equal "text/calendar", response.headers["Content-Type"]
250257
end
251258
end

0 commit comments

Comments
 (0)