From b6360115493e1c8163f995122adfc399ba02c387 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Wed, 15 Nov 2017 09:51:47 -0800 Subject: [PATCH] Issue #389: match exact host before glob matches This patch ensures that exact hostname matches are preferred over glob matches. Fixes #389 --- route/table.go | 4 ++-- route/table_test.go | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/route/table.go b/route/table.go index e2af931a4..bd6c3aa22 100644 --- a/route/table.go +++ b/route/table.go @@ -293,7 +293,7 @@ func (t Table) matchingHosts(req *http.Request) (hosts []string) { hosts = append(hosts, pattern) } } - sort.Strings(hosts) + sort.Sort(sort.Reverse(sort.StringSlice(hosts))) return hosts } @@ -364,7 +364,7 @@ func (t Table) config(addWeight bool) []string { hosts = append(hosts, host) } } - sort.Strings(hosts) + sort.Sort(sort.Reverse(sort.StringSlice(hosts))) // entries without host come last hosts = append(hosts, "") diff --git a/route/table_test.go b/route/table_test.go index f4881d349..fa35806b7 100644 --- a/route/table_test.go +++ b/route/table_test.go @@ -470,6 +470,7 @@ func TestTableLookup(t *testing.T) { route add svc abc.com/foo/ http://foo.com:2000 route add svc abc.com/foo/bar http://foo.com:2500 route add svc abc.com/foo/bar/ http://foo.com:3000 + route add svc z.abc.com/foo/ http://foo.com:3100 route add svc *.abc.com/ http://foo.com:4000 route add svc *.abc.com/foo/ http://foo.com:5000 ` @@ -513,6 +514,9 @@ func TestTableLookup(t *testing.T) { {&http.Request{Host: "x.y.abc.com", URL: mustParse("/foo/")}, "http://foo.com:5000"}, {&http.Request{Host: "y.abc.com:80", URL: mustParse("/foo/")}, "http://foo.com:5000"}, {&http.Request{Host: "y.abc.com:443", URL: mustParse("/foo/"), TLS: &tls.ConnectionState{}}, "http://foo.com:5000"}, + + // exact match has precedence over glob match + {&http.Request{Host: "z.abc.com", URL: mustParse("/foo/")}, "http://foo.com:3100"}, } for i, tt := range tests {