lib/httpx/resolver/native.rb in httpx-0.10.0 vs lib/httpx/resolver/native.rb in httpx-0.10.1

- old
+ new

@@ -13,11 +13,10 @@ RECORD_TYPES = { "A" => Resolv::DNS::Resource::IN::A, "AAAA" => Resolv::DNS::Resource::IN::AAAA, }.freeze - # :nocov: DEFAULTS = if RUBY_VERSION < "2.2" { **Resolv::DNS::Config.default_config_hash, packet_size: 512, timeouts: RESOLVE_TIMEOUT, @@ -42,28 +41,27 @@ true rescue IPAddr::InvalidAddressError false end end if DEFAULTS[:nameserver] - # :nocov: DNS_PORT = 53 def_delegator :@connections, :empty? def initialize(options) @options = Options.new(options) @ns_index = 0 - @resolver_options = Resolver::Options.new(DEFAULTS.merge(@options.resolver_options || {})) - @nameserver = @resolver_options.nameserver - @_timeouts = Array(@resolver_options.timeouts) + @resolver_options = DEFAULTS.merge(@options.resolver_options) + @nameserver = @resolver_options[:nameserver] + @_timeouts = Array(@resolver_options[:timeouts]) @timeouts = Hash.new { |timeouts, host| timeouts[host] = @_timeouts.dup } - @_record_types = Hash.new { |types, host| types[host] = @resolver_options.record_types.dup } + @_record_types = Hash.new { |types, host| types[host] = @resolver_options[:record_types].dup } @connections = [] @queries = {} @read_buffer = "".b - @write_buffer = Buffer.new(@resolver_options.packet_size) + @write_buffer = Buffer.new(@resolver_options[:packet_size]) @state = :idle end def close transition(:closed) @@ -109,13 +107,13 @@ def <<(connection) return if early_resolve(connection) if @nameserver.nil? - ex = ResolveError.new("Can't resolve #{connection.origin.host}: no nameserver") + ex = ResolveError.new("No available nameserver") ex.set_backtrace(caller) - emit(:error, connection, ex) + throw(:resolve_error, ex) else @connections << connection resolve end end @@ -162,11 +160,11 @@ end @queries = queries connections.each { |ch| resolve(ch) } end - def dread(wsize = @resolver_options.packet_size) + def dread(wsize = @resolver_options[:packet_size]) loop do siz = @io.read(wsize, @read_buffer) return unless siz && siz.positive? parse(@read_buffer) @@ -197,17 +195,18 @@ ex.set_backtrace(e.backtrace) raise ex end end - if addresses.empty? + if addresses.nil? || addresses.empty? hostname, connection = @queries.first @_record_types[hostname].shift if @_record_types[hostname].empty? @queries.delete(hostname) @_record_types.delete(hostname) @connections.delete(connection) + raise NativeResolveError.new(connection, hostname) end else address = addresses.first connection = @queries.delete(address["name"]) @@ -221,11 +220,11 @@ @queries.delete(address["name"]) return end else @connections.delete(connection) - Resolver.cached_lookup_set(connection.origin.host, addresses) if @resolver_options.cache + Resolver.cached_lookup_set(connection.origin.host, addresses) if @resolver_options[:cache] emit_addresses(connection, addresses.map { |addr| addr["data"] }) end end return emit(:close) if @connections.empty? @@ -241,10 +240,10 @@ if hostname.nil? hostname = connection.origin.host log { "resolver: resolve IDN #{connection.origin.non_ascii_hostname} as #{hostname}" } if connection.origin.non_ascii_hostname end @queries[hostname] = connection - type = @_record_types[hostname].first + type = @_record_types[hostname].first || "A" log { "resolver: query #{type} for #{hostname}" } begin @write_buffer << Resolver.encode_dns_query(hostname, type: RECORD_TYPES[type]) rescue Resolv::DNS::EncodeError => e emit_resolve_error(connection, hostname, e)