Skip to content

Commit

Permalink
fix: regression in vars extract for wildcard host (gorilla#579)
Browse files Browse the repository at this point in the history
Continuing from PR gorilla#447 we have to add extra
check to ignore the port as well

add tests to cover this case
  • Loading branch information
harshavardhana authored Jul 11, 2020
1 parent 948bec3 commit 98cb6bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
23 changes: 22 additions & 1 deletion old_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 260,18 @@ var hostMatcherTests = []hostMatcherTest{
vars: map[string]string{"foo": "abc", "bar": "def", "baz": "ghi"},
result: true,
},
{
matcher: NewRouter().NewRoute().Host("{foo:[a-z][a-z][a-z]}.{bar:[a-z][a-z][a-z]}.{baz:[a-z][a-z][a-z]}:{port:.*}"),
url: "http://abc.def.ghi:65535/",
vars: map[string]string{"foo": "abc", "bar": "def", "baz": "ghi", "port": "65535"},
result: true,
},
{
matcher: NewRouter().NewRoute().Host("{foo:[a-z][a-z][a-z]}.{bar:[a-z][a-z][a-z]}.{baz:[a-z][a-z][a-z]}"),
url: "http://abc.def.ghi:65535/",
vars: map[string]string{"foo": "abc", "bar": "def", "baz": "ghi"},
result: true,
},
{
matcher: NewRouter().NewRoute().Host("{foo:[a-z][a-z][a-z]}.{bar:[a-z][a-z][a-z]}.{baz:[a-z][a-z][a-z]}"),
url: "http://a.b.c/",
Expand Down Expand Up @@ -365,6 377,11 @@ var urlBuildingTests = []urlBuildingTest{
vars: []string{"subdomain", "bar"},
url: "http://bar.domain.com",
},
{
route: new(Route).Host("{subdomain}.domain.com:{port:.*}"),
vars: []string{"subdomain", "bar", "port", "65535"},
url: "http://bar.domain.com:65535",
},
{
route: new(Route).Host("foo.domain.com").Path("/articles"),
vars: []string{},
Expand Down Expand Up @@ -412,7 429,11 @@ func TestHeaderMatcher(t *testing.T) {

func TestHostMatcher(t *testing.T) {
for _, v := range hostMatcherTests {
request, _ := http.NewRequest("GET", v.url, nil)
request, err := http.NewRequest("GET", v.url, nil)
if err != nil {
t.Errorf("http.NewRequest failed %#v", err)
continue
}
var routeMatch RouteMatch
result := v.matcher.Match(request, &routeMatch)
vars := routeMatch.Vars
Expand Down
6 changes: 6 additions & 0 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 325,12 @@ func (v routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) {
// Store host variables.
if v.host != nil {
host := getHost(req)
if v.host.wildcardHostPort {
// Don't be strict on the port match
if i := strings.Index(host, ":"); i != -1 {
host = host[:i]
}
}
matches := v.host.regexp.FindStringSubmatchIndex(host)
if len(matches) > 0 {
extractVars(host, matches, v.host.varsN, m.Vars)
Expand Down

0 comments on commit 98cb6bf

Please sign in to comment.