commit 07f5791b90e865ea0b19f07d2d3746ddd41ded3f from: Maksim Tiushev via: Alexander Turenko date: Wed Jul 31 15:03:48 2024 UTC 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) commit - 46250b465f87ef092e595f687eec6e2095bef8d7 commit + 07f5791b90e865ea0b19f07d2d3746ddd41ded3f blob - 54fa38235e37f2f418e9bfa833befbdb60d3e68f blob + d6b4d2090a7d355ce5ceff6c55b35c5fce77a6d3 --- changelogs/unreleased/gh-9556-fix-uri-ipv6.md +++ changelogs/unreleased/gh-9556-fix-uri-ipv6.md @@ -1,4 +1,5 @@ ## bugfix/lua/uri * 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). blob - 45561aeeae08528b994f7ad6b905766a281bdae6 blob + b229db0581e63eaeeb75e885064622f29584082f --- src/lib/uri/uri.c +++ src/lib/uri/uri.c @@ -309,7 +309,10 @@ uri_format(char *str, int len, const struct uri *uri, SNPRINT(total, snprintf, str, len, "@"); } 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->host != NULL) { blob - 709a92b7822b5eb102706abc7c34f72312fb73e6 blob + c05cbf2acf2102fc870f78f77684007a2616ad83 --- src/lua/uri.lua +++ src/lua/uri.lua @@ -231,6 +231,16 @@ local function format(uri, write_password) uribuf.path = uri.path uribuf.query = uri.query 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) local ibuf = cord_ibuf_take() local str = ibuf:alloc(1024) blob - 7082f2fe67bb75b087e3d234b2b48c6f4f15f6a3 blob + 6c0394ab711813635341af9156a99fda98aaa924 --- test/app-luatest/gh_9556_uri_parse_format_ipv6_test.lua +++ test/app-luatest/gh_9556_uri_parse_format_ipv6_test.lua @@ -55,3 +55,15 @@ g1.test_parse_urls_with_ipv6_addresses = function(cg) t.assert_equals(value, res[key], msg) 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