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

View File

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

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

View File

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