From 34bf6dc9faa08144e925df4fa1838551b16d6c2a Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Wed, 24 Aug 2016 19:34:02 -0400 Subject: [PATCH] make the getPath method safer, fixing panics within App Engine (#189) --- mux.go | 8 ++------ mux_test.go | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/mux.go b/mux.go index 514bfb0..3263a00 100644 --- a/mux.go +++ b/mux.go @@ -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] } diff --git a/mux_test.go b/mux_test.go index 1670764..424c494 100644 --- a/mux_test.go +++ b/mux_test.go @@ -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"),