Add Response#peer_address and ErrorResponse#peer_address

responses can now expose the IP address used to connect to the peer
server to fetch the response from.
This commit is contained in:
HoneyryderChuck 2023-10-28 23:53:50 +01:00
parent 5bb74ec465
commit 9465a077b1
7 changed files with 32 additions and 1 deletions

View File

@ -462,6 +462,7 @@ module HTTPX
def send_request_to_parser(request)
@inflight += 1
request.peer_address = @io.ip
parser.send(request)
set_request_timeouts(request)

View File

@ -38,6 +38,9 @@ module HTTPX
# Exception raised during enumerable body writes.
attr_reader :drain_error
# The IP address from the peer server.
attr_accessor :peer_address
attr_writer :persistent
# will be +true+ when request body has been completely flushed.
@ -65,6 +68,7 @@ module HTTPX
@body = @options.request_body_class.new(@headers, @options)
@state = :idle
@response = nil
@peer_address = nil
@persistent = @options.persistent
end

View File

@ -44,6 +44,9 @@ module HTTPX
# the corresponding request uri.
def_delegator :@request, :uri
# the IP address of the peer server.
def_delegator :@request, :peer_address
# inits the instance with the corresponding +request+ to this response, an the
# response HTTP +status+, +version+ and HTTPX::Headers instance of +headers+.
def initialize(request, status, version, headers)
@ -226,6 +229,9 @@ module HTTPX
# the request uri
def_delegator :@request, :uri
# the IP address of the peer server.
def_delegator :@request, :peer_address
def initialize(request, error, options)
@request = request
@response = request.response if request.response.is_a?(Response)

View File

@ -2,7 +2,7 @@ module HTTPX
class TCP
include Loggable
attr_reader ip: IPAddr?
attr_reader ip: ipaddr?
attr_reader port: Integer

View File

@ -15,6 +15,8 @@ module HTTPX
attr_reader response: response?
attr_reader drain_error: StandardError?
attr_accessor peer_address: ipaddr?
attr_writer persistent: bool
@trailers: Headers?

View File

@ -25,12 +25,19 @@ module HTTPX
@content_type: ContentType
def copy_to: (_ToPath | _Writer destination) -> void
def close: () -> void
def uri: () -> URI::Generic
def peer_address: () -> ipaddr?
def merge_headers: (_Each[[String, headers_value]]) -> void
def bodyless?: () -> bool
def content_type: () -> ContentType
def complete?: () -> bool
def json: (?json_options opts) -> untyped
@ -77,6 +84,8 @@ module HTTPX
def uri: () -> URI::Generic
def peer_address: () -> ipaddr?
def close: () -> void
private

View File

@ -128,6 +128,15 @@ class SessionTest < Minitest::Test
end
end
def test_session_response_peer_address
uri = URI(build_uri("/get"))
response = HTTPX.get(uri)
verify_status(response, 200)
peer_address = response.peer_address
assert peer_address.is_a?(IPAddr)
assert Resolv.getaddresses(uri.host).include?(peer_address.to_s)
end
TestPlugin = Module.new do
self::ClassMethods = Module.new do
def foo