removing resolver options object, using plain hashes instead

This commit is contained in:
HoneyryderChuck 2020-12-02 01:58:02 +00:00
parent a675fde7dd
commit 4c61c524ed
7 changed files with 21 additions and 50 deletions

View File

@ -42,7 +42,7 @@ module HTTPX
end
# deprecated
# :nocov
# :nocov:
def plugins(*args, **opts)
warn ":#{__method__} is deprecated, use :plugin instead"
klass = is_a?(Session) ? self.class : Session
@ -50,7 +50,7 @@ module HTTPX
klass.instance_variable_set(:@default_options, klass.default_options.merge(default_options))
klass.plugins(*args, **opts).new
end
# :nocov
# :nocov:
def with(options, &blk)
branch(default_options.merge(options), &blk)
@ -69,12 +69,10 @@ module HTTPX
end
def method_missing(meth, *args, **options)
if meth =~ /\Awith_(.+)/
option = Regexp.last_match(1).to_sym
with(option => (args.first || options))
else
super
end
return super unless meth =~ /\Awith_(.+)/
option = Regexp.last_match(1).to_sym
with(option => (args.first || options))
end
def respond_to_missing?(meth, *args)

View File

@ -101,5 +101,3 @@ module HTTPX
end
end
end
require "httpx/resolver/options"

View File

@ -30,12 +30,12 @@ module HTTPX
def initialize(options)
@options = Options.new(options)
@resolver_options = Resolver::Options.new(DEFAULTS.merge(@options.resolver_options || {}))
@_record_types = Hash.new { |types, host| types[host] = @resolver_options.record_types.dup }
@resolver_options = DEFAULTS.merge(@options.resolver_options)
@_record_types = Hash.new { |types, host| types[host] = @resolver_options[:record_types].dup }
@queries = {}
@requests = {}
@connections = []
@uri = URI(@resolver_options.uri)
@uri = URI(@resolver_options[:uri])
@uri_addresses = nil
end
@ -172,7 +172,7 @@ module HTTPX
next unless connection # probably a retried query for which there's an answer
@connections.delete(connection)
Resolver.cached_lookup_set(hostname, addresses) if @resolver_options.cache
Resolver.cached_lookup_set(hostname, addresses) if @resolver_options[:cache]
emit_addresses(connection, addresses.map { |addr| addr["data"] })
end
end
@ -186,7 +186,7 @@ module HTTPX
rklass = @options.request_class
payload = Resolver.encode_dns_query(hostname, type: RECORD_TYPES[type])
if @resolver_options.use_get
if @resolver_options[:use_get]
params = URI.decode_www_form(uri.query.to_s)
params << ["type", type]
params << ["dns", Base64.urlsafe_encode64(payload, padding: false)]

View File

@ -51,15 +51,15 @@ module HTTPX
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
@ -162,7 +162,7 @@ module HTTPX
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?
@ -222,7 +222,7 @@ module HTTPX
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

View File

@ -1,25 +0,0 @@
# frozen_string_literal: true
module HTTPX
class Resolver::Options
def initialize(options = {})
@options = options
end
def method_missing(m, *, &block)
if @options.key?(m)
@options[m]
else
super
end
end
def respond_to_missing?(m, *)
@options.key?(m) || super
end
def to_h
@options
end
end
end

View File

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

View File

@ -14,9 +14,9 @@ module HTTPX
def initialize(options)
@options = Options.new(options)
@resolver_options = Resolver::Options.new(@options.resolver_options)
@resolver_options = @options.resolver_options
@state = :idle
resolv_options = @resolver_options.to_h
resolv_options = @resolver_options.dup
timeouts = resolv_options.delete(:timeouts)
resolv_options.delete(:cache)
@resolver = Resolv::DNS.new(resolv_options.empty? ? nil : resolv_options)