Skip to content

Commit 1217fa5

Browse files
committed
http/client: Normalise ipv6 addresses
1 parent 0f61ed3 commit 1217fa5

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

http/client.lua

+12-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ 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 EOF = lpeg.P(-1)
21+
local IPv4address = IPv4_patts.IPv4address * EOF
22+
local IPv6addrz = IPv6_patts.IPv6addrz * EOF
23+
2024
-- Create a shared 'default' TLS context
2125
local default_ctx = http_tls.new_client_context()
2226

@@ -165,6 +169,13 @@ local record_ipv6_mt = {
165169
__index = record_ipv6_methods;
166170
}
167171
function records_methods:add_v6(addr, port)
172+
if type(addr) == "string" then
173+
-- Normalise
174+
addr = assert(IPv6addrz:match(addr))
175+
elseif getmetatable(addr) ~= IPv6_patts.IPv6_mt then
176+
error("invalid argument")
177+
end
178+
addr = tostring(addr)
168179
local n = self.n + 1
169180
self[n] = setmetatable({ addr = addr, port = port }, record_ipv6_mt)
170181
self.n = n
@@ -196,10 +207,6 @@ function records_methods:remove_family(family)
196207
end
197208
end
198209

199-
local EOF = lpeg.P(-1)
200-
local IPv4address = IPv4_patts.IPv4address * EOF
201-
local IPv6addrz = IPv6_patts.IPv6addrz * EOF
202-
203210
local function lookup_records(options, timeout)
204211
local family = options.family
205212
if family == nil then
@@ -228,7 +235,7 @@ local function lookup_records(options, timeout)
228235

229236
local ipv6 = IPv6addrz:match(host)
230237
if ipv6 then
231-
records:add_v6(host, port)
238+
records:add_v6(ipv6, port)
232239
return records
233240
end
234241

0 commit comments

Comments
 (0)