Skip to content

Commit 3cfc4cd

Browse files
committed
Add specs to functions in Mint.Core.Util
1 parent 2ce7593 commit 3cfc4cd

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

.dialyzer_ignore.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[
22
{"lib/mint/tunnel_proxy.ex", :call_with_opaque, 49},
3+
{"lib/mint/http1.ex", :improper_list_constr},
34
~r{test/support},
45
~r{Function ExUnit.Assertion.* does not exist}
56
]

lib/mint/core/util.ex

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

4+
alias Mint.Types
5+
46
@unallowed_trailer_headers MapSet.new([
57
"content-encoding",
68
"content-length",
@@ -46,6 +48,7 @@ defmodule Mint.Core.Util do
4648
"warning"
4749
])
4850

51+
@spec hostname(keyword(), String.t()) :: String.t()
4952
def hostname(opts, address) when is_list(opts) do
5053
case Keyword.fetch(opts, :hostname) do
5154
{:ok, hostname} ->
@@ -59,6 +62,7 @@ defmodule Mint.Core.Util do
5962
end
6063
end
6164

65+
@spec inet_opts(:gen_tcp | :ssl, :gen_tcp.socket() | :ssl.sslsocket()) :: :ok | {:error, term()}
6266
def inet_opts(transport, socket) do
6367
with {:ok, opts} <- transport.getopts(socket, [:sndbuf, :recbuf, :buffer]),
6468
buffer = calculate_buffer(opts),
@@ -67,6 +71,7 @@ defmodule Mint.Core.Util do
6771
end
6872
end
6973

74+
@spec scheme_to_transport(atom()) :: module()
7075
def scheme_to_transport(:http), do: Mint.Core.Transport.TCP
7176
def scheme_to_transport(:https), do: Mint.Core.Transport.SSL
7277
def scheme_to_transport(module) when is_atom(module), do: module
@@ -78,6 +83,7 @@ defmodule Mint.Core.Util do
7883
end
7984

8085
# Adds a header to the list of headers unless it's nil or it's already there.
86+
@spec put_new_header(Types.headers(), String.t(), String.t() | nil) :: Types.headers()
8187
def put_new_header(headers, name, value)
8288

8389
def put_new_header(headers, _name, nil) do
@@ -92,6 +98,7 @@ defmodule Mint.Core.Util do
9298
end
9399
end
94100

101+
@spec put_new_header_lazy(Types.headers(), String.t(), (-> String.t())) :: Types.headers()
95102
def put_new_header_lazy(headers, name, fun) do
96103
if List.keymember?(headers, name, 0) do
97104
headers
@@ -102,22 +109,28 @@ defmodule Mint.Core.Util do
102109

103110
# Lowercases an ASCII string more efficiently than
104111
# String.downcase/1.
105-
def downcase_ascii(string),
106-
do: for(<<char <- string>>, do: <<downcase_ascii_char(char)>>, into: "")
112+
@spec downcase_ascii(String.t()) :: String.t()
113+
def downcase_ascii(string) do
114+
for <<char <- string>>, do: <<downcase_ascii_char(char)>>, into: ""
115+
end
107116

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

111121
# If the buffer is empty, reusing the incoming data saves
112122
# a potentially large allocation of memory.
113123
# This should be fixed in a subsequent OTP release.
124+
@spec maybe_concat(binary(), binary()) :: binary()
114125
def maybe_concat(<<>>, data), do: data
115126
def maybe_concat(buffer, data) when is_binary(buffer), do: buffer <> data
116127

128+
@spec find_unallowed_trailer_header(Types.headers()) :: {String.t(), String.t()} | nil
117129
def find_unallowed_trailer_header(headers) do
118130
Enum.find(headers, fn {name, _value} -> name in @unallowed_trailer_headers end)
119131
end
120132

133+
@spec remove_unallowed_trailer_headers(Types.headers()) :: Types.headers()
121134
def remove_unallowed_trailer_headers(headers) do
122135
Enum.reject(headers, fn {name, _value} -> name in @unallowed_trailer_headers end)
123136
end

0 commit comments

Comments
 (0)