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) def send_request_to_parser(request)
@inflight += 1 @inflight += 1
request.peer_address = @io.ip
parser.send(request) parser.send(request)
set_request_timeouts(request) set_request_timeouts(request)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -128,6 +128,15 @@ class SessionTest < Minitest::Test
end end
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 TestPlugin = Module.new do
self::ClassMethods = Module.new do self::ClassMethods = Module.new do
def foo def foo