exposing original hostname in errors when there's a dns error in a candidate name for resolution

This commit is contained in:
HoneyryderChuck 2022-03-28 11:10:09 +01:00
parent 0490175835
commit 64f8ebcf51
3 changed files with 17 additions and 18 deletions

View File

@ -98,24 +98,24 @@ module HTTPX
request = build_request(hostname)
request.on(:response, &method(:on_response).curry(2)[request])
request.on(:promise, &method(:on_promise))
@requests[request] = connection
@requests[request] = hostname
resolver_connection.send(request)
@connections << connection
rescue ResolveError, Resolv::DNS::EncodeError, JSON::JSONError => e
@queries.delete(hostname)
emit_resolve_error(connection, hostname, e)
emit_resolve_error(connection, connection.origin.host, e)
end
end
def on_response(request, response)
response.raise_for_status
rescue StandardError => e
connection = @requests[request]
hostname = @queries.key(connection)
emit_resolve_error(connection, hostname, e)
hostname = @requests.delete(request)
connection = @queries.delete(hostname)
emit_resolve_error(connection, connection.origin.host, e)
else
# @type var response: HTTPX::Response
parse(response)
parse(request, response)
ensure
@requests.delete(request)
end
@ -125,22 +125,21 @@ module HTTPX
stream.refuse
end
def parse(response)
def parse(request, response)
begin
answers = decode_response_body(response)
rescue Resolv::DNS::DecodeError, JSON::JSONError => e
host, connection = @queries.first
@queries.delete(host)
emit_resolve_error(connection, host, e)
emit_resolve_error(connection, connection.origin.host, e)
return
end
if answers.nil? || answers.empty?
host, connection = @queries.shift
host = @requests.delete(request)
connection = @queries.delete(host)
emit_resolve_error(connection)
return
unless @queries.value?(connection)
emit_resolve_error(connection, host)
return
end
else
answers = answers.group_by { |answer| answer["name"] }
answers.each do |hostname, addresses|

View File

@ -159,7 +159,7 @@ module HTTPX
@connections.delete(connection)
# This loop_time passed to the exception is bogus. Ideally we would pass the total
# resolve timeout, including from the previous retries.
raise ResolveTimeoutError.new(loop_time, "Timed out while resolving #{host}")
raise ResolveTimeoutError.new(loop_time, "Timed out while resolving #{connection.origin.host}")
else
log { "resolver: timeout after #{timeout}s, retry(#{@timeouts[host].first}) #{host}..." }
resolve(connection)
@ -195,7 +195,7 @@ module HTTPX
@queries.delete(hostname)
@timeouts.delete(hostname)
@connections.delete(connection)
ex = NativeResolveError.new(connection, hostname, e.message)
ex = NativeResolveError.new(connection, connection.origin.host, e.message)
ex.set_backtrace(e.backtrace)
raise ex
end
@ -207,7 +207,7 @@ module HTTPX
unless @queries.value?(connection)
@connections.delete(connection)
raise NativeResolveError.new(connection, hostname)
raise NativeResolveError.new(connection, connection.origin.host)
end
else
address = addresses.first

View File

@ -8,7 +8,7 @@ module HTTPX
@family: ip_family
@options: Options
@requests: Hash[Request, Connection]
@requests: Hash[Request, String]
@connections: Array[Connection]
@uri: URI::Generic
@uri_addresses: Array[String]?
@ -29,7 +29,7 @@ module HTTPX
def on_response: (Request, response) -> void
def parse: (Response response) -> void
def parse: (Request request, Response response) -> void
def build_request: (String hostname) -> Request