Sha256: ea7d41dc04ca7789c2e8ec32aaba11170dcf6feaecd3871b6f6f9cf91e95a922

Contents?: true

Size: 1.99 KB

Versions: 23

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require "resolv"
require "ipaddr"

module HTTPX
  module Resolver
    module ResolverMixin
      include Callbacks
      include Loggable

      CHECK_IF_IP = proc do |name|
        begin
          IPAddr.new(name)
          true
        rescue ArgumentError
          false
        end
      end

      def uncache(connection)
        hostname = hostname || @queries.key(connection) || connection.origin.host
        Resolver.uncache(hostname)
        @_record_types[hostname].shift
      end

      private

      def emit_addresses(connection, addresses)
        addresses.map! do |address|
          address.is_a?(IPAddr) ? address : IPAddr.new(address.to_s)
        end
        log { "resolver: answer #{connection.origin.host}: #{addresses.inspect}" }
        connection.addresses = addresses
        catch(:coalesced) { emit(:resolve, connection) }
      end

      def early_resolve(connection, hostname: connection.origin.host)
        addresses = connection.addresses ||
                    ip_resolve(hostname) ||
                    (@resolver_options[:cache] && Resolver.cached_lookup(hostname)) ||
                    system_resolve(hostname)
        return unless addresses

        emit_addresses(connection, addresses)
      end

      def ip_resolve(hostname)
        [hostname] if CHECK_IF_IP[hostname]
      end

      def system_resolve(hostname)
        @system_resolver ||= Resolv::Hosts.new
        ips = @system_resolver.getaddresses(hostname)
        return if ips.empty?

        ips.map { |ip| IPAddr.new(ip) }
      end

      def emit_resolve_error(connection, hostname = connection.origin.host, ex = nil)
        emit(:error, connection, resolve_error(hostname, ex))
      end

      def resolve_error(hostname, ex = nil)
        return ex if ex.is_a?(ResolveError)

        message = ex ? ex.message : "Can't resolve #{hostname}"
        error = ResolveError.new(message)
        error.set_backtrace(ex ? ex.backtrace : caller)
        error
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
httpx-0.16.1 lib/httpx/resolver/resolver_mixin.rb
httpx-0.16.0 lib/httpx/resolver/resolver_mixin.rb
httpx-0.15.4 lib/httpx/resolver/resolver_mixin.rb
httpx-0.15.3 lib/httpx/resolver/resolver_mixin.rb
httpx-0.15.2 lib/httpx/resolver/resolver_mixin.rb
httpx-0.15.1 lib/httpx/resolver/resolver_mixin.rb
httpx-0.15.0 lib/httpx/resolver/resolver_mixin.rb
httpx-0.14.5 lib/httpx/resolver/resolver_mixin.rb
httpx-0.14.4 lib/httpx/resolver/resolver_mixin.rb
httpx-0.14.3 lib/httpx/resolver/resolver_mixin.rb
httpx-0.14.2 lib/httpx/resolver/resolver_mixin.rb
httpx-0.14.1 lib/httpx/resolver/resolver_mixin.rb
httpx-0.14.0 lib/httpx/resolver/resolver_mixin.rb
httpx-0.13.2 lib/httpx/resolver/resolver_mixin.rb
httpx-0.13.1 lib/httpx/resolver/resolver_mixin.rb
httpx-0.13.0 lib/httpx/resolver/resolver_mixin.rb
httpx-0.12.0 lib/httpx/resolver/resolver_mixin.rb
httpx-0.11.3 lib/httpx/resolver/resolver_mixin.rb
httpx-0.11.2 lib/httpx/resolver/resolver_mixin.rb
httpx-0.11.1 lib/httpx/resolver/resolver_mixin.rb