From d8dd2bdf4899eb19627e9428109216ffc3fea26e Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Wed, 31 Mar 2021 13:05:42 +0200 Subject: [PATCH 1/4] Ignore media type params --- lib/mime/application.ex | 11 ++++++++++- test/mime_test.exs | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/mime/application.ex b/lib/mime/application.ex index f41046d..6df7792 100644 --- a/lib/mime/application.ex +++ b/lib/mime/application.ex @@ -102,7 +102,12 @@ defmodule MIME.Application do """ @spec extensions(String.t()) :: [String.t()] def extensions(type) do - mime_to_ext(downcase(type, "")) || [] + mime = + type + |> strip_params() + |> downcase("") + + mime_to_ext(mime) || [] end @default_type "application/octet-stream" @@ -161,6 +166,10 @@ defmodule MIME.Application do end end + defp strip_params(string) do + string |> String.split(";", parts: 2) |> hd() + end + defp downcase(<>, acc) when h in ?A..?Z, do: downcase(t, <>) diff --git a/test/mime_test.exs b/test/mime_test.exs index bc8d03a..61f1437 100644 --- a/test/mime_test.exs +++ b/test/mime_test.exs @@ -7,13 +7,18 @@ defmodule MIMETest do test "valid?/1" do assert valid?("application/json") refute valid?("application/prs.vacation-photos") + + refute valid?("application/JSON") + refute valid?("application/json; charset=utf-8") end test "extensions/1" do - assert "json" in extensions("application/json") + assert extensions("application/json") == ["json"] assert extensions("application/vnd.api+json") == ["json-api"] assert extensions("audio/amr") == ["amr"] assert extensions("IMAGE/PNG") == ["png"] + + assert extensions("application/json; charset=utf-8") == ["json"] end test "type/1" do From b905cae654478c76ed34215a28a7c8fe659bcba5 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Wed, 31 Mar 2021 13:27:00 +0200 Subject: [PATCH 2/4] Detect subtype suffix in extensions/1 See: https://tools.ietf.org/html/rfc6838#section-4.2.8 --- lib/mime/application.ex | 12 +++++++++++- test/mime_test.exs | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/mime/application.ex b/lib/mime/application.ex index 6df7792..069a109 100644 --- a/lib/mime/application.ex +++ b/lib/mime/application.ex @@ -96,6 +96,9 @@ defmodule MIME.Application do iex> MIME.extensions("application/json") ["json"] + iex> MIME.extensions("application/vnd.custom+xml") + ["xml"] + iex> MIME.extensions("foo/bar") [] @@ -107,7 +110,14 @@ defmodule MIME.Application do |> strip_params() |> downcase("") - mime_to_ext(mime) || [] + mime_to_ext(mime) || suffix(mime) || [] + end + + defp suffix(type) do + case String.split(type, "+") do + [type_subtype_without_suffix, suffix] -> [suffix] + _ -> nil + end end @default_type "application/octet-stream" diff --git a/test/mime_test.exs b/test/mime_test.exs index 61f1437..054cbd0 100644 --- a/test/mime_test.exs +++ b/test/mime_test.exs @@ -19,6 +19,9 @@ defmodule MIMETest do assert extensions("IMAGE/PNG") == ["png"] assert extensions("application/json; charset=utf-8") == ["json"] + + assert extensions("application/vnd.custom+xml") == ["xml"] + assert extensions("application/vnd.custom+xml+xml") == [] end test "type/1" do From 16aa892b61c93504e3e9dd2c700a7cdfdb69eb8a Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Wed, 31 Mar 2021 14:00:52 +0200 Subject: [PATCH 3/4] Update lib/mime/application.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- lib/mime/application.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mime/application.ex b/lib/mime/application.ex index 069a109..f4c4e05 100644 --- a/lib/mime/application.ex +++ b/lib/mime/application.ex @@ -177,7 +177,7 @@ defmodule MIME.Application do end defp strip_params(string) do - string |> String.split(";", parts: 2) |> hd() + string |> :binary.split(";") |> hd() end defp downcase(<>, acc) when h in ?A..?Z, From 38f978e9c847c42e303b10897c0148a10bac0e1c Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Wed, 31 Mar 2021 14:01:54 +0200 Subject: [PATCH 4/4] Revert valid?/1 tests --- test/mime_test.exs | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/mime_test.exs b/test/mime_test.exs index 054cbd0..354fb2f 100644 --- a/test/mime_test.exs +++ b/test/mime_test.exs @@ -7,9 +7,6 @@ defmodule MIMETest do test "valid?/1" do assert valid?("application/json") refute valid?("application/prs.vacation-photos") - - refute valid?("application/JSON") - refute valid?("application/json; charset=utf-8") end test "extensions/1" do