Skip to content

Raise ResponseHeaderError when response headers cannot be parsed - #866

Open
draarivpatel-ui wants to merge 2 commits into
httprb:mainfrom
draarivpatel-ui:fix-response-header-error
Open

draarivpatel-ui wants to merge 2 commits into
httprb:mainfrom
draarivpatel-ui:fix-response-header-error

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

When a response contains a malformed header line, such as a header without a colon, llhttp reports a parse error while the parser is still reading headers. Parser#add turns that into an IOError, and read_more then wraps it in HTTP::SocketReadError with error reading from socket: .... That message points at the socket even though the socket is healthy and the header bytes are the problem.

read_headers! now converts a SocketReadError raised before the headers are complete into HTTP::ResponseHeaderError, preserving the parser message and the original backtrace:

HTTP::ResponseHeaderError: couldn't read response headers:
error reading from socket: Error Parsing data: HPE_INVALID_HEADER_TOKEN Invalid header token

EOF before the headers are complete still raises ResponseHeaderError exactly as before, and parse errors after the headers are complete are unchanged.

Added a regression test that feeds a response with a ver line instead of a valid header field. It fails with SocketReadError before the change and passes after it. test/http/connection_test.rb (80 runs) and test/http/response/parser_test.rb pass.

Fixes #674.

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 httprb#674.
Copilot AI balanced review requested due to automatic review settings November 26, 2026 01:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

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.

Incorrect response headers give a generic message instead of indicating a problem parsing the headers

2 participants