fix: IPv6 addr in square brackets
This commit is contained in:
parent
b33c8cec0b
commit
77c8d60092
2 changed files with 46 additions and 1 deletions
|
@ -49,11 +49,16 @@ func (r *RequestDecorator) ServeHTTP(rw http.ResponseWriter, req *http.Request,
|
||||||
|
|
||||||
func parseHost(addr string) string {
|
func parseHost(addr string) string {
|
||||||
if !strings.Contains(addr, ":") {
|
if !strings.Contains(addr, ":") {
|
||||||
|
// IPv4 without port or empty address
|
||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IPv4 with port or IPv6
|
||||||
host, _, err := net.SplitHostPort(addr)
|
host, _, err := net.SplitHostPort(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if addr[0] == '[' && addr[len(addr)-1] == ']' {
|
||||||
|
return addr[1 : len(addr)-1]
|
||||||
|
}
|
||||||
return addr
|
return addr
|
||||||
}
|
}
|
||||||
return host
|
return host
|
||||||
|
|
|
@ -104,7 +104,7 @@ func TestRequestFlattening(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRequestHostParseHost(t *testing.T) {
|
func Test_parseHost(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
host string
|
host string
|
||||||
|
@ -130,6 +130,46 @@ func TestRequestHostParseHost(t *testing.T) {
|
||||||
host: "127.0.0.1:",
|
host: "127.0.0.1:",
|
||||||
expected: "127.0.0.1",
|
expected: "127.0.0.1",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
desc: "host with : and without port",
|
||||||
|
host: "fe80::215:5dff:fe20:cd6a",
|
||||||
|
expected: "fe80::215:5dff:fe20:cd6a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "IPv6 host with : and with port",
|
||||||
|
host: "[fe80::215:5dff:fe20:cd6a]:123",
|
||||||
|
expected: "fe80::215:5dff:fe20:cd6a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "IPv6 host with : and without port",
|
||||||
|
host: "[fe80::215:5dff:fe20:cd6a]:",
|
||||||
|
expected: "fe80::215:5dff:fe20:cd6a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "IPv6 host without : and without port",
|
||||||
|
host: "[fe80::215:5dff:fe20:cd6a]",
|
||||||
|
expected: "fe80::215:5dff:fe20:cd6a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "invalid IPv6: missing [",
|
||||||
|
host: "fe80::215:5dff:fe20:cd6a]",
|
||||||
|
expected: "fe80::215:5dff:fe20:cd6a]",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "invalid IPv6: missing ]",
|
||||||
|
host: "[fe80::215:5dff:fe20:cd6a",
|
||||||
|
expected: "[fe80::215:5dff:fe20:cd6a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "empty address",
|
||||||
|
host: "",
|
||||||
|
expected: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "only :",
|
||||||
|
host: ":",
|
||||||
|
expected: "",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
|
|
Loading…
Reference in a new issue