diff --git a/lib/gemini.ex b/lib/gemini.ex index 75e80aa..f11a81e 100644 --- a/lib/gemini.ex +++ b/lib/gemini.ex @@ -21,13 +21,25 @@ defmodule Gemini do {:ok, sock} -> Socket.Stream.send(sock, "#{URI.to_string(uri)}\r\n") - case Socket.Stream.recv(sock) do - {:error, reason} -> - {:error, reason} + sock + |> receive_loop() + |> Gemini.Response.parse() + end + end - {:ok, data} -> - Gemini.Response.parse(data) - end + defp receive_loop(sock, acc \\ []) do + res = Socket.Stream.recv(sock) + + case res do + {:ok, nil} -> + Socket.Stream.close!(sock) + + acc + |> Enum.reverse() + |> IO.iodata_to_binary() + + {:ok, data} -> + receive_loop(sock, [data | acc]) end end