mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-12-04 00:01:13 -05:00
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:
parent
5bb74ec465
commit
9465a077b1
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -2,7 +2,7 @@ module HTTPX
|
||||
class TCP
|
||||
include Loggable
|
||||
|
||||
attr_reader ip: IPAddr?
|
||||
attr_reader ip: ipaddr?
|
||||
|
||||
attr_reader port: Integer
|
||||
|
||||
|
||||
@ -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?
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user