From eb2b13b40c286b5fc5ba1c728d9183a58aa70c0e Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 29 Oct 2026 16:31:18 +0900 Subject: [PATCH 1/3] Do not reuse the connection unless the request body was fully sent 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 --- lib/net/http.rb | 3 +++ test/net/http/test_http.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/net/http.rb b/lib/net/http.rb index 150e2277..38752b33 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -2516,10 +2516,12 @@ def send_entity(path, data, initheader, dest, type, &block) def transport_request(req) count = 0 begin + body_sent = false begin_transport req res = catch(:response) { begin req.exec @socket, @curr_http_version, edit_path(req.path) + body_sent = true rescue Errno::EPIPE # Failure when writing full request, but we can probably # still read the received response. @@ -2566,6 +2568,7 @@ def transport_request(req) raise end + @socket.close unless body_sent end_transport req, res res rescue => exception diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 8a306920..d7adf39c 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -1142,6 +1142,43 @@ def test_expect_continue_error_while_waiting assert_match(/Expect: 100-continue/, @debug.string) assert_not_match(/HTTP\/1.1 100 continue/, @debug.string) end + + def test_expect_continue_early_response + server = TCPServer.new(config('host'), 0) + th = Thread.new do + 2.times do + sock = server.accept + begin + while (reqline = sock.gets) + clen = 0 + while (line = sock.gets) && line != "\r\n" + clen = line.split(':', 2)[1].to_i if line.start_with?('Content-Length') + end + if reqline.start_with?('PUT') + sock.write "HTTP/1.1 429 Too Many Requests\r\nContent-Length: 0\r\n\r\n" + sock.read(clen) + else + sock.write "HTTP/1.1 200 OK\r\nContent-Length: #{reqline.bytesize}\r\n\r\n#{reqline}" + end + end + ensure + sock.close + end + end + end + http = Net::HTTP.new(config('host'), server.addr[1]) + http.continue_timeout = 1 + http.start { + res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_equal('429', res.code) + res = http.get('/second') + assert_equal("GET /second HTTP/1.1\r\n", res.body) + } + ensure + th&.kill + th&.join + server&.close + end end class TestNetHTTPSwitchingProtocols < Test::Unit::TestCase From 3ba12ae558807af4f3b126d87ac57961fe7b0091 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 12 Nov 2026 15:29:25 +0900 Subject: [PATCH 2/3] Wait for 100 Continue past other informational responses 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 --- lib/net/http/generic_request.rb | 20 ++++++++++++++---- test/net/http/test_http.rb | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index 96396b0a..d62d8252 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -411,12 +411,24 @@ def flush_buffer(out, buf, chunked_p) def wait_for_continue(sock, ver) if ver >= '1.1' and @header['expect'] and @header['expect'].include?('100-continue') - if sock.io.to_io.wait_readable(sock.continue_timeout) + 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) res = Net::HTTPResponse.read_new(sock) - unless res.kind_of?(Net::HTTPContinue) - res.decode_content = @decode_content - throw :response, res + break if res.kind_of?(Net::HTTPContinue) + if res.kind_of?(Net::HTTPInformation) && !res.kind_of?(Net::HTTPSwitchProtocol) + informational_count += 1 + raise Net::HTTPBadResponse, 'too many informational responses' if + informational_count > Net::HTTPResponse::MAX_INFORMATIONAL_RESPONSES + if deadline + timeout = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) + break if timeout <= 0 + end + next end + res.decode_content = @decode_content + throw :response, res end end end diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index d7adf39c..97e2f57d 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -1143,6 +1143,43 @@ def test_expect_continue_error_while_waiting assert_not_match(/HTTP\/1.1 100 continue/, @debug.string) end + def test_expect_continue_after_early_hints + server = TCPServer.new(config('host'), 0) + th = Thread.new do + sock = server.accept + begin + while (reqline = sock.gets) + clen = 0 + while (line = sock.gets) && line != "\r\n" + clen = line.split(':', 2)[1].to_i if line.start_with?('Content-Length') + end + if reqline.start_with?('PUT') + sock.write "HTTP/1.1 103 Early Hints\r\nLink: \r\n\r\nHTTP/1.1 100 Continue\r\n\r\n" + body = sock.read(clen) + sock.write "HTTP/1.1 201 Created\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}" + else + sock.write "HTTP/1.1 200 OK\r\nContent-Length: #{reqline.bytesize}\r\n\r\n#{reqline}" + end + end + ensure + sock.close + end + end + http = Net::HTTP.new(config('host'), server.addr[1]) + http.continue_timeout = 1 + http.start { + res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_equal('201', res.code) + assert_equal('data!', res.body) + res = http.get('/second') + assert_equal("GET /second HTTP/1.1\r\n", res.body) + } + ensure + th&.kill + th&.join + server&.close + end + def test_expect_continue_early_response server = TCPServer.new(config('host'), 0) th = Thread.new do From 5e7882872c11deb008ab5f96cf91e654b4e254fd Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 26 Nov 2026 22:47:02 +0900 Subject: [PATCH 3/3] Check buffered data before waiting for the response to Expect: 100-continue 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 --- lib/net/http/generic_request.rb | 9 ++++- test/net/http/test_http.rb | 68 ++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index d62d8252..7d6eb54f 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -414,7 +414,7 @@ def wait_for_continue(sock, ver) 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) + while response_available?(sock, timeout) res = Net::HTTPResponse.read_new(sock) break if res.kind_of?(Net::HTTPContinue) if res.kind_of?(Net::HTTPInformation) && !res.kind_of?(Net::HTTPSwitchProtocol) @@ -433,6 +433,13 @@ def wait_for_continue(sock, ver) end end + def response_available?(sock, timeout) + return sock.wait_readable(timeout) if sock.respond_to?(:wait_readable) + sock.__send__(:rbuf_size) > 0 || + (sock.io.respond_to?(:pending) && sock.io.pending > 0) || + sock.io.to_io.wait_readable(timeout) + end + def write_header(sock, ver, path) reqline = "#{@method} #{path} HTTP/#{ver}" if /[\r\n]/ =~ reqline diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 97e2f57d..417c42c1 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -1166,9 +1166,11 @@ def test_expect_continue_after_early_hints end end http = Net::HTTP.new(config('host'), server.addr[1]) - http.continue_timeout = 1 + http.continue_timeout = 10 http.start { + t = Process.clock_gettime(Process::CLOCK_MONOTONIC) res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_operator(Process.clock_gettime(Process::CLOCK_MONOTONIC) - t, :<, 5) assert_equal('201', res.code) assert_equal('data!', res.body) res = http.get('/second') @@ -1180,6 +1182,70 @@ def test_expect_continue_after_early_hints server&.close end + def test_expect_continue_early_hints_with_final_response + server = TCPServer.new(config('host'), 0) + th = Thread.new do + sock = server.accept + begin + while (reqline = sock.gets) + while (line = sock.gets) && line != "\r\n" + end + if reqline.start_with?('PUT') + sock.write "HTTP/1.1 103 Early Hints\r\nLink: \r\n\r\nHTTP/1.1 429 Too Many Requests\r\nContent-Length: 0\r\n\r\n" + else + sock.write "HTTP/1.1 200 OK\r\nContent-Length: #{reqline.bytesize}\r\n\r\n#{reqline}" + end + end + ensure + sock.close + end + end + http = Net::HTTP.new(config('host'), server.addr[1]) + http.continue_timeout = 10 + http.start { + t = Process.clock_gettime(Process::CLOCK_MONOTONIC) + res = http.request_put('/first', 'data!', 'expect' => '100-continue') + assert_equal('429', res.code) + assert_operator(Process.clock_gettime(Process::CLOCK_MONOTONIC) - t, :<, 5) + } + ensure + th&.kill + th&.join + server&.close + end + + def test_expect_continue_pending_ssl_data + io = Object.new + def io.pending + 1 + end + def io.to_io + raise "must not wait on the socket" + end + sock = Object.new + sock.define_singleton_method(:io) { io } + def sock.rbuf_size + 0 + end + sock.singleton_class.send(:private, :rbuf_size) + req = Net::HTTP::Put.new('/') + assert_equal(true, req.__send__(:response_available?, sock, 10)) + end + + def test_expect_continue_buffered_io_wait_readable + sock = Object.new + def sock.wait_readable(timeout) + @timeout = timeout + :ready + end + def sock.io + raise "must not look at the socket directly" + end + req = Net::HTTP::Put.new('/') + assert_equal(:ready, req.__send__(:response_available?, sock, 10)) + assert_equal(10, sock.instance_variable_get(:@timeout)) + end + def test_expect_continue_early_response server = TCPServer.new(config('host'), 0) th = Thread.new do