Skip to content

Do not reuse the connection unless the request body was fully sent - #360

Open
mame wants to merge 3 commits into
ruby:masterfrom
mame:expect-continue-close
Open

mame wants to merge 3 commits into
ruby:masterfrom
mame:expect-continue-close

Conversation

@mame

@mame mame commented Nov 2, 2026

Copy link
Copy Markdown
Member

When the server answers a request that carries Expect: 100-continue with a final status before the body has been sent, or when writing the body fails partway, the announced body never reaches the server in full. Close the connection after reading the response instead of keeping it alive for the next request. This follows the behavior of Java's java.net.http.HttpClient, which closes the connection after any response other than 100 to such a request, and of libcurl, which closes it when the body has not been sent completely.

Reported-by: Claude (Anthropic) and Stephan Lipp (Ada Logics)

mame and others added 2 commits November 11, 2026 18:20
When the server answers a request that carries Expect: 100-continue with
a final status before the body has been sent, or when writing the body
fails partway, the announced body never reaches the server in full.
Close the connection after reading the response instead of keeping it
alive for the next request. This follows the behavior of Java's
java.net.http.HttpClient, which closes the connection after any response
other than 100 to such a request, and of libcurl, which closes it when
the body has not been sent completely.

Reported-by: Claude (Anthropic) and Stephan Lipp (Ada Logics)
Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com>
wait_for_continue treated any response other than 100 Continue as the
final response, so an informational response such as 103 Early Hints
before 100 Continue made the request return 103 without sending its body.
Skip informational responses other than 100 and 101 until 100 Continue,
a final response, or the continue timeout.

Reported-by: Ashton D (https://hackerone.com/ashdfrkl)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mame
mame force-pushed the expect-continue-close branch from cd419fa to 3ba12ae Compare November 23, 2026 09:56
Comment thread lib/net/http/generic_request.rb Outdated
timeout = sock.continue_timeout
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout if timeout
informational_count = 0
while sock.io.to_io.wait_readable(timeout)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one concern.

Sometimes another response (100 Continue or a final response) arrives right after the 103, at the same time. In that case, when Net::HTTPResponse.read_new(sock) reads the 103, the next response is also read into the buffer of Net::BufferedIO.
But read_new only reads the 103 (up to the empty line). So the next response stays in the buffer.

After that, sock.io.to_io.wait_readable(timeout) in the next loop does not check the buffer. The response is already in the buffer, but it waits until the timeout ends.

So I think we should check the buffer before sock.io.to_io.wait_readable(timeout), and go to Net::HTTPResponse.read_new(sock) without waiting if there is unread data.
But Net::BufferedIO has no public method for this check. Net::BufferedIO#rbuf_size is private, so I am not sure if it is OK to call it.

Also, after the timeout, the body is sent. Then HTTPResponse.read_new in Net::HTTP#transport_request reads the response left in the buffer. So I think the final result is correct, although the request waits for continue_timeout when the 103 and the next response arrive together.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, good catch! It now checks the buffer size before waiting on the socket, and I added tests for the case you pointed out.

rbuf_size is private, so I call it with __send__ for now. I'd like to add a public Net::BufferedIO#wait_readable to net-protocol later.

…ntinue

A 103 response read by Net::BufferedIO could pull the next response into
its buffer, and wait_readable on the socket then waited until
continue_timeout although the response had already arrived.  Check the
buffer, and the pending data of an SSL socket, before waiting.  Use
Net::BufferedIO#wait_readable instead when it is defined.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mame
mame force-pushed the expect-continue-close branch from 8061c62 to 5e78828 Compare November 26, 2026 13:58

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants