module HTTPX::Resolver

  1. lib/httpx/resolver.rb
  2. lib/httpx/resolver/https.rb
  3. lib/httpx/resolver/multi.rb
  4. lib/httpx/resolver/native.rb
  5. lib/httpx/resolver/resolver.rb
  6. lib/httpx/resolver/system.rb
  7. show all

Constants

RESOLVE_TIMEOUT = [2, 3].freeze  

Public Instance methods

cached_lookup(hostname)
[show source]
   # File lib/httpx/resolver.rb
54 def cached_lookup(hostname)
55   now = Utils.now
56   @lookup_mutex.synchronize do
57     lookup(hostname, now)
58   end
59 end
cached_lookup_set(hostname, family, entries)
[show source]
   # File lib/httpx/resolver.rb
61 def cached_lookup_set(hostname, family, entries)
62   now = Utils.now
63   entries.each do |entry|
64     entry["TTL"] += now
65   end
66   @lookup_mutex.synchronize do
67     case family
68     when Socket::AF_INET6
69       @lookups[hostname].concat(entries)
70     when Socket::AF_INET
71       @lookups[hostname].unshift(*entries)
72     end
73     entries.each do |entry|
74       next unless entry["name"] != hostname
75 
76       case family
77       when Socket::AF_INET6
78         @lookups[entry["name"]] << entry
79       when Socket::AF_INET
80         @lookups[entry["name"]].unshift(entry)
81       end
82     end
83   end
84 end
decode_dns_answer(payload)
[show source]
    # File lib/httpx/resolver.rb
117 def decode_dns_answer(payload)
118   begin
119     message = Resolv::DNS::Message.decode(payload)
120   rescue Resolv::DNS::DecodeError => e
121     return :decode_error, e
122   end
123 
124   # no domain was found
125   return :no_domain_found if message.rcode == Resolv::DNS::RCode::NXDomain
126 
127   return :message_truncated if message.tc == 1
128 
129   return :dns_error, message.rcode if message.rcode != Resolv::DNS::RCode::NoError
130 
131   addresses = []
132 
133   message.each_answer do |question, _, value|
134     case value
135     when Resolv::DNS::Resource::IN::CNAME
136       addresses << {
137         "name" => question.to_s,
138         "TTL" => value.ttl,
139         "alias" => value.name.to_s,
140       }
141     when Resolv::DNS::Resource::IN::A,
142          Resolv::DNS::Resource::IN::AAAA
143       addresses << {
144         "name" => question.to_s,
145         "TTL" => value.ttl,
146         "data" => value.address.to_s,
147       }
148     end
149   end
150 
151   [:ok, addresses]
152 end
encode_dns_query(hostname, type: Resolv::DNS::Resource::IN::A, message_id: generate_id)
[show source]
    # File lib/httpx/resolver.rb
109 def encode_dns_query(hostname, type: Resolv::DNS::Resource::IN::A, message_id: generate_id)
110   Resolv::DNS::Message.new.tap do |query|
111     query.id = message_id
112     query.rd = 1
113     query.add_question(hostname, type)
114   end.encode
115 end
generate_id()
[show source]
    # File lib/httpx/resolver.rb
105 def generate_id
106   @identifier_mutex.synchronize { @identifier = (@identifier + 1) & 0xFFFF }
107 end
ip_resolve(hostname)
[show source]
   # File lib/httpx/resolver.rb
41 def ip_resolve(hostname)
42   [IPAddr.new(hostname)]
43 rescue ArgumentError
44 end
lookup(hostname, ttl)

do not use directly!

[show source]
    # File lib/httpx/resolver.rb
 87 def lookup(hostname, ttl)
 88   return unless @lookups.key?(hostname)
 89 
 90   entries = @lookups[hostname] = @lookups[hostname].select do |address|
 91     address["TTL"] > ttl
 92   end
 93 
 94   ips = entries.flat_map do |address|
 95     if address.key?("alias")
 96       lookup(address["alias"], ttl)
 97     else
 98       IPAddr.new(address["data"])
 99     end
100   end.compact
101 
102   ips unless ips.empty?
103 end
nolookup_resolve(hostname)
[show source]
   # File lib/httpx/resolver.rb
37 def nolookup_resolve(hostname)
38   ip_resolve(hostname) || cached_lookup(hostname) || system_resolve(hostname)
39 end
resolver_for(resolver_type)
[show source]
   # File lib/httpx/resolver.rb
25 def resolver_for(resolver_type)
26   case resolver_type
27   when :native then Native
28   when :system then System
29   when :https then HTTPS
30   else
31     return resolver_type if resolver_type.is_a?(Class) && resolver_type < Resolver
32 
33     raise Error, "unsupported resolver type (#{resolver_type})"
34   end
35 end
system_resolve(hostname)
[show source]
   # File lib/httpx/resolver.rb
46 def system_resolve(hostname)
47   ips = @system_resolver.getaddresses(hostname)
48   return if ips.empty?
49 
50   ips.map { |ip| IPAddr.new(ip) }
51 rescue IOError
52 end