lib/httpx/options.rb in httpx-0.18.7 vs lib/httpx/options.rb in httpx-0.19.0
- old
+ new
@@ -1,16 +1,30 @@
# frozen_string_literal: true
+require "socket"
+
module HTTPX
class Options
WINDOW_SIZE = 1 << 14 # 16K
MAX_BODY_THRESHOLD_SIZE = (1 << 10) * 112 # 112K
CONNECT_TIMEOUT = 60
OPERATION_TIMEOUT = 60
KEEP_ALIVE_TIMEOUT = 20
SETTINGS_TIMEOUT = 10
+ # https://github.com/ruby/resolv/blob/095f1c003f6073730500f02acbdbc55f83d70987/lib/resolv.rb#L408
+ ip_address_families = begin
+ list = Socket.ip_address_list
+ if list.any? { |a| a.ipv6? && !a.ipv6_loopback? && !a.ipv6_linklocal? }
+ [Socket::AF_INET6, Socket::AF_INET]
+ else
+ [Socket::AF_INET]
+ end
+ rescue NotImplementedError
+ [Socket::AF_INET]
+ end
+
DEFAULT_OPTIONS = {
:debug => ENV.key?("HTTPX_DEBUG") ? $stderr : nil,
:debug_level => (ENV["HTTPX_DEBUG"] || 1).to_i,
:ssl => {},
:http2_settings => { settings_enable_push: 0 },
@@ -35,10 +49,11 @@
:transport_options => nil,
:addresses => nil,
:persistent => false,
:resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym,
:resolver_options => { cache: true },
+ :ip_families => ip_address_families,
}.freeze
begin
module HashExtensions
refine Hash do
@@ -108,24 +123,22 @@
end
end
end
def initialize(options = {})
- defaults = DEFAULT_OPTIONS.merge(options)
- defaults.each do |k, v|
- next if v.nil?
-
- begin
- value = __send__(:"option_#{k}", v)
- instance_variable_set(:"@#{k}", value)
- rescue NoMethodError
- raise Error, "unknown option: #{k}"
- end
- end
+ __initialize__(options)
freeze
end
+ def freeze
+ super
+ @origin.freeze
+ @timeout.freeze
+ @headers.freeze
+ @addresses.freeze
+ end
+
def option_origin(value)
URI(value)
end
def option_headers(value)
@@ -172,10 +185,14 @@
def option_addresses(value)
Array(value)
end
+ def option_ip_families(value)
+ Array(value)
+ end
+
%i[
params form json body ssl http2_settings
request_class response_class headers_class request_body_class
response_body_class connection_class options_class
io fallback_protocol debug debug_level transport_options resolver_class resolver_options
@@ -244,9 +261,25 @@
value
else
value.dup
end
instance_variable_set(ivar, value)
+ end
+ end
+ end
+
+ private
+
+ def __initialize__(options = {})
+ defaults = DEFAULT_OPTIONS.merge(options)
+ defaults.each do |k, v|
+ next if v.nil?
+
+ begin
+ value = __send__(:"option_#{k}", v)
+ instance_variable_set(:"@#{k}", value)
+ rescue NoMethodError
+ raise Error, "unknown option: #{k}"
end
end
end
end
end