Skip to content

Commit

Permalink
Replace downcase_ascii/1 implementation with String.downcase/2 (#424)
Browse files Browse the repository at this point in the history
Also moved "downcase_ascii_char/1" to "Mint.HTTP1.Parse" as it's the only module that uses it now.
  • Loading branch information
IceDragon200 authored Feb 11, 2024
1 parent a0afdce commit 99bd9bd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
11 changes: 0 additions & 11 deletions lib/mint/core/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,6 @@ defmodule Mint.Core.Util do
end
end

# Lowercases an ASCII string more efficiently than
# String.downcase/1.
@spec downcase_ascii(String.t()) :: String.t()
def downcase_ascii(string) do
for <<char <- string>>, do: <<downcase_ascii_char(char)>>, into: ""
end

@spec downcase_ascii_char(byte()) :: byte()
def downcase_ascii_char(char) when char in ?A..?Z, do: char + 32
def downcase_ascii_char(char) when char in 0..127, do: char

# If the buffer is empty, reusing the incoming data saves
# a potentially large allocation of memory.
# This should be fixed in a subsequent OTP release.
Expand Down
2 changes: 1 addition & 1 deletion lib/mint/http1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ defmodule Mint.HTTP1 do
end

defp lower_header_keys(headers) do
for {name, value} <- headers, do: {Util.downcase_ascii(name), value}
for {name, value} <- headers, do: {String.downcase(name, :ascii), value}
end

defp add_default_headers(headers, conn) do
Expand Down
5 changes: 4 additions & 1 deletion lib/mint/http1/parse.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Mint.HTTP1.Parse do
defp token_list_downcase(rest, acc), do: token_downcase(rest, _token_acc = <<>>, acc)

defp token_downcase(<<char, rest::binary>>, token_acc, acc) when is_tchar(char),
do: token_downcase(rest, <<token_acc::binary, Util.downcase_ascii_char(char)>>, acc)
do: token_downcase(rest, <<token_acc::binary, downcase_ascii_char(char)>>, acc)

defp token_downcase(rest, token_acc, acc), do: token_list_sep_downcase(rest, [token_acc | acc])

Expand All @@ -68,4 +68,7 @@ defmodule Mint.HTTP1.Parse do
do: token_list_downcase(rest, acc)

defp token_list_sep_downcase(_rest, _acc), do: :error

defp downcase_ascii_char(char) when char in ?A..?Z, do: char + 32
defp downcase_ascii_char(char) when char in 0..127, do: char
end
9 changes: 7 additions & 2 deletions lib/mint/http1/response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ defmodule Mint.HTTP1.Response do
end
end

defp header_name(atom) when is_atom(atom), do: atom |> Atom.to_string() |> Util.downcase_ascii()
defp header_name(binary) when is_binary(binary), do: Util.downcase_ascii(binary)
defp header_name(atom) when is_atom(atom) do
atom
|> Atom.to_string()
|> String.downcase(:ascii)
end

defp header_name(binary) when is_binary(binary), do: String.downcase(binary, :ascii)
end
4 changes: 2 additions & 2 deletions lib/mint/http2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ defmodule Mint.HTTP2 do
end

defp downcase_header_names(headers) do
for {name, value} <- headers, do: {Util.downcase_ascii(name), value}
for {name, value} <- headers, do: {String.downcase(name, :ascii), value}
end

defp add_default_headers(headers, body) do
Expand Down Expand Up @@ -1746,7 +1746,7 @@ defmodule Mint.HTTP2 do

defp join_cookie_headers(headers) do
# If we have 0 or 1 Cookie headers, we just use the old list of headers.
case Enum.split_with(headers, fn {name, _value} -> Util.downcase_ascii(name) == "cookie" end) do
case Enum.split_with(headers, fn {name, _value} -> String.downcase(name, :ascii) == "cookie" end) do
{[], _headers} ->
headers

Expand Down

0 comments on commit 99bd9bd

Please sign in to comment.