Skip to content

Commit 9e88479

Browse files
Guard Peer.for against IO objects without remote_address.
Use respond_to?(:remote_address) before calling the method, so plain IO objects (e.g. stdin/stdout used by HTTY) return nil rather than raising NoMethodError. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a2f2d1c commit 9e88479

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎lib/protocol/http/peer.rb‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ class Peer
1111
#
1212
# @returns [Peer | Nil] The peer object, or nil if the remote address is not available.
1313
def self.for(io)
14-
if address = io.remote_address
15-
return new(address)
14+
if io.respond_to?(:remote_address)
15+
if address = io.remote_address
16+
return new(address)
17+
end
1618
end
19+
20+
return nil
1721
end
1822

1923
# Initialize the peer with the given address.

‎releases.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- `Protocol::HTTP::Peer.for` now returns `nil` for IO objects that do not respond to `remote_address`, rather than raising `NoMethodError`.
6+
37
## v0.62.1
48

59
- Fix handling of `Stream#read(0)`, it must return a mutable string (or clear the given buffer).

‎test/protocol/http/peer.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@
1717
address: be_equal(address),
1818
)
1919
end
20+
21+
it "returns nil for IO that does not support remote_address" do
22+
io = StringIO.new
23+
24+
expect(Protocol::HTTP::Peer.for(io)).to be_nil
25+
end
2026
end

0 commit comments

Comments
 (0)