Skip to content

Commit

Permalink
Expose cached peer address interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 9, 2024
1 parent 6429f22 commit 9fbad7c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
3 changes: 2 additions & 1 deletion lib/async/http/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require "protocol/http1"

require_relative "../peer"
require_relative "request"
require_relative "response"

Expand Down Expand Up @@ -42,7 +43,7 @@ def http2?
end

def peer
@stream.io
@peer ||= Peer.for(@stream.io)
end

attr :count
Expand Down
3 changes: 2 additions & 1 deletion lib/async/http/protocol/http2/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Copyright, 2020, by Bruno Sutic.

require_relative "stream"
require_relative "../peer"

require "async/semaphore"

Expand Down Expand Up @@ -112,7 +113,7 @@ def read_in_background(parent: Task.current)
attr :promises

def peer
@stream.io
@peer ||= Peer.for(@stream.io)
end

attr :count
Expand Down
32 changes: 32 additions & 0 deletions lib/async/http/protocol/peer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

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

module Async
module HTTP
module Protocol
# 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
end
10 changes: 2 additions & 8 deletions lib/async/http/protocol/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,11 @@ def write_interim_response(status, headers = nil)
end

def peer
if connection = self.connection
connection.peer
end
self.connection&.peer
end

def remote_address
@remote_address ||= peer.remote_address
end

def remote_address= value
@remote_address = value
self.peer&.address
end

def inspect
Expand Down
10 changes: 2 additions & 8 deletions lib/async/http/protocol/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@ def hijack?
end

def peer
if connection = self.connection
connection.peer
end
self.connection&.peer
end

def remote_address
@remote_address ||= peer.remote_address
end

def remote_address= value
@remote_address = value
self.peer&.remote_address
end

def inspect
Expand Down
3 changes: 0 additions & 3 deletions lib/async/http/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ def accept(peer, address, task: Task.current)
# https://tools.ietf.org/html/rfc7230#section-5.5
request.scheme ||= self.scheme

# This is a slight optimization to avoid having to get the address from the socket.
request.remote_address = address

# Console.logger.debug(self) {"Incoming request from #{address.inspect}: #{request.method} #{request.path}"}

# If this returns nil, we assume that the connection has been hijacked.
Expand Down

0 comments on commit 9fbad7c

Please sign in to comment.