Skip to content

Commit

Permalink
Add Protocol::HTTP::Peer interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 9, 2024
1 parent b6d92cb commit f90a991
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/protocol/http/peer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2017-2024, by Samuel Williams.

module Protocol
module HTTP
# Provide a well defined, cached representation of a peer (address).
class Peer
def self.for(io)
if address = io.remote_address
return new(address)
end
end

def initialize(address)
@address = address

if address.ip?
@ip_address = @address.ip_address
end
end

attr :address
attr :ip_address

alias remote_address address
end
end
end
7 changes: 7 additions & 0 deletions lib/protocol/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def initialize(scheme = nil, authority = nil, method = nil, path = nil, version
# @attribute [Proc] a callback which is called when an interim response is received.
attr_accessor :interim_response

# 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.
#
# @returns [Peer | Nil] The peer (address) associated with the request.
def peer
nil
end

# Send the request to the given connection.
def call(connection)
connection.call(self)
Expand Down
7 changes: 7 additions & 0 deletions lib/protocol/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def initialize(version = nil, status = 200, headers = Headers.new, body = nil, p
# @attribute [String | Array(String) | Nil] The protocol, e.g. `"websocket"`, etc.
attr_accessor :protocol

# 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.
#
# @returns [Peer | Nil] The peer (address) associated with the response.
def peer
nil
end

# 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.
def hijack?
false
Expand Down
20 changes: 20 additions & 0 deletions test/protocol/http/peer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require "protocol/http/peer"
require "socket"

describe Protocol::HTTP::Peer do
it "can be created from IO" do
address = Addrinfo.tcp("192.168.1.1", 80)
io = Socket.new(:AF_INET, :SOCK_STREAM)
expect(io).to receive(:remote_address).and_return(address)

peer = Protocol::HTTP::Peer.for(io)
expect(peer).to have_attributes(
address: be_equal(address),
)
end
end

0 comments on commit f90a991

Please sign in to comment.