lib/httpx/resolver/native.rb in httpx-0.3.0 vs lib/httpx/resolver/native.rb in httpx-0.3.1

- old
+ new

@@ -79,13 +79,13 @@ rescue Errno::EHOSTUNREACH => e @ns_index += 1 if @ns_index < @nameserver.size transition(:idle) else - ex = ResolvError.new(e.message) - ex.set_backtrace(e.backtrace) - raise ex + @queries.each do |host, channel| + emit_resolve_error(channel, host, e) + end end end def interests readable = !@read_buffer.full? @@ -98,11 +98,11 @@ end def <<(channel) return if early_resolve(channel) if @nameserver.nil? - ex = ResolveError.new("Can't resolve #{channel.uri.host}") + ex = ResolveError.new("Can't resolve #{channel.uri.host}: no nameserver") ex.set_backtrace(caller) emit(:error, channel, ex) else @channels << channel end @@ -176,14 +176,25 @@ return if siz.zero? end end def parse(buffer) - addresses = Resolver.decode_dns_answer(buffer) + addresses = begin + Resolver.decode_dns_answer(buffer) + rescue Resolv::DNS::DecodeError => e + hostname, channel = @queries.first + if @_record_types[hostname].empty? + emit_resolve_error(channel, hostname, e) + return + end + end + if addresses.empty? hostname, channel = @queries.first + @_record_types[hostname].shift if @_record_types[hostname].empty? + @_record_types.delete(hostname) emit_resolve_error(channel, hostname) return end else address = addresses.first @@ -210,12 +221,16 @@ def resolve(channel = @channels.first, hostname = nil) raise Error, "no URI to resolve" unless channel return unless @write_buffer.empty? hostname = hostname || @queries.key(channel) || channel.uri.host @queries[hostname] = channel - type = @_record_types[hostname].shift + type = @_record_types[hostname].first log(label: "resolver: ") { "query #{type} for #{hostname}" } - @write_buffer << Resolver.encode_dns_query(hostname, type: RECORD_TYPES[type]) + begin + @write_buffer << Resolver.encode_dns_query(hostname, type: RECORD_TYPES[type]) + rescue Resolv::DNS::EncodeError => e + emit_resolve_error(channel, hostname, e) + end end def build_socket return if @io ip, port = @nameserver[@ns_index]