Skip to content

Instantly share code, notes, and snippets.

@tomtaylor
Created November 7, 2023 22:35
Show Gist options
  • Save tomtaylor/30780323285967a567eba6b8c2e23576 to your computer and use it in GitHub Desktop.
Save tomtaylor/30780323285967a567eba6b8c2e23576 to your computer and use it in GitHub Desktop.
Mix.install([:mint])
defmodule Main do
require Logger
@byte_count 8192
def run() do
{:ok, conn} = Mint.HTTP.connect(:http, "speed.transip.nl", 80, mode: :passive)
{:ok, conn, _req_ref} = Mint.HTTP.request(conn, "GET", "/10mb.bin", [], "")
loop(conn)
end
defp loop(conn) do
{:ok, conn, responses} = Mint.HTTP.recv(conn, @byte_count, 5_000)
done = process_responses(responses, false)
if done do
Logger.info("Done")
else
loop(conn)
end
end
defp process_responses([], done) do
done
end
defp process_responses([{:status, _, _} | responses], _done) do
Logger.info("Received HTTP status")
process_responses(responses, false)
end
defp process_responses([{:headers, _, _} | responses], _done) do
Logger.info("Received headers")
process_responses(responses, false)
end
defp process_responses([{:data, _, bytes} | responses], _done) do
Logger.info("Received #{byte_size(bytes)} bytes")
process_responses(responses, false)
end
defp process_responses([{:done, _} | responses], _done) do
Logger.info("Received done")
process_responses(responses, true)
end
end
Main.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment