Skip to content

Commit

Permalink
Send download encodes reserved chars (#5928)
Browse files Browse the repository at this point in the history
Closes #5926.
  • Loading branch information
mcrumm authored Oct 22, 2024
1 parent 2b3c13b commit 842f242
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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

0 comments on commit 842f242

Please sign in to comment.