pkg/types: Support Unix sockets in NewURLS

Resolves #12450
This commits adds support to unix/unixs socket URLs, which currently
fail with the message "URL address does not have the form "host:port".
It also replaces the work started in #11747.
This commit is contained in:
Karen Almog 2020-11-12 15:23:54 +01:00
parent 3f639e59e4
commit d93b7c8cb1
2 changed files with 13 additions and 11 deletions

View File

@ -36,20 +36,25 @@ func NewURLs(strs []string) (URLs, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if u.Scheme != "http" && u.Scheme != "https" && u.Scheme != "unix" && u.Scheme != "unixs" {
switch u.Scheme {
case "http", "https":
if _, _, err := net.SplitHostPort(u.Host); err != nil {
return nil, fmt.Errorf(`URL address does not have the form "host:port": %s`, in)
}
if u.Path != "" {
return nil, fmt.Errorf("URL must not contain a path: %s", in)
}
case "unix", "unixs":
break
default:
return nil, fmt.Errorf("URL scheme must be http, https, unix, or unixs: %s", in) return nil, fmt.Errorf("URL scheme must be http, https, unix, or unixs: %s", in)
} }
if _, _, err := net.SplitHostPort(u.Host); err != nil {
return nil, fmt.Errorf(`URL address does not have the form "host:port": %s`, in)
}
if u.Path != "" {
return nil, fmt.Errorf("URL must not contain a path: %s", in)
}
all[i] = *u all[i] = *u
} }
us := URLs(all) us := URLs(all)
us.Sort() us.Sort()
return us, nil return us, nil
} }

View File

@ -29,9 +29,6 @@ func TestValidateURLsValueBad(t *testing.T) {
// bad port specification // bad port specification
"127.0.0.1:foo", "127.0.0.1:foo",
"127.0.0.1:", "127.0.0.1:",
// unix sockets not supported
"unix://",
"unix://tmp/etcd.sock",
// bad strings // bad strings
"somewhere", "somewhere",
"234#$", "234#$",