From 1356037fc61ef5c4a3a0f8fd47609a2b7c56e74a Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Wed, 1 Oct 2014 11:38:04 -0700 Subject: [PATCH] flags/urls: reject url without port For now, if etcd receives a url without port, it will listen on a random port, which is useless. --- pkg/flags/urls.go | 4 ++++ pkg/flags/urls_test.go | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/flags/urls.go b/pkg/flags/urls.go index 34808d862..455e4238b 100644 --- a/pkg/flags/urls.go +++ b/pkg/flags/urls.go @@ -3,6 +3,7 @@ package flags import ( "errors" "fmt" + "net" "net/url" "strings" ) @@ -28,6 +29,9 @@ func (us *URLs) Set(s string) error { if u.Scheme != "http" && u.Scheme != "https" { return fmt.Errorf("URL scheme must be http or https: %s", s) } + if _, _, err := net.SplitHostPort(u.Host); err != nil { + return fmt.Errorf(`URL address does not have the form "host:port": %s`, s) + } if u.Path != "" { return fmt.Errorf("URL must not contain a path: %s", s) } diff --git a/pkg/flags/urls_test.go b/pkg/flags/urls_test.go index 49aa3e47d..caab40c0e 100644 --- a/pkg/flags/urls_test.go +++ b/pkg/flags/urls_test.go @@ -21,6 +21,7 @@ func TestValidateURLsBad(t *testing.T) { "234#$", "file://foo/bar", "http://hello/asdf", + "http://10.1.1.1", } for i, in := range tests { u := URLs{} @@ -34,7 +35,8 @@ func TestValidateURLsGood(t *testing.T) { tests := []string{ "https://1.2.3.4:8080", "http://10.1.1.1:80", - "http://10.1.1.1", + "http://localhost:80", + "http://:80", } for i, in := range tests { u := URLs{}