Skip to content

Commit

Permalink
return full host:port info from getHost
Browse files Browse the repository at this point in the history
  • Loading branch information
santsai committed May 28, 2018
1 parent e0b5aba commit d493923
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
20 changes: 19 additions & 1 deletion mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ func TestHost(t *testing.T) {
path: "",
shouldMatch: false,
},
// BUG {new(Route).Host("aaa.bbb.ccc:1234"), newRequestHost("GET", "/111/222/333", "aaa.bbb.ccc:1234"), map[string]string{}, "aaa.bbb.ccc:1234", "", true},
{
title: "Host route with port, match with request header",
route: new(Route).Host("aaa.bbb.ccc:1234"),
request: newRequestHost("GET", "/111/222/333", "aaa.bbb.ccc:1234"),
vars: map[string]string{},
host: "aaa.bbb.ccc:1234",
path: "",
shouldMatch: true,
},
{
title: "Host route with port, wrong host in request header",
route: new(Route).Host("aaa.bbb.ccc:1234"),
Expand All @@ -123,6 +131,16 @@ func TestHost(t *testing.T) {
path: "",
shouldMatch: false,
},
{
title: "Host route with pattern, match with request header",
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc:1{v2:(?:23|4)}"),
request: newRequestHost("GET", "/111/222/333", "aaa.bbb.ccc:123"),
vars: map[string]string{"v1": "bbb", "v2": "23"},
host: "aaa.bbb.ccc:123",
path: "",
hostTemplate: `aaa.{v1:[a-z]{3}}.ccc:1{v2:(?:23|4)}`,
shouldMatch: true,
},
{
title: "Host route with pattern, match",
route: new(Route).Host("aaa.{v1:[a-z]{3}}.ccc"),
Expand Down
10 changes: 3 additions & 7 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,13 @@ func (v *routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route)
}

// getHost tries its best to return the request host.
// According to section 14.23 of RFC 2616 the Host header
// can include the port number if the default value of 80 is not used.
func getHost(r *http.Request) string {
if r.URL.IsAbs() {
return r.URL.Host
}
host := r.Host
// Slice off any port information.
if i := strings.Index(host, ":"); i != -1 {
host = host[:i]
}
return host

return r.Host
}

func extractVars(input string, matches []int, names []string, output map[string]string) {
Expand Down

0 comments on commit d493923

Please sign in to comment.