Fix ipv6 parsing in client
Old implementation of function ParseConnectionString would parse ipv6 incorrectly Added a unit test for thispull/4632/head
parent
437f80af8d
commit
9051451e00
|
@ -38,22 +38,21 @@ func ParseConnectionString(path string, ssl bool) (url.URL, error) {
|
||||||
var host string
|
var host string
|
||||||
var port int
|
var port int
|
||||||
|
|
||||||
if strings.Contains(path, ":") {
|
h, p, err := net.SplitHostPort(path)
|
||||||
h := strings.Split(path, ":")
|
if err != nil {
|
||||||
i, e := strconv.Atoi(h[1])
|
if path == "" {
|
||||||
if e != nil {
|
|
||||||
return url.URL{}, fmt.Errorf("invalid port number %q: %s\n", path, e)
|
|
||||||
}
|
|
||||||
port = i
|
|
||||||
if h[0] == "" {
|
|
||||||
host = DefaultHost
|
host = DefaultHost
|
||||||
} else {
|
|
||||||
host = h[0]
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
host = path
|
host = path
|
||||||
|
}
|
||||||
// If they didn't specify a port, always use the default port
|
// If they didn't specify a port, always use the default port
|
||||||
port = DefaultPort
|
port = DefaultPort
|
||||||
|
} else {
|
||||||
|
host = h
|
||||||
|
port, err = strconv.Atoi(p)
|
||||||
|
if err != nil {
|
||||||
|
return url.URL{}, fmt.Errorf("invalid port number %q: %s\n", path, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
u := url.URL{
|
u := url.URL{
|
||||||
|
@ -62,6 +61,7 @@ func ParseConnectionString(path string, ssl bool) (url.URL, error) {
|
||||||
if ssl {
|
if ssl {
|
||||||
u.Scheme = "https"
|
u.Scheme = "https"
|
||||||
}
|
}
|
||||||
|
|
||||||
u.Host = net.JoinHostPort(host, strconv.Itoa(port))
|
u.Host = net.JoinHostPort(host, strconv.Itoa(port))
|
||||||
|
|
||||||
return u, nil
|
return u, nil
|
||||||
|
|
|
@ -547,3 +547,14 @@ func TestClient_NoTimeout(t *testing.T) {
|
||||||
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClient_ParseConnectionString_IPv6(t *testing.T) {
|
||||||
|
path := "[fdf5:9ede:1875:0:a9ee:a600:8fe3:d495]:8086"
|
||||||
|
u, err := client.ParseConnectionString(path, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error, expected %v, actual %v", nil, err)
|
||||||
|
}
|
||||||
|
if u.Host != path {
|
||||||
|
t.Fatalf("ipv6 parse failed, expected %s, actual %s", path, u.Host)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue