Skip to content

Commit 835d746

Browse files
committed
http/client: Move lookup into its own function
1 parent 1817d68 commit 835d746

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

http/client.lua

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ local connection_common = require "http.connection_common"
1010
local onerror = connection_common.onerror
1111
local new_h1_connection = require "http.h1_connection".new
1212
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"
1316
local openssl_ssl = require "openssl.ssl"
1417
local openssl_ctx = require "openssl.ssl.context"
1518
local openssl_verify_param = require "openssl.x509.verify_param"
@@ -177,47 +180,58 @@ function records_methods:remove_family(family)
177180
end
178181
end
179182

180-
local function connect(options, timeout)
183+
local EOF = lpeg.P(-1)
184+
local IPv4address = IPv4_patts.IPv4address * EOF
185+
local IPv6addrz = IPv6_patts.IPv6addrz * EOF
186+
187+
local function lookup_records(options, timeout)
181188
local family = options.family
182189
if family == nil then
183190
family = cs.AF_UNSPEC
184191
end
185192

193+
local records = new_records()
194+
186195
local path = options.path
187196
if path then
188-
if family == cs.AF_UNSPEC then
189-
family = cs.AF_UNIX
190-
elseif family ~= cs.AF_UNIX then
197+
if family ~= cs.AF_UNSPEC and family ~= cs.AF_UNIX then
191198
error("cannot use .path with non-unix address family")
192199
end
200+
records:add_unix(path)
201+
return records
193202
end
194203

195-
local deadline = timeout and monotime()+timeout
196-
197204
local host = options.host
198205

199-
local records = new_records()
206+
local ipv4 = IPv4address:match(host)
207+
if ipv4 then
208+
records:add_v4(host)
209+
return records
210+
end
200211

201-
if path then
202-
records:add_unix(path)
203-
elseif http_util.is_ip(host) then
204-
if host:find(":", 1, true) then
205-
records:add_v6(host)
206-
else
207-
records:add_v4(host)
208-
end
209-
else
210-
local dns_resolver = options.dns_resolver or cqueues_dns.getpool()
211-
if family == cs.AF_UNSPEC then
212-
dns_lookup(records, dns_resolver, host, cqueues_dns_record.AAAA, nil, timeout)
213-
dns_lookup(records, dns_resolver, host, cqueues_dns_record.A, nil, deadline and deadline-monotime())
214-
elseif family == cs.AF_INET then
215-
dns_lookup(records, dns_resolver, host, cqueues_dns_record.A, cqueues_dns_record.A, timeout)
216-
elseif family == cs.AF_INET6 then
217-
dns_lookup(records, dns_resolver, host, cqueues_dns_record.AAAA, cqueues_dns_record.AAAA, timeout)
218-
end
219-
timeout = deadline and deadline-monotime()
212+
local ipv6 = IPv6addrz:match(host)
213+
if ipv6 then
214+
records:add_v6(host)
215+
return records
216+
end
217+
218+
local dns_resolver = options.dns_resolver or cqueues_dns.getpool()
219+
if family == cs.AF_UNSPEC then
220+
local deadline = timeout and monotime()+timeout
221+
dns_lookup(records, dns_resolver, host, cqueues_dns_record.AAAA, nil, timeout)
222+
dns_lookup(records, dns_resolver, host, cqueues_dns_record.A, nil, deadline and deadline-monotime())
223+
elseif family == cs.AF_INET then
224+
dns_lookup(records, dns_resolver, host, cqueues_dns_record.A, cqueues_dns_record.A, timeout)
225+
elseif family == cs.AF_INET6 then
226+
dns_lookup(records, dns_resolver, host, cqueues_dns_record.AAAA, cqueues_dns_record.AAAA, timeout)
220227
end
228+
return records
229+
end
230+
231+
local function connect(options, timeout)
232+
local deadline = timeout and monotime()+timeout
233+
234+
local records = lookup_records(options, timeout)
221235

222236
local bind = options.bind
223237
if bind ~= nil then

0 commit comments

Comments
 (0)