Skip to content

Commit 67bf1cf

Browse files
authored
Expose cached peer address interface. (#189)
1 parent 6429f22 commit 67bf1cf

File tree

6 files changed

+40
-21
lines changed

6 files changed

+40
-21
lines changed

lib/async/http/protocol/http1/connection.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
require "protocol/http1"
77

8+
require_relative "../peer"
89
require_relative "request"
910
require_relative "response"
1011

@@ -42,7 +43,7 @@ def http2?
4243
end
4344

4445
def peer
45-
@stream.io
46+
@peer ||= Peer.for(@stream.io)
4647
end
4748

4849
attr :count

lib/async/http/protocol/http2/connection.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright, 2020, by Bruno Sutic.
66

77
require_relative "stream"
8+
require_relative "../peer"
89

910
require "async/semaphore"
1011

@@ -112,7 +113,7 @@ def read_in_background(parent: Task.current)
112113
attr :promises
113114

114115
def peer
115-
@stream.io
116+
@peer ||= Peer.for(@stream.io)
116117
end
117118

118119
attr :count

lib/async/http/protocol/peer.rb

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

lib/async/http/protocol/request.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,11 @@ def write_interim_response(status, headers = nil)
2929
end
3030

3131
def peer
32-
if connection = self.connection
33-
connection.peer
34-
end
32+
self.connection&.peer
3533
end
3634

3735
def remote_address
38-
@remote_address ||= peer.remote_address
39-
end
40-
41-
def remote_address= value
42-
@remote_address = value
36+
self.peer&.address
4337
end
4438

4539
def inspect

lib/async/http/protocol/response.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,11 @@ def hijack?
2121
end
2222

2323
def peer
24-
if connection = self.connection
25-
connection.peer
26-
end
24+
self.connection&.peer
2725
end
2826

2927
def remote_address
30-
@remote_address ||= peer.remote_address
31-
end
32-
33-
def remote_address= value
34-
@remote_address = value
28+
self.peer&.remote_address
3529
end
3630

3731
def inspect

lib/async/http/server.rb

-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ def accept(peer, address, task: Task.current)
5252
# https://tools.ietf.org/html/rfc7230#section-5.5
5353
request.scheme ||= self.scheme
5454

55-
# This is a slight optimization to avoid having to get the address from the socket.
56-
request.remote_address = address
57-
5855
# Console.logger.debug(self) {"Incoming request from #{address.inspect}: #{request.method} #{request.path}"}
5956

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

0 commit comments

Comments
 (0)