@@ -10,6 +10,9 @@ local connection_common = require "http.connection_common"
10
10
local onerror = connection_common .onerror
11
11
local new_h1_connection = require " http.h1_connection" .new
12
12
local 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"
13
16
local openssl_ssl = require " openssl.ssl"
14
17
local openssl_ctx = require " openssl.ssl.context"
15
18
local openssl_verify_param = require " openssl.x509.verify_param"
@@ -181,48 +184,60 @@ function records_methods:remove_family(family)
181
184
end
182
185
end
183
186
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 )
185
192
local family = options .family
186
193
if family == nil then
187
194
family = cs .AF_UNSPEC
188
195
end
189
196
197
+ local records = new_records ()
198
+
190
199
local path = options .path
191
200
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
195
202
error (" cannot use .path with non-unix address family" )
196
203
end
204
+ records :add_unix (path )
205
+ return records
197
206
end
198
207
199
- local deadline = timeout and monotime ()+ timeout
200
-
201
208
local host = options .host
202
209
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
204
215
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
224
220
end
225
221
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
+
226
241
local bind = options .bind
227
242
if bind ~= nil then
228
243
assert (type (bind ) == " string" )
@@ -264,7 +279,7 @@ local function connect(options, timeout)
264
279
s , lasterr , lasterrno = ca .fileresult (cs .connect (connect_params ))
265
280
if s then
266
281
local c
267
- c , lasterr , lasterrno = negotiate (s , options , timeout )
282
+ c , lasterr , lasterrno = negotiate (s , options , deadline and deadline - monotime () )
268
283
if c then
269
284
-- Force TCP connect to occur
270
285
local ok
@@ -276,7 +291,6 @@ local function connect(options, timeout)
276
291
else
277
292
s :close ()
278
293
end
279
- timeout = deadline and deadline - monotime ()
280
294
end
281
295
if lasterrno == ce .EAFNOSUPPORT then
282
296
-- If an address family is not supported then entirely remove that
0 commit comments