Merge branch 'issue-251' into 'master'

Add Response#peer_address and ErrorResponse#peer_address

Closes #251

See merge request os85/httpx!286
This commit is contained in:
HoneyryderChuck 2023-10-30 10:13:52 +00:00
commit 14c94e6d14
9 changed files with 34 additions and 3 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

@ -41,7 +41,7 @@ module HTTPX
end end
def socket def socket
@io.to_io @io
end end
def add_addresses(addrs) def add_addresses(addrs)

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

View File

@ -11,7 +11,7 @@ module Requests
response = HTTPX.plugin(SessionWithPool).on_connection_opened do |o, sock| response = HTTPX.plugin(SessionWithPool).on_connection_opened do |o, sock|
origin = o origin = o
ip = sock.remote_address.ip_address ip = sock.to_io.remote_address.ip_address
end.get(uri) end.get(uri)
verify_status(response, 200) verify_status(response, 200)