@@ -10,6 +10,9 @@ local connection_common = require "http.connection_common"
1010local onerror = connection_common .onerror
1111local new_h1_connection = require " http.h1_connection" .new
1212local new_h2_connection = require " http.h2_connection" .new
13+ local lpeg = require " lpeg"
14+ local IPv4_patts = require " lpeg_patterns.IPv4"
15+ local IPv6_patts = require " lpeg_patterns.IPv6"
1316local openssl_ssl = require " openssl.ssl"
1417local openssl_ctx = require " openssl.ssl.context"
1518local openssl_verify_param = require " openssl.x509.verify_param"
@@ -181,48 +184,60 @@ function records_methods:remove_family(family)
181184 end
182185end
183186
184- local function connect (options , timeout )
187+ local EOF = lpeg .P (- 1 )
188+ local IPv4address = IPv4_patts .IPv4address * EOF
189+ local IPv6addrz = IPv6_patts .IPv6addrz * EOF
190+
191+ local function lookup_records (options , timeout )
185192 local family = options .family
186193 if family == nil then
187194 family = cs .AF_UNSPEC
188195 end
189196
197+ local records = new_records ()
198+
190199 local path = options .path
191200 if path then
192- if family == cs .AF_UNSPEC then
193- family = cs .AF_UNIX
194- elseif family ~= cs .AF_UNIX then
201+ if family ~= cs .AF_UNSPEC and family ~= cs .AF_UNIX then
195202 error (" cannot use .path with non-unix address family" )
196203 end
204+ records :add_unix (path )
205+ return records
197206 end
198207
199- local deadline = timeout and monotime ()+ timeout
200-
201208 local host = options .host
202209
203- local records = new_records ()
210+ local ipv4 = IPv4address :match (host )
211+ if ipv4 then
212+ records :add_v4 (host )
213+ return records
214+ end
204215
205- if path then
206- records :add_unix (path )
207- elseif http_util .is_ip (host ) then
208- if host :find (" :" , 1 , true ) then
209- records :add_v6 (host )
210- else
211- records :add_v4 (host )
212- end
213- else
214- local dns_resolver = options .dns_resolver or cqueues_dns .getpool ()
215- if family == cs .AF_UNSPEC then
216- dns_lookup (records , dns_resolver , host , cqueues_dns_record .AAAA , nil , timeout )
217- dns_lookup (records , dns_resolver , host , cqueues_dns_record .A , nil , deadline and deadline - monotime ())
218- elseif family == cs .AF_INET then
219- dns_lookup (records , dns_resolver , host , cqueues_dns_record .A , cqueues_dns_record .A , timeout )
220- elseif family == cs .AF_INET6 then
221- dns_lookup (records , dns_resolver , host , cqueues_dns_record .AAAA , cqueues_dns_record .AAAA , timeout )
222- end
223- timeout = deadline and deadline - monotime ()
216+ local ipv6 = IPv6addrz :match (host )
217+ if ipv6 then
218+ records :add_v6 (host )
219+ return records
224220 end
225221
222+ local dns_resolver = options .dns_resolver or cqueues_dns .getpool ()
223+ if family == cs .AF_UNSPEC then
224+ local deadline = timeout and monotime ()+ timeout
225+ dns_lookup (records , dns_resolver , host , cqueues_dns_record .AAAA , nil , timeout )
226+ dns_lookup (records , dns_resolver , host , cqueues_dns_record .A , nil , deadline and deadline - monotime ())
227+ elseif family == cs .AF_INET then
228+ dns_lookup (records , dns_resolver , host , cqueues_dns_record .A , cqueues_dns_record .A , timeout )
229+ elseif family == cs .AF_INET6 then
230+ dns_lookup (records , dns_resolver , host , cqueues_dns_record .AAAA , cqueues_dns_record .AAAA , timeout )
231+ end
232+
233+ return records
234+ end
235+
236+ local function connect (options , timeout )
237+ local deadline = timeout and monotime ()+ timeout
238+
239+ local records = lookup_records (options , timeout )
240+
226241 local bind = options .bind
227242 if bind ~= nil then
228243 assert (type (bind ) == " string" )
@@ -264,7 +279,7 @@ local function connect(options, timeout)
264279 s , lasterr , lasterrno = ca .fileresult (cs .connect (connect_params ))
265280 if s then
266281 local c
267- c , lasterr , lasterrno = negotiate (s , options , timeout )
282+ c , lasterr , lasterrno = negotiate (s , options , deadline and deadline - monotime () )
268283 if c then
269284 -- Force TCP connect to occur
270285 local ok
@@ -276,7 +291,6 @@ local function connect(options, timeout)
276291 else
277292 s :close ()
278293 end
279- timeout = deadline and deadline - monotime ()
280294 end
281295 if lasterrno == ce .EAFNOSUPPORT then
282296 -- If an address family is not supported then entirely remove that
0 commit comments