make the getPath method safer, fixing panics within App Engine (#189)

This commit is contained in:
Aaron Taylor
2016-08-24 19:34:02 -04:00
committed by Matt Silverlock
parent 674ef1c280
commit 34bf6dc9fa
2 changed files with 16 additions and 6 deletions

8
mux.go
View File

@@ -369,12 +369,8 @@ func getPath(req *http.Request) string {
// for < 1.5 server side workaround
// http://localhost/path/here?v=1 -> /path/here
path := req.RequestURI
if i := len(req.URL.Scheme); i > 0 {
path = path[i+len(`://`):]
}
if i := len(req.URL.Host); i > 0 {
path = path[i:]
}
path = strings.TrimPrefix(path, req.URL.Scheme+`://`)
path = strings.TrimPrefix(path, req.URL.Host)
if i := strings.LastIndex(path, "?"); i > -1 {
path = path[:i]
}

View File

@@ -292,6 +292,20 @@ func TestPath(t *testing.T) {
pathTemplate: `/`,
shouldMatch: true,
},
{
title: "Path route, match root with no host, App Engine format",
route: new(Route).Path("/"),
request: func() *http.Request {
r := newRequest("GET", "http://localhost/")
r.RequestURI = "/"
return r
}(),
vars: map[string]string{},
host: "",
path: "/",
pathTemplate: `/`,
shouldMatch: true,
},
{
title: "Path route, wrong path in request in request URL",
route: new(Route).Path("/111/222/333"),