Skip to content

Commit ce8102b

Browse files
committed
Refactor small things here and there
1 parent 99bd9bd commit ce8102b

File tree

6 files changed

+16
-23
lines changed

6 files changed

+16
-23
lines changed

lib/mint/core/util.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ defmodule Mint.Core.Util do
114114
def maybe_concat(<<>>, data), do: data
115115
def maybe_concat(buffer, data) when is_binary(buffer), do: buffer <> data
116116

117+
@spec lower_header_name(String.t()) :: String.t()
118+
def lower_header_name(name) do
119+
String.downcase(name, :ascii)
120+
end
121+
122+
@spec lower_header_keys(Types.headers()) :: Types.headers()
123+
def lower_header_keys(headers) do
124+
:lists.map(fn {name, value} -> {lower_header_name(name), value} end, headers)
125+
end
126+
117127
@spec find_unallowed_trailer_header(Types.headers()) :: {String.t(), String.t()} | nil
118128
def find_unallowed_trailer_header(headers) do
119129
Enum.find(headers, fn {name, _value} -> name in @unallowed_trailer_headers end)

lib/mint/http1.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,6 @@ defmodule Mint.HTTP1 do
972972
}
973973
end
974974

975-
defp lower_header_keys(headers) do
976-
for {name, value} <- headers, do: {String.downcase(name, :ascii), value}
977-
end
978-
979975
defp add_default_headers(headers, conn) do
980976
headers
981977
|> Util.put_new_header("user-agent", @user_agent)

lib/mint/http1/parse.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule Mint.HTTP1.Parse do
22
@moduledoc false
33

4-
alias Mint.Core.Util
5-
64
defmacro is_digit(char), do: quote(do: unquote(char) in ?0..?9)
75
defmacro is_alpha(char), do: quote(do: unquote(char) in ?a..?z or unquote(char) in ?A..?Z)
86
defmacro is_whitespace(char), do: quote(do: unquote(char) in ~c"\s\t")

lib/mint/http1/response.ex

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule Mint.HTTP1.Response do
22
@moduledoc false
33

4-
alias Mint.Core.Util
5-
64
def decode_status_line(binary) do
75
case :erlang.decode_packet(:http_bin, binary, []) do
86
{:ok, {:http_response, version, status, reason}, rest} ->
@@ -38,11 +36,6 @@ defmodule Mint.HTTP1.Response do
3836
end
3937
end
4038

41-
defp header_name(atom) when is_atom(atom) do
42-
atom
43-
|> Atom.to_string()
44-
|> String.downcase(:ascii)
45-
end
46-
47-
defp header_name(binary) when is_binary(binary), do: String.downcase(binary, :ascii)
39+
defp header_name(atom) when is_atom(atom), do: atom |> Atom.to_string() |> header_name()
40+
defp header_name(binary) when is_binary(binary), do: Mint.Core.Util.lower_header_name(binary)
4841
end

lib/mint/http2.ex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ defmodule Mint.HTTP2 do
521521
when is_binary(method) and is_binary(path) and is_list(headers) do
522522
headers =
523523
headers
524-
|> downcase_header_names()
524+
|> lower_header_keys()
525525
|> add_pseudo_headers(conn, method, path)
526526
|> add_default_headers(body)
527527
|> sort_pseudo_headers_to_front()
@@ -1108,7 +1108,7 @@ defmodule Mint.HTTP2 do
11081108
end
11091109

11101110
defp encode_stream_body_request_payload(conn, stream_id, {:eof, trailer_headers}) do
1111-
lowered_headers = downcase_header_names(trailer_headers)
1111+
lowered_headers = lower_header_keys(trailer_headers)
11121112

11131113
if unallowed_trailer_header = Util.find_unallowed_trailer_header(lowered_headers) do
11141114
error = wrap_error({:unallowed_trailing_header, unallowed_trailer_header})
@@ -1344,10 +1344,6 @@ defmodule Mint.HTTP2 do
13441344
end)
13451345
end
13461346

1347-
defp downcase_header_names(headers) do
1348-
for {name, value} <- headers, do: {String.downcase(name, :ascii), value}
1349-
end
1350-
13511347
defp add_default_headers(headers, body) do
13521348
headers
13531349
|> Util.put_new_header("user-agent", @user_agent)
@@ -1746,7 +1742,7 @@ defmodule Mint.HTTP2 do
17461742

17471743
defp join_cookie_headers(headers) do
17481744
# If we have 0 or 1 Cookie headers, we just use the old list of headers.
1749-
case Enum.split_with(headers, fn {name, _value} -> String.downcase(name, :ascii) == "cookie" end) do
1745+
case Enum.split_with(headers, fn {name, _value} -> lower_header_name(name) == "cookie" end) do
17501746
{[], _headers} ->
17511747
headers
17521748

pages/Decompression.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ We need to attempt to decompress the data if the `content-encoding` header is pr
3131
defp get_content_encoding_header(headers) do
3232
headers
3333
|> Enum.flat_map([], fn {name, value} ->
34-
if String.downcase(name) == "content-encoding" do
34+
if String.downcase(name, :ascii) == "content-encoding" do
3535
value
3636
|> String.downcase()
3737
|> String.split(",", trim: true)

0 commit comments

Comments
 (0)