From 67074a6a99965bdea5db21859423b2ac6fda4437 Mon Sep 17 00:00:00 2001 From: tzssangglass Date: Wed, 28 Oct 2026 21:10:25 +0800 Subject: [PATCH] fix(http): accept a bare ipv6 host joined with a port A caller that builds "scheme://" .. host .. ":" .. port with a bare IPv6 host produces "http://::1:8080", which no URI grammar can parse: the colons are ambiguous. Tolerate it in parse_uri by splitting at the last colon only when the part before it already looks like an IPv6 address (two or more colons), then bracket that part. Names, IPv4 addresses and hosts that already carry brackets are left untouched, and a portless bare literal is still rejected rather than guessed. --- lib/resty/http.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/resty/http.lua b/lib/resty/http.lua index 0bac957..75339c4 100644 --- a/lib/resty/http.lua +++ b/lib/resty/http.lua @@ -10,6 +10,8 @@ local str_lower = string.lower local str_upper = string.upper local str_find = string.find local str_sub = string.sub +local str_gsub = string.gsub +local str_match = string.match local tbl_concat = table.concat local tbl_insert = table.insert local ngx_encode_args = ngx.encode_args @@ -271,6 +273,21 @@ end function _M.parse_uri(_, uri, query_in_path) if query_in_path == nil then query_in_path = true end + do + local prefix, authority, suffix = str_match(uri, "^(.-//)([^/?#]*)(.*)$") + if authority and str_sub(authority, 1, 1) ~= "[" then + local last = str_find(authority, ":[^:]*$") + if last then + local host = str_sub(authority, 1, last - 1) + local port = str_sub(authority, last + 1) + local _, colons = str_gsub(host, ":", "") + if colons > 1 and str_find(port, "^%d+$") and str_find(host, "^[%x:.]+$") then + uri = prefix .. "[" .. host .. "]:" .. port .. suffix + end + end + end + end + local m, err = ngx_re_match( uri, [[^(?:(http[s]?):)?//((?:[^\[\]:/\?]+)|(?:\[.+\]))(?::(\d+))?([^\?]*)\??(.*)]],