Skip to content

Commit

Permalink
Refactor small things here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Feb 11, 2024
1 parent 99bd9bd commit ce8102b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
10 changes: 10 additions & 0 deletions lib/mint/core/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions lib/mint/http1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions lib/mint/http1/parse.ex
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
11 changes: 2 additions & 9 deletions lib/mint/http1/response.ex
Original file line number Diff line number Diff line change
@@ -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} ->
Expand Down Expand Up @@ -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
10 changes: 3 additions & 7 deletions lib/mint/http2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pages/Decompression.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ce8102b

Please sign in to comment.