Skip to content
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
7 changes: 4 additions & 3 deletions lib/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def read(length)

# Read a line from the connection.
#
# @returns [String | Nil] the line read, or nil if the connection is gracefully closed.
# @returns [String | Nil] the line read, or nil if the connection is closed.
# @raises [LineLengthError] if the line is too long.
# @raises [ProtocolError] if the line is not terminated properly.
def read_line?
Expand All @@ -366,8 +366,9 @@ def read_line?
end

return line

# I considered rescuing Errno::ECONNRESET here, but it seems like that would be ignoring a potentially serious error.
# If a connection is shut down abruptly, we treat it as EOF, but only specifically in `read_line?`.
rescue Errno::ECONNRESET
return nil
end

# Read a line from the connection.
Expand Down
1 change: 1 addition & 0 deletions releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Tidy up implementation of `read_line?` to handle line length errors and protocol violations more clearly.
- Improve error handling for unexpected connection closures (`Errno::ECONNRESET`) in `read_line?`.

## v0.35.0

Expand Down
6 changes: 6 additions & 0 deletions test/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
server.read_line?
end.to raise_exception(Protocol::HTTP1::ProtocolError)
end

it "returns nil on Errno::ECONNRESET" do
expect(server.stream).to receive(:gets).and_raise(Errno::ECONNRESET)

expect(server.read_line?).to be_nil
end
end

with "#read_request" do
Expand Down
Loading