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

allow send_file/send_data to skip disposition header, closes #2973 #6078

Merged
merged 2 commits into from Apr 30, 2012
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
30 changes: 12 additions & 18 deletions actionpack/lib/action_controller/metal/data_streaming.rb
Expand Up @@ -8,10 +8,8 @@ module DataStreaming

include ActionController::Rendering

DEFAULT_SEND_FILE_OPTIONS = {
:type => 'application/octet-stream'.freeze,
:disposition => 'attachment'.freeze,
}.freeze
DEFAULT_SEND_FILE_TYPE = 'application/octet-stream'.freeze #:nodoc:
DEFAULT_SEND_FILE_DISPOSITION = 'attachment'.freeze #:nodoc:

protected
# Sends the file. This uses a server-appropriate method (such as X-Sendfile)
Expand Down Expand Up @@ -127,23 +125,16 @@ def each
#
# See +send_file+ for more information on HTTP Content-* headers and caching.
def send_data(data, options = {}) #:doc:
send_file_headers! options.dup
send_file_headers! options
render options.slice(:status, :content_type).merge(:text => data)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we are modifying the original options, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed send_file_headers! method and it doesn't modify options now.


private
def send_file_headers!(options)
type_provided = options.has_key?(:type)

options.update(DEFAULT_SEND_FILE_OPTIONS.merge(options))
[:type, :disposition].each do |arg|
raise ArgumentError, ":#{arg} option required" if options[arg].nil?
end

disposition = options[:disposition]
disposition += %(; filename="#{options[:filename]}") if options[:filename]

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

if content_type.is_a?(Symbol)
extension = Mime[content_type]
Expand All @@ -157,10 +148,13 @@ def send_file_headers!(options)
self.content_type = content_type
end

headers.merge!(
'Content-Disposition' => disposition,
'Content-Transfer-Encoding' => 'binary'
)
disposition = options.fetch(:disposition, DEFAULT_SEND_FILE_DISPOSITION)
unless disposition.nil?
disposition += %(; filename="#{options[:filename]}") if options[:filename]
headers['Content-Disposition'] = disposition
end

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

response.sending_file = true

Expand Down
11 changes: 11 additions & 0 deletions actionpack/test/controller/send_file_test.rb
Expand Up @@ -154,6 +154,17 @@ def test_send_file_headers_guess_type_from_extension
end
end

def test_send_file_with_default_content_disposition_header
process('data')
assert_equal 'attachment', @controller.headers['Content-Disposition']
end

def test_send_file_without_content_disposition_header
@controller.options = {:disposition => nil}
process('data')
assert_nil @controller.headers['Content-Disposition']
end

%w(file data).each do |method|
define_method "test_send_#{method}_status" do
@controller.options = { :stream => false, :status => 500 }
Expand Down