Skip to content

Commit 1817d68

Browse files
committed
http/client: Turn records collection into a full type with metatable
1 parent f3de948 commit 1817d68

File tree

1 file changed

+67
-19
lines changed

1 file changed

+67
-19
lines changed

http/client.lua

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,62 @@ local function dns_lookup(records, dns_resolver, host, query_type, filter_type,
117117
for rec in each_matching_record(packet, host, filter_type) do
118118
local t = rec:type()
119119
if t == cqueues_dns_record.AAAA then
120-
table.insert(records, { family = cs.AF_INET6, host = rec:addr() })
120+
records:add_v6(rec:addr())
121121
elseif t == cqueues_dns_record.A then
122-
table.insert(records, { family = cs.AF_INET, host = rec:addr() })
122+
records:add_v4(rec:addr())
123+
end
124+
end
125+
end
126+
127+
local records_methods = {}
128+
local records_mt = {
129+
__name = "http.client.records";
130+
__index = records_methods;
131+
}
132+
133+
local function new_records()
134+
return setmetatable({
135+
n = 0;
136+
nil -- preallocate space for one
137+
}, records_mt)
138+
end
139+
140+
function records_mt:__len()
141+
return self.n
142+
end
143+
144+
function records_methods:add_v4(addr)
145+
local n = self.n + 1
146+
self[n] = {
147+
family = cs.AF_INET;
148+
addr = addr;
149+
}
150+
self.n = n
151+
end
152+
153+
function records_methods:add_v6(addr)
154+
local n = self.n + 1
155+
self[n] = {
156+
family = cs.AF_INET6;
157+
addr = addr;
158+
}
159+
self.n = n
160+
end
161+
162+
function records_methods:add_unix(path)
163+
local n = self.n + 1
164+
self[n] = {
165+
family = cs.AF_UNIX;
166+
path = path;
167+
}
168+
self.n = n
169+
end
170+
171+
function records_methods:remove_family(family)
172+
for j=self.n, 1, -1 do
173+
if self[j].family == family then
174+
table.remove(self, j)
175+
self.n = self.n - 1
123176
end
124177
end
125178
end
@@ -142,15 +195,19 @@ local function connect(options, timeout)
142195
local deadline = timeout and monotime()+timeout
143196

144197
local host = options.host
145-
local records
198+
199+
local records = new_records()
200+
146201
if path then
147-
records = { { family = family, path = path } }
202+
records:add_unix(path)
148203
elseif http_util.is_ip(host) then
149-
family = host:find(":", 1, true) and cs.AF_INET6 or cs.AF_INET
150-
records = { { family = family, host = host } }
204+
if host:find(":", 1, true) then
205+
records:add_v6(host)
206+
else
207+
records:add_v4(host)
208+
end
151209
else
152210
local dns_resolver = options.dns_resolver or cqueues_dns.getpool()
153-
records = {}
154211
if family == cs.AF_UNSPEC then
155212
dns_lookup(records, dns_resolver, host, cqueues_dns_record.AAAA, nil, timeout)
156213
dns_lookup(records, dns_resolver, host, cqueues_dns_record.A, nil, deadline and deadline-monotime())
@@ -194,11 +251,10 @@ local function connect(options, timeout)
194251

195252
local lasterr, lasterrno = "The name does not resolve for the supplied parameters"
196253
local i = 1
197-
local n = #records
198-
while i <= n do
254+
while i <= records.n do
199255
local rec = records[i]
200256
connect_params.family = rec.family;
201-
connect_params.host = rec.host;
257+
connect_params.host = rec.addr;
202258
connect_params.path = rec.path;
203259
local s
204260
s, lasterr, lasterrno = ca.fileresult(cs.connect(connect_params))
@@ -221,15 +277,7 @@ local function connect(options, timeout)
221277
if lasterrno == ce.EAFNOSUPPORT then
222278
-- If an address family is not supported then entirely remove that
223279
-- family from candidate records
224-
local af = connect_params.family
225-
for j=n, i+1, -1 do
226-
if records[j].family == af then
227-
table.remove(records, j)
228-
n = n - 1
229-
end
230-
end
231-
table.remove(records, i)
232-
n = n - 1
280+
records:remove_family(connect_params.family)
233281
else
234282
i = i + 1
235283
end

0 commit comments

Comments
 (0)