From 914998489a94c0cd940fa3a187b3e3d965372fe7 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:29:44 -0600 Subject: [PATCH 1/2] Raise ResponseHeaderError when response headers cannot be parsed A malformed header line, such as one without a colon, makes llhttp fail while the response headers are still being read. That error was wrapped in SocketReadError, whose message only said that reading from the socket failed, even though the socket was healthy and the header bytes were the problem. Convert failures raised before the headers are complete into ResponseHeaderError while preserving the parser message and the original backtrace. EOF before the headers are complete keeps its current error. Fixes #674. --- lib/http/connection.rb | 7 ++++++- test/http/connection_test.rb | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/http/connection.rb b/lib/http/connection.rb index e550a4b6..c132b0a4 100644 --- a/lib/http/connection.rb +++ b/lib/http/connection.rb @@ -150,7 +150,12 @@ def readpartial(size = BUFFER_SIZE, outbuf = nil) # @api public def read_headers! until @parser.headers? - result = read_more(BUFFER_SIZE) + begin + result = read_more(BUFFER_SIZE) + rescue SocketReadError => e + raise ResponseHeaderError, "couldn't read response headers: #{e.message}", e.backtrace + end + raise ResponseHeaderError, "couldn't read response headers" if result == :eof end diff --git a/test/http/connection_test.rb b/test/http/connection_test.rb index 33790bad..ee5dee93 100644 --- a/test/http/connection_test.rb +++ b/test/http/connection_test.rb @@ -496,6 +496,20 @@ def test_read_headers_raises_response_header_error_on_eof_before_headers_complet assert_includes err.message, "couldn't read response headers" end + def test_read_headers_raises_response_header_error_on_malformed_header + req = build_req + raw_response = "HTTP/1.1 200 OK\r\nContent-Type: text\r\nver\r\n\r\n" + read_socket = fake(connect: nil, close: nil, readpartial: raw_response) + read_timeout_class = fake(new: read_socket) + read_opts = HTTP::Options.new(timeout_class: read_timeout_class) + conn = HTTP::Connection.new(req, read_opts) + conn.instance_variable_set(:@pending_response, true) + + err = assert_raises(HTTP::ResponseHeaderError) { conn.read_headers! } + assert_includes err.message, "couldn't read response headers" + assert_includes err.message, "Invalid header token" + end + def test_read_headers_calls_set_keep_alive_after_reading req = build_req raw_response = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n" From feaa27e3aa8268ed2c3602c167c0f1e6db67b276 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:45:50 -0600 Subject: [PATCH 2/2] Keep Connection within the rubocop class length limit The begin/rescue in read_headers! added four lines to a class already at the 100 line cap, failing Metrics/ClassLength in CI. Use a method level rescue like initialize does, inline the single use result variable, and drop the single use finished local in readpartial. Behavior is unchanged. --- lib/http/connection.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/http/connection.rb b/lib/http/connection.rb index c132b0a4..59d7fd06 100644 --- a/lib/http/connection.rb +++ b/lib/http/connection.rb @@ -132,9 +132,8 @@ def readpartial(size = BUFFER_SIZE, outbuf = nil) unless chunk eof = read_more(size) == :eof check_premature_eof(eof) - finished = eof || @parser.finished? - chunk = @parser.read(size) || "".b - finish_response if finished + chunk = @parser.read(size) || "".b + finish_response if eof || @parser.finished? end outbuf ? outbuf.replace(chunk) : chunk @@ -150,16 +149,12 @@ def readpartial(size = BUFFER_SIZE, outbuf = nil) # @api public def read_headers! until @parser.headers? - begin - result = read_more(BUFFER_SIZE) - rescue SocketReadError => e - raise ResponseHeaderError, "couldn't read response headers: #{e.message}", e.backtrace - end - - raise ResponseHeaderError, "couldn't read response headers" if result == :eof + raise ResponseHeaderError, "couldn't read response headers" if read_more(BUFFER_SIZE) == :eof end set_keep_alive + rescue SocketReadError => e + raise ResponseHeaderError, "couldn't read response headers: #{e.message}", e.backtrace end # Callback for when we've reached the end of a response