Skip to content

Commit 550176f

Browse files
authored
Add Protocol::HTTP::Peer interface. (#72)
1 parent b6d92cb commit 550176f

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

lib/protocol/http/peer.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2017-2024, by Samuel Williams.
5+
6+
module Protocol
7+
module HTTP
8+
# Provide a well defined, cached representation of a peer (address).
9+
class Peer
10+
def self.for(io)
11+
if address = io.remote_address
12+
return new(address)
13+
end
14+
end
15+
16+
def initialize(address)
17+
@address = address
18+
19+
if address.ip?
20+
@ip_address = @address.ip_address
21+
end
22+
end
23+
24+
attr :address
25+
attr :ip_address
26+
27+
alias remote_address address
28+
end
29+
end
30+
end

lib/protocol/http/request.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ def initialize(scheme = nil, authority = nil, method = nil, path = nil, version
6464
# @attribute [Proc] a callback which is called when an interim response is received.
6565
attr_accessor :interim_response
6666

67+
# A request that is generated by a server, may choose to include the peer (address) associated with the request. It should be implemented by a sub-class.
68+
#
69+
# @returns [Peer | Nil] The peer (address) associated with the request.
70+
def peer
71+
nil
72+
end
73+
6774
# Send the request to the given connection.
6875
def call(connection)
6976
connection.call(self)

lib/protocol/http/response.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ def initialize(version = nil, status = 200, headers = Headers.new, body = nil, p
5252
# @attribute [String | Array(String) | Nil] The protocol, e.g. `"websocket"`, etc.
5353
attr_accessor :protocol
5454

55+
# A response that is generated by a client, may choose to include the peer (address) associated with the response. It should be implemented by a sub-class.
56+
#
57+
# @returns [Peer | Nil] The peer (address) associated with the response.
58+
def peer
59+
nil
60+
end
61+
5562
# Whether the response is considered a hijack: the connection has been taken over by the application and the server should not send any more data.
5663
def hijack?
5764
false

test/protocol/http/peer.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
require "protocol/http/peer"
7+
require "socket"
8+
9+
describe Protocol::HTTP::Peer do
10+
it "can be created from IO" do
11+
address = Addrinfo.tcp("192.168.1.1", 80)
12+
io = Socket.new(:AF_INET, :SOCK_STREAM)
13+
expect(io).to receive(:remote_address).and_return(address)
14+
15+
peer = Protocol::HTTP::Peer.for(io)
16+
expect(peer).to have_attributes(
17+
address: be_equal(address),
18+
)
19+
end
20+
end

0 commit comments

Comments
 (0)