Skip to content

Commit

Permalink
modified and test matchinghosts
Browse files Browse the repository at this point in the history
  • Loading branch information
javier.molina authored and javier.molina committed Oct 30, 2018
1 parent 8b59614 commit 6e15729
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
25 changes: 21 additions & 4 deletions route/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,25 @@ func normalizeHost(host string, tls bool) string {
return host
}

type byDots []string

func (s byDots) Len() int {
return len(s)
}
func (s byDots) Swap(i, j int) {
s[i], s[j] = s[j], s[i]

}
func (s byDots) Less(i, j int) bool {
if strings.Count(s[i], ".") < strings.Count(s[j], ".") {
return false
}
if strings.Count(s[i], ".") > strings.Count(s[j], ".") {
return true
}
return s[i] < s[j]
}

// matchingHostsStr returns all keys (host name patterns) from the
// routing table which match the normalized host and takes if it is TLS into account.
func (t Table) matchingHostsStr(host string, isTLS bool) (hosts []string) {
Expand All @@ -303,7 +322,6 @@ func (t Table) matchingHostsStr(host string, isTLS bool) (hosts []string) {
hosts = append(hosts, pattern)
}
}
//sort.Sort(sort.Reverse(sort.StringSlice(hosts)))
sort.Sort(sort.Reverse(sort.StringSlice(hosts)))
i := -1
for j, v := range hosts {
Expand All @@ -313,10 +331,10 @@ func (t Table) matchingHostsStr(host string, isTLS bool) (hosts []string) {
}
}
if i != -1 {
sort.Sort(sort.StringSlice(hosts[i:]))
sort.Sort(byDots(hosts[i:]))
}

return hosts

}

// matchingHosts returns all keys (host name patterns) from the
Expand All @@ -326,7 +344,6 @@ func (t Table) matchingHosts(req *http.Request) (hosts []string) {
}

func checkHTTPRedirect(req *http.Request, hosts []string) string {
fmt.Printf("[INFO] req.Host %s\n", req.Host)
if !strings.Contains(req.Host, ":") && req.TLS == nil {
for _, h := range hosts {
if strings.HasSuffix(h, ":80") {
Expand Down
66 changes: 64 additions & 2 deletions route/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ func TestTableLookupGlobalRedirect(t *testing.T) {
route add svc *.abc.com/foo/ http://foo.com:5000
route add svc *.xyz.com/ https://xyz.com
route add svc foo.xyz.com:80/ https://zyx.com
route add svc *.d.a.b.com http://test.com
`

tbl, err := NewTable(s)
Expand Down Expand Up @@ -714,11 +715,13 @@ func TestTableLookupGlobalRedirect(t *testing.T) {

// explicit port on route
{&http.Request{Host: "s.xyz.com", URL: mustParse("/")}, "https://s.xyz.com/"},
{&http.Request{Host: "s.xyz.com", URL: mustParse("/"), TLS: &tls.ConnectionState{}}, "http://foo.com:7000"},
{&http.Request{Host: "s.xyz.com", URL: mustParse("/"), TLS: &tls.ConnectionState{}}, "https://xyz.com"},

// just defined 80
{&http.Request{Host: "foo.xyz.com", URL: mustParse("/")}, "https://zyx.com"},
{&http.Request{Host: "foo.xyz.com", URL: mustParse("/"), TLS: &tls.ConnectionState{}}, "http://foo.com:7000"},
{&http.Request{Host: "foo.xyz.com", URL: mustParse("/"), TLS: &tls.ConnectionState{}}, "https://xyz.com"},
{&http.Request{Host: "test.d.a.b.com", URL: mustParse("/")}, "https://test.d.a.b.com/"},
{&http.Request{Host: "test.d.a.b.com", URL: mustParse("/"), TLS: &tls.ConnectionState{}}, "http://test.com"},
}

for i, tt := range tests {
Expand All @@ -732,3 +735,62 @@ func TestTableLookupGlobalRedirect(t *testing.T) {
}
}
}

func TestMatchinHostStr(t *testing.T) {
s := `
route add redirect *:80/ https://$host/$path opts "redirect=301"
route add svc / http://foo.com:800
route add svc /foo http://foo.com:900
route add svc *.com/ http://foo.com:7000
route add svc abc.com/ http://foo.com:1000
route add svc abc.com/foo http://foo.com:1500
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
route add svc *.xyz.com/ https://xyz.com
route add svc foo.xyz.com:80/ https://zyx.com
route add svc *.a.b.com http://test.com
route add svc *.b.com http://test.com
route add svc *.com http://test.com
route add svc *.abbc.b.com http://test.com
route add svc *.d.a.b.com http://test.com
`
tbl, err := NewTable(s)
if err != nil {
t.Fatal(err)
}

var tests = []struct {
req *http.Request
dst []string
}{
{&http.Request{Host: "abc.com", URL: mustParse("/")}, []string{"abc.com", "*.com", "*:80"}},
{&http.Request{Host: "foo.xyz.com", URL: mustParse("/")}, []string{"foo.xyz.com:80", "*.xyz.com", "*.com", "*:80"}},
{&http.Request{Host: "test.d.a.b.com", URL: mustParse("/")}, []string{"*.d.a.b.com", "*.a.b.com", "*.b.com", "*.com", "*:80"}},
{&http.Request{Host: "d.a.b.com", URL: mustParse("/")}, []string{"*.a.b.com", "*.b.com", "*.com", "*:80"}},
{&http.Request{Host: "abc.com:80", URL: mustParse("/")}, []string{"abc.com", "*.com", "*:80"}},
{&http.Request{Host: "def.io:443", URL: mustParse("/"), TLS: &tls.ConnectionState{}}, []string{}},
{&http.Request{Host: "def.io", URL: mustParse("/")}, []string{"*:80"}},
{&http.Request{Host: "foo.xyz.com", URL: mustParse("/")}, []string{"foo.xyz.com:80", "*.xyz.com", "*.com", "*:80"}},
{&http.Request{Host: "foo.xyz.com", URL: mustParse("/"), TLS: &tls.ConnectionState{}}, []string{"*.xyz.com", "*.com"}},
}
for i, tt := range tests {
hosts := tbl.matchingHostsStr(tt.req.Host, tt.req.TLS != nil)
if len(hosts) != len(tt.dst) {
t.Errorf("%d: got %v want %v", i, hosts, tt.dst)
break
}
check := 0
for k, v := range hosts {
if v != tt.dst[k] {
check++
}
}
if check > 0 {
t.Errorf("%d: got %v want %v", i, hosts, tt.dst)
}
}
}

0 comments on commit 6e15729

Please sign in to comment.