From ce8102b57a9bf8b4159cd0096b31d08b5d70ad09 Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Sun, 11 Feb 2024 12:40:32 +0100 Subject: [PATCH] Refactor small things here and there --- lib/mint/core/util.ex | 10 ++++++++++ lib/mint/http1.ex | 4 ---- lib/mint/http1/parse.ex | 2 -- lib/mint/http1/response.ex | 11 ++--------- lib/mint/http2.ex | 10 +++------- pages/Decompression.md | 2 +- 6 files changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/mint/core/util.ex b/lib/mint/core/util.ex index c5fd1d98..39c4e7a2 100644 --- a/lib/mint/core/util.ex +++ b/lib/mint/core/util.ex @@ -114,6 +114,16 @@ defmodule Mint.Core.Util do def maybe_concat(<<>>, data), do: data def maybe_concat(buffer, data) when is_binary(buffer), do: buffer <> data + @spec lower_header_name(String.t()) :: String.t() + def lower_header_name(name) do + String.downcase(name, :ascii) + end + + @spec lower_header_keys(Types.headers()) :: Types.headers() + def lower_header_keys(headers) do + :lists.map(fn {name, value} -> {lower_header_name(name), value} end, headers) + end + @spec find_unallowed_trailer_header(Types.headers()) :: {String.t(), String.t()} | nil def find_unallowed_trailer_header(headers) do Enum.find(headers, fn {name, _value} -> name in @unallowed_trailer_headers end) diff --git a/lib/mint/http1.ex b/lib/mint/http1.ex index a12b70d4..4462efde 100644 --- a/lib/mint/http1.ex +++ b/lib/mint/http1.ex @@ -972,10 +972,6 @@ defmodule Mint.HTTP1 do } end - defp lower_header_keys(headers) do - for {name, value} <- headers, do: {String.downcase(name, :ascii), value} - end - defp add_default_headers(headers, conn) do headers |> Util.put_new_header("user-agent", @user_agent) diff --git a/lib/mint/http1/parse.ex b/lib/mint/http1/parse.ex index f1414477..95310a5c 100644 --- a/lib/mint/http1/parse.ex +++ b/lib/mint/http1/parse.ex @@ -1,8 +1,6 @@ defmodule Mint.HTTP1.Parse do @moduledoc false - alias Mint.Core.Util - defmacro is_digit(char), do: quote(do: unquote(char) in ?0..?9) defmacro is_alpha(char), do: quote(do: unquote(char) in ?a..?z or unquote(char) in ?A..?Z) defmacro is_whitespace(char), do: quote(do: unquote(char) in ~c"\s\t") diff --git a/lib/mint/http1/response.ex b/lib/mint/http1/response.ex index abc8832f..e747e01a 100644 --- a/lib/mint/http1/response.ex +++ b/lib/mint/http1/response.ex @@ -1,8 +1,6 @@ defmodule Mint.HTTP1.Response do @moduledoc false - alias Mint.Core.Util - def decode_status_line(binary) do case :erlang.decode_packet(:http_bin, binary, []) do {:ok, {:http_response, version, status, reason}, rest} -> @@ -38,11 +36,6 @@ defmodule Mint.HTTP1.Response do end end - 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) + defp header_name(atom) when is_atom(atom), do: atom |> Atom.to_string() |> header_name() + defp header_name(binary) when is_binary(binary), do: Mint.Core.Util.lower_header_name(binary) end diff --git a/lib/mint/http2.ex b/lib/mint/http2.ex index 78d5559b..e9e67d61 100644 --- a/lib/mint/http2.ex +++ b/lib/mint/http2.ex @@ -521,7 +521,7 @@ defmodule Mint.HTTP2 do when is_binary(method) and is_binary(path) and is_list(headers) do headers = headers - |> downcase_header_names() + |> lower_header_keys() |> add_pseudo_headers(conn, method, path) |> add_default_headers(body) |> sort_pseudo_headers_to_front() @@ -1108,7 +1108,7 @@ defmodule Mint.HTTP2 do end defp encode_stream_body_request_payload(conn, stream_id, {:eof, trailer_headers}) do - lowered_headers = downcase_header_names(trailer_headers) + lowered_headers = lower_header_keys(trailer_headers) if unallowed_trailer_header = Util.find_unallowed_trailer_header(lowered_headers) do error = wrap_error({:unallowed_trailing_header, unallowed_trailer_header}) @@ -1344,10 +1344,6 @@ defmodule Mint.HTTP2 do end) end - defp downcase_header_names(headers) do - for {name, value} <- headers, do: {String.downcase(name, :ascii), value} - end - defp add_default_headers(headers, body) do headers |> Util.put_new_header("user-agent", @user_agent) @@ -1746,7 +1742,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} -> String.downcase(name, :ascii) == "cookie" end) do + case Enum.split_with(headers, fn {name, _value} -> lower_header_name(name) == "cookie" end) do {[], _headers} -> headers diff --git a/pages/Decompression.md b/pages/Decompression.md index 6e3b43a3..8192655b 100644 --- a/pages/Decompression.md +++ b/pages/Decompression.md @@ -31,7 +31,7 @@ We need to attempt to decompress the data if the `content-encoding` header is pr defp get_content_encoding_header(headers) do headers |> Enum.flat_map([], fn {name, value} -> - if String.downcase(name) == "content-encoding" do + if String.downcase(name, :ascii) == "content-encoding" do value |> String.downcase() |> String.split(",", trim: true)