From 9111617f3252526b26a45af9c5193c47b8e2bc7a Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Sat, 10 Aug 2013 19:22:04 -0700 Subject: [PATCH] chore(etcd): introduce sanitizeURL checkURL was a little weird and allowed two different ways to specify flags. Introduce sanitizeURL which will make sure the host passed in is simply hostname:port and then appends a Scheme. --- etcd.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/etcd.go b/etcd.go index 7bfb6c957..1cf9ca41d 100644 --- a/etcd.go +++ b/etcd.go @@ -142,18 +142,27 @@ var info *Info // //------------------------------------------------------------------------------ -// Check a URL and clean it up if the user forgot the schema -func checkURL(u string, defaultSchema string) string { - p, err := url.Parse(u) +// sanitizeURL will cleanup a host string in the format hostname:port and +// attach a schema. +func sanitizeURL(host string, defaultScheme string) string { + // Blank URLs are fine input, just return it + if len(host) == 0 { + return host + } + p, err := url.Parse(host) if err != nil { - panic(err) + fatal(err) } - if len(p.Host) == 0 && len(defaultSchema) != 0 { - return checkURL(fmt.Sprintf("%s://%s", defaultSchema, u), "") + // Make sure the host is in Host:Port format + _, _, err = net.SplitHostPort(host) + if err != nil { + fatal(err) } + p = &url.URL{Host: host, Scheme: defaultScheme} + return p.String() } @@ -226,8 +235,9 @@ func main() { fatal("ERROR: server name required. e.g. '-n=server_name'") } - argInfo.RaftURL = checkURL(argInfo.RaftURL, raftDefaultScheme) - argInfo.EtcdURL = checkURL(argInfo.EtcdURL, etcdDefaultScheme) + argInfo.RaftURL = sanitizeURL(argInfo.RaftURL, raftTlsConfig.Scheme) + argInfo.EtcdURL = sanitizeURL(argInfo.EtcdURL, etcdTlsConfig.Scheme) + argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http") // Setup commands. registerCommands()