From fd70e6edb1b202014fed4b973d57a7414eab6905 Mon Sep 17 00:00:00 2001 From: Marco Jantke Date: Fri, 6 Oct 2017 11:34:03 +0200 Subject: [PATCH] enable prefix matching within slash boundaries --- middlewares/stripPrefix.go | 10 +--------- middlewares/stripPrefix_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/middlewares/stripPrefix.go b/middlewares/stripPrefix.go index 2f2529796..8c1499d63 100644 --- a/middlewares/stripPrefix.go +++ b/middlewares/stripPrefix.go @@ -16,17 +16,9 @@ type StripPrefix struct { func (s *StripPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) { for _, prefix := range s.Prefixes { - origPrefix := strings.TrimSpace(prefix) - if origPrefix == r.URL.Path { - r.URL.Path = "/" - s.serveRequest(w, r, origPrefix) - return - } - - prefix = strings.TrimSuffix(origPrefix, "/") + "/" if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) { r.URL.Path = "/" + strings.TrimPrefix(p, "/") - s.serveRequest(w, r, origPrefix) + s.serveRequest(w, r, strings.TrimSpace(prefix)) return } } diff --git a/middlewares/stripPrefix_test.go b/middlewares/stripPrefix_test.go index 2103102c1..2e8778d72 100644 --- a/middlewares/stripPrefix_test.go +++ b/middlewares/stripPrefix_test.go @@ -86,6 +86,14 @@ func TestStripPrefix(t *testing.T) { expectedPath: "/", expectedHeader: "/stat", }, + { + desc: "prefix matching within slash boundaries", + prefixes: []string{"/stat"}, + path: "/status", + expectedStatusCode: http.StatusOK, + expectedPath: "/us", + expectedHeader: "/stat", + }, } for _, test := range tests {