From 67bf1cf624ffc335d43d9be856b0ead60f54b084 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 9 Nov 2024 22:08:53 +1300 Subject: [PATCH] Expose cached peer address interface. (#189) --- lib/async/http/protocol/http1/connection.rb | 3 +- lib/async/http/protocol/http2/connection.rb | 3 +- lib/async/http/protocol/peer.rb | 32 +++++++++++++++++++++ lib/async/http/protocol/request.rb | 10 ++----- lib/async/http/protocol/response.rb | 10 ++----- lib/async/http/server.rb | 3 -- 6 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 lib/async/http/protocol/peer.rb diff --git a/lib/async/http/protocol/http1/connection.rb b/lib/async/http/protocol/http1/connection.rb index fb41351..f008647 100755 --- a/lib/async/http/protocol/http1/connection.rb +++ b/lib/async/http/protocol/http1/connection.rb @@ -5,6 +5,7 @@ require "protocol/http1" +require_relative "../peer" require_relative "request" require_relative "response" @@ -42,7 +43,7 @@ def http2? end def peer - @stream.io + @peer ||= Peer.for(@stream.io) end attr :count diff --git a/lib/async/http/protocol/http2/connection.rb b/lib/async/http/protocol/http2/connection.rb index 7d989d0..1a04e9c 100644 --- a/lib/async/http/protocol/http2/connection.rb +++ b/lib/async/http/protocol/http2/connection.rb @@ -5,6 +5,7 @@ # Copyright, 2020, by Bruno Sutic. require_relative "stream" +require_relative "../peer" require "async/semaphore" @@ -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 diff --git a/lib/async/http/protocol/peer.rb b/lib/async/http/protocol/peer.rb new file mode 100644 index 0000000..4331b0d --- /dev/null +++ b/lib/async/http/protocol/peer.rb @@ -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 diff --git a/lib/async/http/protocol/request.rb b/lib/async/http/protocol/request.rb index bada183..b364de0 100644 --- a/lib/async/http/protocol/request.rb +++ b/lib/async/http/protocol/request.rb @@ -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 diff --git a/lib/async/http/protocol/response.rb b/lib/async/http/protocol/response.rb index 751e8e9..76f761b 100644 --- a/lib/async/http/protocol/response.rb +++ b/lib/async/http/protocol/response.rb @@ -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 diff --git a/lib/async/http/server.rb b/lib/async/http/server.rb index bb7c515..be0a536 100755 --- a/lib/async/http/server.rb +++ b/lib/async/http/server.rb @@ -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.