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

Send download encodes reserved chars #5928

Merged
merged 2 commits into from
Oct 22, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/phoenix/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ defmodule Phoenix.Controller do
end

defp encode_filename(filename, false), do: filename
defp encode_filename(filename, true), do: URI.encode(filename)
defp encode_filename(filename, true), do: URI.encode(filename, &URI.char_unreserved?/1)

defp get_disposition_type(:attachment), do: "attachment"
defp get_disposition_type(:inline), do: "inline"
Expand Down
11 changes: 11 additions & 0 deletions test/phoenix/controller/controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,17 @@ defmodule Phoenix.Controller.ControllerTest do
"world"
end

test "sends file for download for filename with unreserved characters" do
conn = send_download(conn(:get, "/"), {:file, @hello_txt}, filename: "hello, world.json")
assert conn.status == 200
assert get_resp_header(conn, "content-disposition") ==
["attachment; filename=\"hello%2C%20world.json\"; filename*=utf-8''hello%2C%20world.json"]
assert get_resp_header(conn, "content-type") ==
["application/json"]
assert conn.resp_body ==
"world"
end

test "sends file supports UTF-8" do
conn = send_download(conn(:get, "/"), {:file, @hello_txt}, filename: "测 试.txt")
assert conn.status == 200
Expand Down