@@ -98,29 +98,57 @@ def parse_hosts(io)
9898 end
9999 end
100100
101+ DEFAULT_TIMEOUT = 5.0
102+
101103 # Parse the `resolv.conf` file and return a list of nameservers.
102104 def self . parse_resolv_configuration ( path )
103105 nameservers = [ ]
106+ search = nil
107+ ndots = 1
108+ edns = nil
109+ timeout = DEFAULT_TIMEOUT
110+
104111 File . open ( path ) do |file |
105112 file . each do |line |
106113 # Remove any comments:
107114 line . sub! ( /[#;].*/ , '' )
108115
109116 # Extract resolv.conf command:
110- keyword , *args = line . split ( /\s +/ )
117+ keyword , *arguments = line . split ( /\s +/ )
111118
112119 case keyword
113120 when 'nameserver'
114- nameservers += args
121+ nameservers . concat ( arguments )
122+ when 'domain' , 'search'
123+ search = arguments
124+ when 'options'
125+ arguments . each do |argument |
126+ key , value = argument . split ( ':' , 2 )
127+
128+ case key
129+ when 'ndots'
130+ ndots = value . to_i
131+ when 'edns0'
132+ edns = 0
133+ when 'timeout'
134+ timeout = value . to_f
135+ end
136+ end
115137 end
116138 end
117139 end
118140
119- return nameservers
141+ return {
142+ nameservers : nameservers ,
143+ search : search ,
144+ ndots : ndots ,
145+ edns : edns ,
146+ timeout : timeout ,
147+ }
120148 end
121149
122150 # Get a list of standard nameserver connections which can be used for querying any standard servers that the system has been configured with.
123- def self . standard_connections ( nameservers , **options )
151+ def self . endpoint_for ( nameservers , **options )
124152 connections = [ ]
125153
126154 nameservers . each do |host |
@@ -132,21 +160,33 @@ def self.standard_connections(nameservers, **options)
132160 end
133161
134162 # Get a list of standard nameserver connections which can be used for querying any standard servers that the system has been configured with. There is no equivalent facility to use the `hosts` file at present.
135- def self . nameservers ( **options )
163+ def self . resolver ( **options )
136164 nameservers = [ ]
137165
138166 if File . exist? RESOLV_CONF
139- nameservers = parse_resolv_configuration ( RESOLV_CONF )
167+ options . update ( parse_resolv_configuration ( RESOLV_CONF ) )
168+ nameservers = options . delete ( :nameservers )
140169 elsif defined? ( Win32 ::Resolv ) and RUBY_PLATFORM =~ /mswin32|cygwin|mingw|bccwin/
141170 search , nameservers = Win32 ::Resolv . get_resolv_info
171+ options . update ( search : search )
142172 end
143173
144- return standard_connections ( nameservers , **options )
145- end
146-
147- # Get a list of default nameservers.
148- def self . default_nameservers
149- self . nameservers ( timeout : 5.0 )
174+ if search = options [ :search ]
175+ unless search . include? ( '.' )
176+ search << nil
177+ end
178+ else
179+ options [ :search ] = [ nil ]
180+ end
181+
182+ timeout = options . delete ( :timeout ) || DEFAULT_TIMEOUT
183+ endpoint = self . endpoint_for ( nameservers , timeout : timeout )
184+
185+ if block_given?
186+ yield endpoint , **options
187+ else
188+ return Resolver . new ( endpoint , **options )
189+ end
150190 end
151191 end
152192end
0 commit comments