diff options
author | 2019-10-19 03:08:14 -0400 | |
---|---|---|
committer | 2019-10-19 08:08:14 +0100 | |
commit | 6f375cbbda9cd8572d8f7b271138045282ede7b5 (patch) | |
tree | a454fdbdd5d6aaf901c82e76a79b04dcee1a69d7 | |
parent | 5f114d38cab74f0c5c99b03ffe99f5607e393a92 (diff) | |
download | coredns-6f375cbbda9cd8572d8f7b271138045282ede7b5.tar.gz coredns-6f375cbbda9cd8572d8f7b271138045282ede7b5.tar.zst coredns-6f375cbbda9cd8572d8f7b271138045282ede7b5.zip |
add MustNormalize (#3385)
Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
-rw-r--r-- | plugin/normalize.go | 17 | ||||
-rw-r--r-- | plugin/normalize_test.go | 11 |
2 files changed, 26 insertions, 2 deletions
diff --git a/plugin/normalize.go b/plugin/normalize.go index 1289207fd..dea7d6723 100644 --- a/plugin/normalize.go +++ b/plugin/normalize.go @@ -61,13 +61,26 @@ type ( // Normalize will return the host portion of host, stripping // of any port or transport. The host will also be fully qualified and lowercased. +// An empty string is returned on failure func (h Host) Normalize() string { + // The error can be ignored here, because this function should only be called after the corefile has already been vetted. + host, _ := h.MustNormalize() + return host +} + +// MustNormalize will return the host portion of host, stripping +// of any port or transport. The host will also be fully qualified and lowercased. +// An error is returned on error +func (h Host) MustNormalize() (string, error) { s := string(h) _, s = parse.Transport(s) // The error can be ignored here, because this function is called after the corefile has already been vetted. - host, _, _, _ := SplitHostPort(s) - return Name(host).Normalize() + host, _, _, err := SplitHostPort(s) + if err != nil { + return "", err + } + return Name(host).Normalize(), nil } // SplitHostPort splits s up in a host and port portion, taking reverse address notation into account. diff --git a/plugin/normalize_test.go b/plugin/normalize_test.go index 315aaf5d9..2a82271ba 100644 --- a/plugin/normalize_test.go +++ b/plugin/normalize_test.go @@ -83,6 +83,17 @@ func TestHostNormalize(t *testing.T) { } } +func TestHostMustNormalizeFail(t *testing.T) { + hosts := []string{"..:53", "::", ""} + for i := 0; i < len(hosts); i++ { + ts := hosts[i] + h, err := Host(ts).MustNormalize() + if err == nil { + t.Errorf("Expected error, got %v", h) + } + } +} + func TestSplitHostPortReverse(t *testing.T) { tests := map[string]int{ "example.org.": 0, |