Module: HTTPX::Resolver

Extended by:
Registry
Defined in:
lib/httpx/resolver.rb,
lib/httpx/resolver/resolver_mixin.rb

Defined Under Namespace

Modules: ResolverMixin Classes: HTTPS, Native, Options, System

Constant Summary

Constants included from Registry

HTTPX::Registry::Error

Class Method Summary collapse

Methods included from Registry

extended, included

Class Method Details

.cached_lookup(hostname) ⇒ Object



26
27
28
29
30
31
# File 'lib/httpx/resolver.rb', line 26

def cached_lookup(hostname)
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @lookup_mutex.synchronize do
    lookup(hostname, now)
  end
end

.cached_lookup_set(hostname, entries) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/httpx/resolver.rb', line 33

def cached_lookup_set(hostname, entries)
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  entries.each do |entry|
    entry["TTL"] += now
  end
  @lookup_mutex.synchronize do
    @lookups[hostname] += entries
    entries.each do |entry|
      @lookups[entry["name"]] << entry if entry["name"] != hostname
    end
  end
end

.decode_dns_answer(payload) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/httpx/resolver.rb', line 81

def decode_dns_answer(payload)
  message = Resolv::DNS::Message.decode(payload)
  addresses = []
  message.each_answer do |question, _, value|
    case value
    when Resolv::DNS::Resource::IN::CNAME
      addresses << {
        "name" => question.to_s,
        "TTL" => value.ttl,
        "alias" => value.name.to_s,
      }
    when Resolv::DNS::Resource::IN::A,
         Resolv::DNS::Resource::IN::AAAA
      addresses << {
        "name" => question.to_s,
        "TTL" => value.ttl,
        "data" => value.address.to_s,
      }
    end
  end
  addresses
end

.encode_dns_query(hostname, type: Resolv::DNS::Resource::IN::A) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/httpx/resolver.rb', line 73

def encode_dns_query(hostname, type: Resolv::DNS::Resource::IN::A)
  Resolv::DNS::Message.new.tap do |query|
    query.id = generate_id
    query.rd = 1
    query.add_question(hostname, type)
  end.encode
end

.generate_idObject



69
70
71
# File 'lib/httpx/resolver.rb', line 69

def generate_id
  @identifier_mutex.synchronize { @identifier = (@identifier + 1) & 0xFFFF }
end

.lookup(hostname, ttl) ⇒ Object

do not use directly!



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/httpx/resolver.rb', line 53

def lookup(hostname, ttl)
  return unless @lookups.key?(hostname)

  @lookups[hostname] = @lookups[hostname].select do |address|
    address["TTL"] > ttl
  end
  ips = @lookups[hostname].flat_map do |address|
    if address.key?("alias")
      lookup(address["alias"], ttl)
    else
      address["data"]
    end
  end
  ips unless ips.empty?
end

.uncache(hostname) ⇒ Object



46
47
48
49
50
# File 'lib/httpx/resolver.rb', line 46

def uncache(hostname)
  @lookup_mutex.synchronize do
    @lookups.delete(hostname)
  end
end