Skip to content

Commit 2501ea7

Browse files
committed
Small cleanups and refactors
1 parent 1ab3eeb commit 2501ea7

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

lib/mint/http.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ defmodule Mint.HTTP do
763763
with a response status code and a reason-phrase. The reason-phrase is a string.
764764
Returned when the `:optional_responses` option is passed to `connect/4`, with
765765
`:status_reason` in the list. See `Mint.HTTP1.connect/4` for more information.
766-
This is only available for HTTP/1.1 connections. *Available since v1.7.2*.
766+
This is only available for HTTP/1.1 connections. *Available since v1.8.0*.
767767
768768
* `{:headers, request_ref, headers}` - returned when the server replied
769769
with a list of headers. Headers are in the form `{header_name, header_value}`

lib/mint/http1.ex

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ defmodule Mint.HTTP1 do
132132
conforming URIs but need to preserve them. The default is to validate the request
133133
target. *Available since v1.7.0*.
134134
* `:optional_responses` - (list of atoms) a list of optional responses to return.
135-
The possible values in the list are -
136-
* `:status_reason` which will return the
135+
Defaults to `[]`. The allowed values in the list are:
136+
* `:status_reason`: includes the
137137
[reason-phrase](https://datatracker.ietf.org/doc/html/rfc9112#name-status-line)
138-
for the status code, if it is returned by the server in status-line.
139-
This is only available for HTTP/1.1 connections. *Available since v1.7.2*.
138+
for the status code if it is returned by the server in the status-line.
139+
This is only available for HTTP/1.1 connections. *Available since v1.8.0*.
140140
141141
"""
142142
@spec connect(Types.scheme(), Types.address(), :inet.port_number(), keyword()) ::
@@ -233,8 +233,8 @@ defmodule Mint.HTTP1 do
233233
|> Enum.map(fn opt ->
234234
if opt not in @optional_responses_opts do
235235
raise ArgumentError, """
236-
invalid :optional_responses value #{opt}.
237-
allowed values are - #{inspect(@optional_responses_opts)}
236+
invalid :optional_responses value #{inspect(opt)}, the allowed values are: \
237+
#{inspect(@optional_responses_opts)}\
238238
"""
239239
end
240240

@@ -671,10 +671,18 @@ defmodule Mint.HTTP1 do
671671

672672
defp decode(:status, %{request: request} = conn, data, responses) do
673673
case Response.decode_status_line(data) do
674-
{:ok, {version, status, _reason} = status_line, rest} ->
674+
{:ok, {version, status, status_reason}, rest} ->
675675
request = %{request | version: version, status: status, state: :headers}
676676
conn = %{conn | request: request}
677-
responses = put_status_responses(conn, status_line, responses)
677+
responses = [{:status, request.ref, status} | responses]
678+
679+
responses =
680+
if :status_reason in conn.optional_responses do
681+
[{:status_reason, request.ref, status_reason} | responses]
682+
else
683+
responses
684+
end
685+
678686
decode(:headers, conn, rest, responses)
679687

680688
:more ->
@@ -897,20 +905,6 @@ defmodule Mint.HTTP1 do
897905
end
898906
end
899907

900-
defp put_status_responses(
901-
%{request: request, optional_responses: optional_responses},
902-
{_version, status, reason},
903-
responses
904-
) do
905-
responses = [{:status, request.ref, status} | responses]
906-
907-
if Enum.member?(optional_responses, :status_reason) do
908-
[{:status_reason, request.ref, reason} | responses]
909-
else
910-
responses
911-
end
912-
end
913-
914908
defp store_header(%{content_length: nil} = request, "content-length", value) do
915909
with {:ok, content_length} <- Parse.content_length_header(value),
916910
do: {:ok, %{request | content_length: content_length}}

0 commit comments

Comments
 (0)