lib/httpx/resolver/https.rb in httpx-0.3.0 vs lib/httpx/resolver/https.rb in httpx-0.3.1
- old
+ new
@@ -1,7 +1,8 @@
# frozen_string_literal: true
+require "resolv"
require "uri"
require "cgi"
require "forwardable"
module HTTPX
@@ -65,17 +66,21 @@
end
def resolve(channel = @channels.first, hostname = nil)
return if @building_channel
hostname = hostname || @queries.key(channel) || channel.uri.host
- type = @_record_types[hostname].shift
+ type = @_record_types[hostname].first
log(label: "resolver: ") { "query #{type} for #{hostname}" }
- request = build_request(hostname, type)
- @requests[request] = channel
- resolver_channel.send(request)
- @queries[hostname] = channel
- @channels << channel
+ begin
+ request = build_request(hostname, type)
+ @requests[request] = channel
+ resolver_channel.send(request)
+ @queries[hostname] = channel
+ @channels << channel
+ rescue Resolv::DNS::EncodeError, JSON::JSONError => e
+ emit_resolve_error(channel, hostname, e)
+ end
end
def find_channel(_request, **options)
@connection.find_channel(@uri) || begin
@building_channel = true
@@ -105,14 +110,24 @@
ensure
@requests.delete(request)
end
def parse(response)
- answers = decode_response_body(response)
+ answers = begin
+ decode_response_body(response)
+ rescue Resolv::DNS::DecodeError, JSON::JSONError => e
+ host, channel = @queries.first
+ if @_record_types[host].empty?
+ emit_resolve_error(channel, host, e)
+ return
+ end
+ end
if answers.empty?
host, channel = @queries.first
+ @_record_types[host].shift
if @_record_types[host].empty?
+ @_record_types.delete(host)
emit_resolve_error(channel, host)
return
end
else
answers = answers.group_by { |answer| answer["name"] }
@@ -171,10 +186,9 @@
payload = JSON.parse(response.to_s)
payload["Answer"]
when "application/dns-udpwireformat",
"application/dns-message"
Resolver.decode_dns_answer(response.to_s)
-
# TODO: what about the rest?
end
end
end
end