Skip to content

Commit 5a80e99

Browse files
committed
http/client: Normalise ipv6 addresses
1 parent 8168ad5 commit 5a80e99

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
@@ -192,10 +203,6 @@ function records_methods:remove_family(family)
192203
end
193204
end
194205

195-
local EOF = lpeg.P(-1)
196-
local IPv4address = IPv4_patts.IPv4address * EOF
197-
local IPv6addrz = IPv6_patts.IPv6addrz * EOF
198-
199206
local function lookup_records(options, timeout)
200207
local family = options.family
201208
if family == nil then
@@ -224,7 +231,7 @@ local function lookup_records(options, timeout)
224231

225232
local ipv6 = IPv6addrz:match(host)
226233
if ipv6 then
227-
records:add_v6(host, port)
234+
records:add_v6(ipv6, port)
228235
return records
229236
end
230237

0 commit comments

Comments
 (0)