Case insensitive host rule

This commit is contained in:
Benjamin Gandon 2018-10-25 10:18:03 +02:00 committed by Traefiker Bot
parent 22ee8700ca
commit aa26927d61
4 changed files with 45 additions and 5 deletions

3
Gopkg.lock generated
View file

@ -291,7 +291,7 @@
branch = "master"
name = "github.com/containous/mux"
packages = ["."]
revision = "06ccd3e75091eb659b1d720cda0e16bc7057954c"
revision = "c33f32e268983f989290677351b871b65da75ba5"
[[projects]]
name = "github.com/containous/staert"
@ -819,6 +819,7 @@
revision = "9b66602d496a139e4722bdde32f0f1ac1c12d4a8"
[[projects]]
branch = "master"
name = "github.com/jjcollinge/servicefabric"
packages = ["."]
revision = "8eebe170fa1ba25d3dfb928b3f86a7313b13b9fe"

View file

@ -53,10 +53,10 @@ func (r *Rules) host(hosts ...string) *mux.Route {
})
}
func (r *Rules) hostRegexp(hosts ...string) *mux.Route {
func (r *Rules) hostRegexp(hostPatterns ...string) *mux.Route {
router := r.Route.Route.Subrouter()
for _, host := range hosts {
router.Host(strings.ToLower(host))
for _, hostPattern := range hostPatterns {
router.Host(hostPattern)
}
return r.Route.Route
}

View file

@ -195,6 +195,39 @@ func TestHostRegexp(t *testing.T) {
"http://barcom": false,
},
},
{
desc: "regex insensitive",
hostExp: "{dummy:[A-Za-z-]+\\.bar\\.com}",
urls: map[string]bool{
"http://FOO.bar.com": true,
"http://foo.bar.com": true,
"http://fooubar.com": false,
"http://barucom": false,
"http://barcom": false,
},
},
{
desc: "insensitive host",
hostExp: "{dummy:[a-z-]+\\.bar\\.com}",
urls: map[string]bool{
"http://FOO.bar.com": true,
"http://foo.bar.com": true,
"http://fooubar.com": false,
"http://barucom": false,
"http://barcom": false,
},
},
{
desc: "insensitive host simple",
hostExp: "foo.bar.com",
urls: map[string]bool{
"http://FOO.bar.com": true,
"http://foo.bar.com": true,
"http://fooubar.com": false,
"http://barucom": false,
"http://barcom": false,
},
},
}
for _, test := range testCases {
@ -212,7 +245,7 @@ func TestHostRegexp(t *testing.T) {
for testURL, match := range test.urls {
req := testhelpers.MustNewRequest(http.MethodGet, testURL, nil)
assert.Equal(t, match, rt.Match(req, &mux.RouteMatch{}))
assert.Equal(t, match, rt.Match(req, &mux.RouteMatch{}), testURL)
}
})
}

View file

@ -53,6 +53,12 @@ func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash,
varsN := make([]string, len(idxs)/2)
varsR := make([]*regexp.Regexp, len(idxs)/2)
pattern := bytes.NewBufferString("")
// Host matching is case insensitive
if matchHost {
fmt.Fprint(pattern, "(?i)")
}
pattern.WriteByte('^')
reverse := bytes.NewBufferString("")
var end int