Skip to content

Commit

Permalink
Return errors from chunk/2 calls in HTTP/1 (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrudel authored Aug 25, 2023
1 parent 7aa6e8a commit 10fbf54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/bandit/http1/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,7 @@ defmodule Bandit.HTTP1.Adapter do
@impl Plug.Conn.Adapter
def chunk(%__MODULE__{socket: socket}, chunk) do
byte_size = chunk |> IO.iodata_length() |> Integer.to_string(16)
_ = ThousandIsland.Socket.send(socket, [byte_size, "\r\n", chunk, "\r\n"])
:ok
ThousandIsland.Socket.send(socket, [byte_size, "\r\n", chunk, "\r\n"])
end

@impl Plug.Conn.Adapter
Expand Down
28 changes: 28 additions & 0 deletions test/bandit/http1/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,34 @@ defmodule HTTP1RequestTest do
conn
end

test "returns socket errors on chunk calls", context do
client = SimpleHTTP1Client.tcp_client(context)

errors =
capture_log(fn ->
SimpleHTTP1Client.send(client, "GET", "/erroring_chunk", ["host: localhost"])
Process.sleep(500)
assert {:ok, "200 OK", _headers, "2\r\nOK\r\n"} = SimpleHTTP1Client.recv_reply(client)
end)

assert errors == ""
end

def erroring_chunk(conn) do
{:ok, conn} =
conn
|> send_chunked(200)
|> chunk("OK")

# This is a pretty bogus wayr to get an error out of socket sending, but it's easy to set up
{_, adapter} = conn.adapter
ThousandIsland.Socket.close(adapter.socket)

assert {:error, :closed} == chunk(conn, "NOT OK")

conn
end

test "writes out a sent file for the entire file with content length", context do
response = Req.get!(context.req, url: "/send_full_file")

Expand Down

0 comments on commit 10fbf54

Please sign in to comment.