From 352d69c71f45d51971f2d23f5c4450c6410aa481 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 29 Nov 2019 00:02:02 +0800 Subject: [PATCH] =?UTF-8?q?chore(performance):=20Improve=20performance=20f?= =?UTF-8?q?or=20adding=20RemoveExtraS=E2=80=A6=20(#2159)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: Add RemoveExtraSlash flag * fix testing Signed-off-by: Bo-Yi Wu --- gin.go | 10 +++++++++- routes_test.go | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/gin.go b/gin.go index c194d6bf..f0cd09e4 100644 --- a/gin.go +++ b/gin.go @@ -97,6 +97,10 @@ type Engine struct { // method call. MaxMultipartMemory int64 + // RemoveExtraSlash a parameter can be parsed from the URL even with extra slashes. + // See the PR #1817 and issue #1644 + RemoveExtraSlash bool + delims render.Delims secureJsonPrefix string HTMLRender render.HTMLRender @@ -134,6 +138,7 @@ func New() *Engine { ForwardedByClientIP: true, AppEngine: defaultAppEngine, UseRawPath: false, + RemoveExtraSlash: false, UnescapePathValues: true, MaxMultipartMemory: defaultMultipartMemory, trees: make(methodTrees, 0, 9), @@ -385,7 +390,10 @@ func (engine *Engine) handleHTTPRequest(c *Context) { rPath = c.Request.URL.RawPath unescape = engine.UnescapePathValues } - rPath = cleanPath(rPath) + + if engine.RemoveExtraSlash { + rPath = cleanPath(rPath) + } // Find root of the tree for the given HTTP method t := engine.trees diff --git a/routes_test.go b/routes_test.go index 0c2f9a0c..d1ddbe91 100644 --- a/routes_test.go +++ b/routes_test.go @@ -263,6 +263,7 @@ func TestRouteParamsByNameWithExtraSlash(t *testing.T) { lastName := "" wild := "" router := New() + router.RemoveExtraSlash = true router.GET("/test/:name/:last_name/*wild", func(c *Context) { name = c.Params.ByName("name") lastName = c.Params.ByName("last_name") @@ -407,6 +408,29 @@ func TestRouteNotAllowedDisabled(t *testing.T) { assert.Equal(t, http.StatusNotFound, w.Code) } +func TestRouterNotFoundWithRemoveExtraSlash(t *testing.T) { + router := New() + router.RemoveExtraSlash = true + router.GET("/path", func(c *Context) {}) + router.GET("/", func(c *Context) {}) + + testRoutes := []struct { + route string + code int + location string + }{ + {"/../path", http.StatusOK, ""}, // CleanPath + {"/nope", http.StatusNotFound, ""}, // NotFound + } + for _, tr := range testRoutes { + w := performRequest(router, "GET", tr.route) + assert.Equal(t, tr.code, w.Code) + if w.Code != http.StatusNotFound { + assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location"))) + } + } +} + func TestRouterNotFound(t *testing.T) { router := New() router.RedirectFixedPath = true @@ -419,14 +443,14 @@ func TestRouterNotFound(t *testing.T) { code int location string }{ - {"/path/", http.StatusMovedPermanently, "/path"}, // TSR -/ - {"/dir", http.StatusMovedPermanently, "/dir/"}, // TSR +/ - {"/PATH", http.StatusMovedPermanently, "/path"}, // Fixed Case - {"/DIR/", http.StatusMovedPermanently, "/dir/"}, // Fixed Case - {"/PATH/", http.StatusMovedPermanently, "/path"}, // Fixed Case -/ - {"/DIR", http.StatusMovedPermanently, "/dir/"}, // Fixed Case +/ - {"/../path", http.StatusOK, ""}, // CleanPath - {"/nope", http.StatusNotFound, ""}, // NotFound + {"/path/", http.StatusMovedPermanently, "/path"}, // TSR -/ + {"/dir", http.StatusMovedPermanently, "/dir/"}, // TSR +/ + {"/PATH", http.StatusMovedPermanently, "/path"}, // Fixed Case + {"/DIR/", http.StatusMovedPermanently, "/dir/"}, // Fixed Case + {"/PATH/", http.StatusMovedPermanently, "/path"}, // Fixed Case -/ + {"/DIR", http.StatusMovedPermanently, "/dir/"}, // Fixed Case +/ + {"/../path", http.StatusMovedPermanently, "/path"}, // Without CleanPath + {"/nope", http.StatusNotFound, ""}, // NotFound } for _, tr := range testRoutes { w := performRequest(router, "GET", tr.route)