Skip to content

Commit

Permalink
Fix Bandit.HTTP1.Adapter.inform/3 return value
Browse files Browse the repository at this point in the history
Per https://hexdocs.pm/plug/Plug.Conn.Adapter.html#c:inform/3, the
callback should return:

    :ok | {:error, :not_supported}

The way Plug.Conn.inform! is implemented at the moment,
https://github.com/elixir-plug/plug/blob/v1.15.2/lib/plug/conn.ex#L1335,
it looked like Bandit did _not_ implement inform but it sure does!
  • Loading branch information
wojtekmach committed Nov 22, 2023
1 parent 124d697 commit 18e12ed
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
15 changes: 3 additions & 12 deletions lib/bandit/http1/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -505,19 +505,10 @@ defmodule Bandit.HTTP1.Adapter do
@impl Plug.Conn.Adapter
def inform(%__MODULE__{version: :"HTTP/1.0"}, _status, _headers), do: {:error, :not_supported}

def inform(%__MODULE__{socket: socket, version: version} = req, status, headers) do
start_time = Bandit.Telemetry.monotonic_time()

{header_iodata, header_metrics} = response_header(version, status, headers)
def inform(%__MODULE__{socket: socket, version: version}, status, headers) do
{header_iodata, _header_metrics} = response_header(version, status, headers)
_ = ThousandIsland.Socket.send(socket, header_iodata)

metrics =
req.metrics
|> Map.merge(header_metrics)
|> Map.put(:resp_start_time, start_time)
|> Map.put(:resp_body_bytes, 0)

{:ok, nil, %{req | metrics: metrics}}
:ok
end

defp response_header(nil, status, headers), do: response_header("HTTP/1.0", status, headers)
Expand Down
7 changes: 6 additions & 1 deletion test/bandit/http1/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ defmodule HTTP1RequestTest do

test "sending informational responses", context do
client = SimpleHTTP1Client.tcp_client(context)
SimpleHTTP1Client.send(client, "GET", "/send_inform", ["host: localhost"])
SimpleHTTP1Client.send(client, "GET", "/send_inform!", ["host: localhost"])

Process.sleep(100)
assert {:ok, "100 Continue", headers, rest} = SimpleHTTP1Client.recv_reply(client)
Expand All @@ -1356,6 +1356,11 @@ defmodule HTTP1RequestTest do
conn |> send_resp(200, "Informer")
end

def send_inform!(conn) do
conn = conn |> inform!(100, [{"x-from", "inform"}])
conn |> send_resp(200, "Informer")
end

test "reading HTTP version", context do
response = Req.get!(context.req, url: "/report_version")

Expand Down

0 comments on commit 18e12ed

Please sign in to comment.