Receive multiple chunks

This commit is contained in:
Shadowfacts 2020-07-18 16:27:40 -04:00
parent 18da6d23e1
commit 37864e9f11
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 18 additions and 6 deletions

View File

@ -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
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} ->
Gemini.Response.parse(data)
end
receive_loop(sock, [data | acc])
end
end