From adc0f9673b25e5c1c18ff38a07826c0cfa78ef36 Mon Sep 17 00:00:00 2001 From: karupanerura Date: Sun, 7 Jul 2019 15:15:56 +0900 Subject: [PATCH] no path url pattern should be invalid --- host_path_matcher.go | 7 +++++-- host_path_matcher_test.go | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/host_path_matcher.go b/host_path_matcher.go index ae371b0..b404849 100644 --- a/host_path_matcher.go +++ b/host_path_matcher.go @@ -21,9 +21,12 @@ func (m *genericHostPathMatcher) MatchHostPath(host, path string) bool { // CompileHostPathMatcher is constructor for HostPathMatcher func CompileHostPathMatcher(pattern string) (HostPathMatcher, error) { - parts := strings.SplitN(pattern, "/", 2) - host, path := parts[0], parts[1] + index := strings.Index(pattern, "/") + if index == -1 { + return nil, fmt.Errorf("Invalid URL pattern: %s (No Path)", pattern) + } + host, path := pattern[:index], pattern[index+1:] hostMatcher, err := compileHostMatcher(host) if err != nil { return nil, fmt.Errorf("Invalid URL pattern: %s (%s)", pattern, err.Error()) diff --git a/host_path_matcher_test.go b/host_path_matcher_test.go index 7ea281b..bc843a3 100644 --- a/host_path_matcher_test.go +++ b/host_path_matcher_test.go @@ -82,6 +82,7 @@ func TestCompileHostPathMatcher(t *testing.T) { t.Run("Invalid", func(t *testing.T) { patterns := []string{ + "hostnameonly", "/service1/*/foo/*", "*/service1/*/foo/*", "*/service1/*/foo/",