Skip to content

Commit b499e0d

Browse files
committed
Simplify
1 parent 2549e1f commit b499e0d

File tree

1 file changed

+17
-86
lines changed

1 file changed

+17
-86
lines changed

lib/mint/http1.ex

Lines changed: 17 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -548,103 +548,34 @@ defmodule Mint.HTTP1 do
548548
}
549549
"""
550550
def recv_response(conn, ref, timeout) do
551-
recv_response(conn, ref, timeout, %{status: nil, headers: [], body: ""}, fn
552-
conn, {:status, status}, acc ->
553-
{:cont, conn, %{acc | status: status}}
554-
555-
conn, {:headers, headers}, acc ->
556-
{:cont, conn, update_in(acc.headers, &(&1 ++ headers))}
557-
558-
conn, {:data, data}, acc ->
559-
{:cont, conn, update_in(acc.body, &(&1 <> data))}
560-
561-
conn, :done, acc ->
562-
{:cont, conn, acc}
563-
end)
551+
recv_response([], %{status: nil, headers: [], body: ""}, conn, ref, timeout)
564552
end
565553

566-
@doc """
567-
TODO
568-
569-
## Examples
570-
571-
iex> {:ok, conn} = Mint.HTTP1.connect(:https, "httpbin.org", 443, mode: :passive)
572-
iex> {:ok, conn, ref} = Mint.HTTP1.request(conn, "GET", "/user-agent", [], nil)
573-
iex> {:ok, _conn, _acc} =
574-
...> Mint.HTTP1.recv_response(conn, ref, 5000, nil, fn
575-
...> conn, entry, acc ->
576-
...> IO.inspect(entry)
577-
...> {:cont, conn, acc}
578-
...> end)
579-
580-
Outputs:
581-
582-
{:status, 201}
583-
{:headers, [{"date", ...}, ...]}
584-
{:data, "{\\n \\"user-agent\\": \\"mint/1.6.2\\"\\n}\\n"}
585-
:done
586-
"""
587-
def recv_response(conn, ref, timeout, acc, fun) do
588-
recv_response([], acc, fun, conn, ref, timeout)
554+
defp recv_response([{:status, ref, status} | rest], acc, conn, ref, timeout) do
555+
acc = put_in(acc.status, status)
556+
recv_response(rest, acc, conn, ref, timeout)
589557
end
590558

591-
defp recv_response([{:status, ref, status} | rest], acc, fun, conn, ref, timeout) do
592-
case fun.(conn, {:status, status}, acc) do
593-
{:cont, conn, acc} ->
594-
recv_response(rest, acc, fun, conn, ref, timeout)
595-
596-
{:halt, conn, acc} ->
597-
{:ok, conn, acc}
598-
599-
other ->
600-
raise ArgumentError,
601-
"expected {:cont, conn, acc} or {:halt, conn, acc}, got: #{inspect(other)}"
602-
end
559+
defp recv_response([{:headers, ref, headers} | rest], acc, conn, ref, timeout) do
560+
acc = update_in(acc.headers, &(&1 ++ headers))
561+
recv_response(rest, acc, conn, ref, timeout)
603562
end
604563

605-
defp recv_response([{:headers, ref, headers} | rest], acc, fun, conn, ref, timeout) do
606-
case fun.(conn, {:headers, headers}, acc) do
607-
{:cont, conn, acc} ->
608-
recv_response(rest, acc, fun, conn, ref, timeout)
609-
610-
{:halt, conn, acc} ->
611-
{:ok, conn, acc}
612-
613-
other ->
614-
raise ArgumentError,
615-
"expected {:cont, conn, acc} or {:halt, conn, acc}, got: #{inspect(other)}"
616-
end
564+
defp recv_response([{:data, ref, data} | rest], acc, conn, ref, timeout) do
565+
acc = update_in(acc.body, &(&1 <> data))
566+
recv_response(rest, acc, conn, ref, timeout)
617567
end
618568

619-
defp recv_response([{:data, ref, data} | rest], acc, fun, conn, ref, timeout) do
620-
case fun.(conn, {:data, data}, acc) do
621-
{:cont, conn, acc} ->
622-
recv_response(rest, acc, fun, conn, ref, timeout)
623-
624-
{:halt, conn, acc} ->
625-
{:ok, conn, acc}
626-
627-
other ->
628-
raise ArgumentError,
629-
"expected {:cont, conn, acc} or {:halt, conn, acc}, got: #{inspect(other)}"
630-
end
569+
defp recv_response([{:done, ref} | _], acc, conn, ref, _timeout) do
570+
{:ok, conn, acc}
631571
end
632572

633-
defp recv_response([{:done, ref} | _], acc, fun, conn, ref, _timeout) do
634-
case fun.(conn, :done, acc) do
635-
{:cont, conn, acc} ->
636-
{:ok, conn, acc}
637-
638-
{:halt, conn, acc} ->
639-
{:ok, conn, acc}
640-
641-
other ->
642-
raise ArgumentError,
643-
"expected {:cont, conn, acc} or {:halt, conn, acc}, got: #{inspect(other)}"
644-
end
573+
# Ignore entries from other requests.
574+
defp recv_response([_entry | rest], acc, conn, ref, timeout) do
575+
recv_response(rest, acc, conn, ref, timeout)
645576
end
646577

647-
defp recv_response([], acc, fun, conn, ref, timeout) do
578+
defp recv_response([], acc, conn, ref, timeout) do
648579
start_time = System.monotonic_time(:millisecond)
649580

650581
with {:ok, conn, entries} <- recv(conn, 0, timeout) do
@@ -655,7 +586,7 @@ defmodule Mint.HTTP1 do
655586
timeout
656587
end
657588

658-
recv_response(entries, acc, fun, conn, ref, timeout)
589+
recv_response(entries, acc, conn, ref, timeout)
659590
end
660591
end
661592

0 commit comments

Comments
 (0)