Skip to content
Snippets Groups Projects
Commit 07f5791b authored by Maksim Tiushev's avatar Maksim Tiushev Committed by Alexander Turenko
Browse files

uri: uri.format encapsulate IPv6 address in []

This patch adds encapsulation for IPv6 addresses in brackets when
calling uri.format (as per RFC 2732).

Closes #9556

NO_DOC=bugfix

(cherry picked from commit a49ec23b85edb684744eb525427465dfa4f660e1)
parent 46250b46
No related branches found
No related tags found
No related merge requests found
## bugfix/lua/uri ## bugfix/lua/uri
* Fixed a bug that caused characters A-F to be unsupported in IPv6 * Fixed a bug that caused characters A-F to be unsupported in IPv6
addresses (gh-9556). addresses. Changed the `uri.format` output for IPv6 to be
encapsulated in brackets `[]` (gh-9556).
...@@ -309,7 +309,10 @@ uri_format(char *str, int len, const struct uri *uri, bool write_password) ...@@ -309,7 +309,10 @@ uri_format(char *str, int len, const struct uri *uri, bool write_password)
SNPRINT(total, snprintf, str, len, "@"); SNPRINT(total, snprintf, str, len, "@");
} }
if (uri->host != NULL) { if (uri->host != NULL) {
SNPRINT(total, snprintf, str, len, "%s", uri->host); if (uri->host_hint == 2)
SNPRINT(total, snprintf, str, len, "[%s]", uri->host);
else
SNPRINT(total, snprintf, str, len, "%s", uri->host);
} }
if (uri->service != NULL) { if (uri->service != NULL) {
if (uri->host != NULL) { if (uri->host != NULL) {
......
...@@ -231,6 +231,16 @@ local function format(uri, write_password) ...@@ -231,6 +231,16 @@ local function format(uri, write_password)
uribuf.path = uri.path uribuf.path = uri.path
uribuf.query = uri.query uribuf.query = uri.query
uribuf.fragment = uri.fragment uribuf.fragment = uri.fragment
-- The `uri_format` function needs the hint to enclose an IPv6
-- address into square brackets. So we must set the `host_hint`
-- to determine the type of `uri.host`.
if uri.ipv4 ~= nil then
uribuf.host_hint = 1
elseif uri.ipv6 ~= nil then
uribuf.host_hint = 2
else
uribuf.host_hint = 3
end
fill_uribuf_params(uribuf, uri) fill_uribuf_params(uribuf, uri)
local ibuf = cord_ibuf_take() local ibuf = cord_ibuf_take()
local str = ibuf:alloc(1024) local str = ibuf:alloc(1024)
......
...@@ -55,3 +55,15 @@ g1.test_parse_urls_with_ipv6_addresses = function(cg) ...@@ -55,3 +55,15 @@ g1.test_parse_urls_with_ipv6_addresses = function(cg)
t.assert_equals(value, res[key], msg) t.assert_equals(value, res[key], msg)
end end
end end
g1.test_format_urls_with_ipv6_addresses = function(cg)
-- This test checks the correct formatting URIs containing IPv6 addresses.
local url = join_url(cg.params)
local res = uri.parse(url)
t.assert_not_equals(res, nil, string.format('Error while parsing %q', url))
t.assert_equals(type(res), 'table',
string.format('uri.parse(%q) is not a table', url))
local u = uri.format(res)
t.assert_equals(url, u)
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment