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

Don't blow up when passing frozen string to send_file disposition. #1137

Merged
merged 2 commits into from Aug 4, 2018
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
2 changes: 1 addition & 1 deletion lib/sinatra/base.rb
Expand Up @@ -358,7 +358,7 @@ def content_type(type = nil, params = {})
# Set the Content-Disposition to "attachment" with the specified filename,
# instructing the user agents to prompt to save.
def attachment(filename = nil, disposition = :attachment)
response['Content-Disposition'] = disposition.to_s
response['Content-Disposition'] = disposition.to_s.dup
if filename
params = '; filename="%s"' % File.basename(filename)
response['Content-Disposition'] << params
Expand Down
6 changes: 6 additions & 0 deletions test/helpers_test.rb
Expand Up @@ -879,6 +879,12 @@ def send_file_app(opts={})
assert_equal 'inline; filename="file.txt"', response['Content-Disposition']
end

it "does not raise an error when :disposition set to a frozen string" do
send_file_app :disposition => 'inline'.freeze
Copy link
Member

Choose a reason for hiding this comment

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

Where does this actually occur?

Copy link
Author

@aselder aselder May 6, 2017

Choose a reason for hiding this comment

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

@zzak It happened to up when we added the magic frozen string literal comment at the top of our files.

Since we had inline as a literal in the file, it was frozen and then the in attachment method we get an error modifying a frozen string literal.

As rubocop, code climate, etc are now flagging files with string literals non-frozen I suspect this will be coming up more and more

Copy link

Choose a reason for hiding this comment

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

I have the same problem after passing the script through rubocop

get '/file.txt'
assert_equal 'inline; filename="file.txt"', response['Content-Disposition']
end

it "sets the Content-Disposition header when :filename provided" do
send_file_app :filename => 'foo.txt'
get '/file.txt'
Expand Down