Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose body read timeouts as 408 Request Timeout errors #385

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/bandit/http1/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
read_timeout = Keyword.get(opts, :read_timeout)

iolist = read!(socket, bytes_to_read, [], read_size, read_timeout)
to_return = IO.iodata_to_binary([buffer | iolist])

Check warning on line 232 in lib/bandit/http1/socket.ex

View workflow job for this annotation

GitHub Actions / lint / lint (1.15.x, 26.x)

improper_list_constr

List construction (cons) will produce an improper list, because its second argument is
bytes_remaining = bytes_remaining - (byte_size(to_return) - byte_size(buffer))
{to_return, <<>>, bytes_remaining}
end
Expand All @@ -252,7 +252,7 @@

if to_read > 0 do
iolist = read!(socket, to_read, [], read_size, read_timeout)
buffer = IO.iodata_to_binary([buffer | iolist])

Check warning on line 255 in lib/bandit/http1/socket.ex

View workflow job for this annotation

GitHub Actions / lint / lint (1.15.x, 26.x)

improper_list_constr

List construction (cons) will produce an improper list, because its second argument is
do_read_chunked_data!(socket, buffer, body, read_size, read_timeout)
else
chunk = read_available!(socket, read_timeout)
Expand Down Expand Up @@ -313,7 +313,7 @@
end

{:error, :timeout} ->
already_read
request_error!("Body read timeout", :request_timeout)

{:error, reason} ->
request_error!(reason)
Expand Down
9 changes: 4 additions & 5 deletions test/bandit/http1/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -875,17 +875,16 @@ defmodule HTTP1RequestTest do
"POST /short_body HTTP/1.1\r\nhost: localhost\r\ncontent-length: 5\r\n\r\nABC"
)

assert {:ok, "200 OK", _, "OK"} = SimpleHTTP1Client.recv_reply(client)
assert {:ok, "408 Request Timeout", _, ""} = SimpleHTTP1Client.recv_reply(client)
Process.sleep(1100)
end)

assert errors =~ "(Bandit.HTTPError) Unable to read remaining data in request body"
assert errors =~ "(Bandit.HTTPError) Body read timeout"
end

def short_body(conn) do
{:more, "ABC", conn} = Plug.Conn.read_body(conn)
{:more, "", conn} = Plug.Conn.read_body(conn)
send_resp(conn, 200, "OK")
Plug.Conn.read_body(conn)
raise "Shouldn't get here"
end

test "handles the case where the declared content length is less than what is sent",
Expand Down
Loading