Skip to content

Commit 98cb6bf

Browse files
fix: regression in vars extract for wildcard host (gorilla#579)
Continuing from PR gorilla#447 we have to add extra check to ignore the port as well add tests to cover this case
1 parent 948bec3 commit 98cb6bf

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

old_test.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ var hostMatcherTests = []hostMatcherTest{
260260
vars: map[string]string{"foo": "abc", "bar": "def", "baz": "ghi"},
261261
result: true,
262262
},
263+
{
264+
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:.*}"),
265+
url: "http://abc.def.ghi:65535/",
266+
vars: map[string]string{"foo": "abc", "bar": "def", "baz": "ghi", "port": "65535"},
267+
result: true,
268+
},
269+
{
270+
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]}"),
271+
url: "http://abc.def.ghi:65535/",
272+
vars: map[string]string{"foo": "abc", "bar": "def", "baz": "ghi"},
273+
result: true,
274+
},
263275
{
264276
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]}"),
265277
url: "http://a.b.c/",
@@ -365,6 +377,11 @@ var urlBuildingTests = []urlBuildingTest{
365377
vars: []string{"subdomain", "bar"},
366378
url: "http://bar.domain.com",
367379
},
380+
{
381+
route: new(Route).Host("{subdomain}.domain.com:{port:.*}"),
382+
vars: []string{"subdomain", "bar", "port", "65535"},
383+
url: "http://bar.domain.com:65535",
384+
},
368385
{
369386
route: new(Route).Host("foo.domain.com").Path("/articles"),
370387
vars: []string{},
@@ -412,7 +429,11 @@ func TestHeaderMatcher(t *testing.T) {
412429

413430
func TestHostMatcher(t *testing.T) {
414431
for _, v := range hostMatcherTests {
415-
request, _ := http.NewRequest("GET", v.url, nil)
432+
request, err := http.NewRequest("GET", v.url, nil)
433+
if err != nil {
434+
t.Errorf("http.NewRequest failed %#v", err)
435+
continue
436+
}
416437
var routeMatch RouteMatch
417438
result := v.matcher.Match(request, &routeMatch)
418439
vars := routeMatch.Vars

regexp.go

+6
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ func (v routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) {
325325
// Store host variables.
326326
if v.host != nil {
327327
host := getHost(req)
328+
if v.host.wildcardHostPort {
329+
// Don't be strict on the port match
330+
if i := strings.Index(host, ":"); i != -1 {
331+
host = host[:i]
332+
}
333+
}
328334
matches := v.host.regexp.FindStringSubmatchIndex(host)
329335
if len(matches) > 0 {
330336
extractVars(host, matches, v.host.varsN, m.Vars)

0 commit comments

Comments
 (0)