Skip to content

Commit 3526603

Browse files
committed
http/client: Add port to record structure
1 parent fcdb705 commit 3526603

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

http/client.lua

+16-12
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,17 @@ local function each_matching_record(pkt, name, type)
112112
return pkt:grep(params)
113113
end
114114

115-
local function dns_lookup(records, dns_resolver, host, query_type, filter_type, timeout)
115+
local function dns_lookup(records, dns_resolver, host, port, query_type, filter_type, timeout)
116116
local packet = dns_resolver:query(host, query_type, nil, timeout)
117117
if not packet then
118118
return
119119
end
120120
for rec in each_matching_record(packet, host, filter_type) do
121121
local t = rec:type()
122122
if t == cqueues_dns_record.AAAA then
123-
records:add_v6(rec:addr())
123+
records:add_v6(rec:addr(), port)
124124
elseif t == cqueues_dns_record.A then
125-
records:add_v4(rec:addr())
125+
records:add_v4(rec:addr(), port)
126126
end
127127
end
128128
end
@@ -144,20 +144,22 @@ function records_mt:__len()
144144
return self.n
145145
end
146146

147-
function records_methods:add_v4(addr)
147+
function records_methods:add_v4(addr, port)
148148
local n = self.n + 1
149149
self[n] = {
150150
family = cs.AF_INET;
151151
addr = addr;
152+
port = port;
152153
}
153154
self.n = n
154155
end
155156

156-
function records_methods:add_v6(addr)
157+
function records_methods:add_v6(addr, port)
157158
local n = self.n + 1
158159
self[n] = {
159160
family = cs.AF_INET6;
160161
addr = addr;
162+
port = port;
161163
}
162164
self.n = n
163165
end
@@ -206,28 +208,29 @@ local function lookup_records(options, timeout)
206208
end
207209

208210
local host = options.host
211+
local port = options.port
209212

210213
local ipv4 = IPv4address:match(host)
211214
if ipv4 then
212-
records:add_v4(host)
215+
records:add_v4(host, port)
213216
return records
214217
end
215218

216219
local ipv6 = IPv6addrz:match(host)
217220
if ipv6 then
218-
records:add_v6(host)
221+
records:add_v6(host, port)
219222
return records
220223
end
221224

222225
local dns_resolver = options.dns_resolver or cqueues_dns.getpool()
223226
if family == cs.AF_UNSPEC then
224227
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())
228+
dns_lookup(records, dns_resolver, host, port, cqueues_dns_record.AAAA, nil, timeout)
229+
dns_lookup(records, dns_resolver, host, port, cqueues_dns_record.A, nil, deadline and deadline-monotime())
227230
elseif family == cs.AF_INET then
228-
dns_lookup(records, dns_resolver, host, cqueues_dns_record.A, cqueues_dns_record.A, timeout)
231+
dns_lookup(records, dns_resolver, host, port, cqueues_dns_record.A, cqueues_dns_record.A, timeout)
229232
elseif family == cs.AF_INET6 then
230-
dns_lookup(records, dns_resolver, host, cqueues_dns_record.AAAA, cqueues_dns_record.AAAA, timeout)
233+
dns_lookup(records, dns_resolver, host, port, cqueues_dns_record.AAAA, cqueues_dns_record.AAAA, timeout)
231234
end
232235

233236
return records
@@ -260,7 +263,7 @@ local function connect(options, timeout)
260263
local connect_params = {
261264
family = nil;
262265
host = nil;
263-
port = options.port;
266+
port = nil;
264267
path = nil;
265268
bind = bind;
266269
sendname = false;
@@ -274,6 +277,7 @@ local function connect(options, timeout)
274277
local rec = records[i]
275278
connect_params.family = rec.family;
276279
connect_params.host = rec.addr;
280+
connect_params.port = rec.port;
277281
connect_params.path = rec.path;
278282
local s
279283
s, lasterr, lasterrno = ca.fileresult(cs.connect(connect_params))

0 commit comments

Comments
 (0)