Skip to content

Commit b8dd999

Browse files
committed
http/client: localise DNS constants
1 parent 1217fa5 commit b8dd999

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

http/client.lua

+26-16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ local openssl_ssl = require "openssl.ssl"
1717
local openssl_ctx = require "openssl.ssl.context"
1818
local openssl_verify_param = require "openssl.x509.verify_param"
1919

20+
local AF_UNSPEC = cs.AF_UNSPEC
21+
local AF_UNIX = cs.AF_UNIX
22+
local AF_INET = cs.AF_INET
23+
local AF_INET6 = cs.AF_INET6
24+
25+
local DNS_CLASS_IN = cqueues_dns_record.IN
26+
local DNS_TYPE_A = cqueues_dns_record.A
27+
local DNS_TYPE_AAAA = cqueues_dns_record.AAAA
28+
local DNS_TYPE_CNAME = cqueues_dns_record.CNAME
29+
2030
local EOF = lpeg.P(-1)
2131
local IPv4address = IPv4_patts.IPv4address * EOF
2232
local IPv6addrz = IPv6_patts.IPv6addrz * EOF
@@ -98,8 +108,8 @@ local function each_matching_record(pkt, name, type)
98108
-- First need to do CNAME chasing
99109
local params = {
100110
section = "answer";
101-
class = cqueues_dns_record.IN;
102-
type = cqueues_dns_record.CNAME;
111+
class = DNS_CLASS_IN;
112+
type = DNS_TYPE_CNAME;
103113
name = name .. ".";
104114
}
105115
for _=1, 8 do -- avoid cname loops
@@ -123,9 +133,9 @@ local function dns_lookup(records, dns_resolver, host, port, query_type, filter_
123133
end
124134
for rec in each_matching_record(packet, host, filter_type) do
125135
local t = rec:type()
126-
if t == cqueues_dns_record.AAAA then
136+
if t == DNS_TYPE_AAAA then
127137
records:add_v6(rec:addr(), port)
128-
elseif t == cqueues_dns_record.A then
138+
elseif t == DNS_TYPE_A then
129139
records:add_v4(rec:addr(), port)
130140
end
131141
end
@@ -149,7 +159,7 @@ function records_mt:__len()
149159
end
150160

151161
local record_ipv4_methods = {
152-
family = cs.AF_INET;
162+
family = AF_INET;
153163
}
154164
local record_ipv4_mt = {
155165
__name = "http.client.record.ipv4";
@@ -162,7 +172,7 @@ function records_methods:add_v4(addr, port)
162172
end
163173

164174
local record_ipv6_methods = {
165-
family = cs.AF_INET6;
175+
family = AF_INET6;
166176
}
167177
local record_ipv6_mt = {
168178
__name = "http.client.record.ipv6";
@@ -182,7 +192,7 @@ function records_methods:add_v6(addr, port)
182192
end
183193

184194
local record_unix_methods = {
185-
family = cs.AF_UNIX;
195+
family = AF_UNIX;
186196
}
187197
local record_unix_mt = {
188198
__name = "http.client.record.unix";
@@ -210,14 +220,14 @@ end
210220
local function lookup_records(options, timeout)
211221
local family = options.family
212222
if family == nil then
213-
family = cs.AF_UNSPEC
223+
family = AF_UNSPEC
214224
end
215225

216226
local records = new_records()
217227

218228
local path = options.path
219229
if path then
220-
if family ~= cs.AF_UNSPEC and family ~= cs.AF_UNIX then
230+
if family ~= AF_UNSPEC and family ~= AF_UNIX then
221231
error("cannot use .path with non-unix address family")
222232
end
223233
records:add_unix(path)
@@ -240,14 +250,14 @@ local function lookup_records(options, timeout)
240250
end
241251

242252
local dns_resolver = options.dns_resolver or cqueues_dns.getpool()
243-
if family == cs.AF_UNSPEC then
253+
if family == AF_UNSPEC then
244254
local deadline = timeout and monotime()+timeout
245-
dns_lookup(records, dns_resolver, host, port, cqueues_dns_record.AAAA, nil, timeout)
246-
dns_lookup(records, dns_resolver, host, port, cqueues_dns_record.A, nil, deadline and deadline-monotime())
247-
elseif family == cs.AF_INET then
248-
dns_lookup(records, dns_resolver, host, port, cqueues_dns_record.A, cqueues_dns_record.A, timeout)
249-
elseif family == cs.AF_INET6 then
250-
dns_lookup(records, dns_resolver, host, port, cqueues_dns_record.AAAA, cqueues_dns_record.AAAA, timeout)
255+
dns_lookup(records, dns_resolver, host, port, DNS_TYPE_AAAA, nil, timeout)
256+
dns_lookup(records, dns_resolver, host, port, DNS_TYPE_A, nil, deadline and deadline-monotime())
257+
elseif family == AF_INET then
258+
dns_lookup(records, dns_resolver, host, port, DNS_TYPE_A, DNS_TYPE_A, timeout)
259+
elseif family == AF_INET6 then
260+
dns_lookup(records, dns_resolver, host, port, DNS_TYPE_AAAA, DNS_TYPE_AAAA, timeout)
251261
end
252262

253263
return records

0 commit comments

Comments
 (0)