chore(performance): Improve performance for adding RemoveExtraS… (#2159)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* chore: Add RemoveExtraSlash flag * fix testing
authored by Bo-Yi Wu · committed by GitHub · parent 231ff00 · Browse files · 2 files·+41 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Diff stats
2 files+41 added−9 removed
- other:+41 added−9 removed
Provenance
- Commit
352d69c- Tree
ae6fcc6- Author
Bo-Yi Wu <appleboy.tw@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
231ff00
2 files changed+41 added−9 removed
modified:gin.go+9 added-1 removed
95 unmodified lines96 // Value of 'maxMemory' param that is given to http.Request's ParseMultipartForm97 // method call.98 MaxMultipartMemory int6499 100+ // RemoveExtraSlash a parameter can be parsed from the URL even with extra slashes.101+ // See the PR #1817 and issue #1644102+ RemoveExtraSlash bool103+104 delims render.Delims105 secureJsonPrefix string106 HTMLRender render.HTMLRender107 FuncMap template.FuncMap29 unmodified lines137 HandleMethodNotAllowed: false,138 ForwardedByClientIP: true,139 AppEngine: defaultAppEngine,140 UseRawPath: false,141+ RemoveExtraSlash: false,142 UnescapePathValues: true,143 MaxMultipartMemory: defaultMultipartMemory,144 trees: make(methodTrees, 0, 9),145 delims: render.Delims{Left: "{{", Right: "}}"},243 unmodified lines389 if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {390 rPath = c.Request.URL.RawPath391 unescape = engine.UnescapePathValues392 }388- rPath = cleanPath(rPath)393 394+ if engine.RemoveExtraSlash {395+ rPath = cleanPath(rPath)396+ }397+398 // Find root of the tree for the given HTTP method399 t := engine.trees400 for i, tl := 0, len(t); i < tl; i++ {401 if t[i].method != httpMethod {98 unmodified linesmodified:routes_test.go+32 added-8 removed
261 unmodified lines262 name := ""263 lastName := ""264 wild := ""265 router := New()266+ router.RemoveExtraSlash = true267 router.GET("/test/:name/:last_name/*wild", func(c *Context) {268 name = c.Params.ByName("name")269 lastName = c.Params.ByName("last_name")270 var ok bool136 unmodified lines407 assert.Equal(t, "404 page not found", w.Body.String())408 assert.Equal(t, http.StatusNotFound, w.Code)409 }410 411+func TestRouterNotFoundWithRemoveExtraSlash(t *testing.T) {412+ router := New()413+ router.RemoveExtraSlash = true414+ router.GET("/path", func(c *Context) {})415+ router.GET("/", func(c *Context) {})416+417+ testRoutes := []struct {418+ route string419+ code int420+ location string421+ }{422+ {"/../path", http.StatusOK, ""}, // CleanPath423+ {"/nope", http.StatusNotFound, ""}, // NotFound424+ }425+ for _, tr := range testRoutes {426+ w := performRequest(router, "GET", tr.route)427+ assert.Equal(t, tr.code, w.Code)428+ if w.Code != http.StatusNotFound {429+ assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location")))430+ }431+ }432+}433+434 func TestRouterNotFound(t *testing.T) {435 router := New()436 router.RedirectFixedPath = true437 router.GET("/path", func(c *Context) {})4 unmodified lines442 route string443 code int444 location string445 }{422- {"/path/", http.StatusMovedPermanently, "/path"}, // TSR -/423- {"/dir", http.StatusMovedPermanently, "/dir/"}, // TSR +/424- {"/PATH", http.StatusMovedPermanently, "/path"}, // Fixed Case425- {"/DIR/", http.StatusMovedPermanently, "/dir/"}, // Fixed Case426- {"/PATH/", http.StatusMovedPermanently, "/path"}, // Fixed Case -/427- {"/DIR", http.StatusMovedPermanently, "/dir/"}, // Fixed Case +/428- {"/../path", http.StatusOK, ""}, // CleanPath429- {"/nope", http.StatusNotFound, ""}, // NotFound446+ {"/path/", http.StatusMovedPermanently, "/path"}, // TSR -/447+ {"/dir", http.StatusMovedPermanently, "/dir/"}, // TSR +/448+ {"/PATH", http.StatusMovedPermanently, "/path"}, // Fixed Case449+ {"/DIR/", http.StatusMovedPermanently, "/dir/"}, // Fixed Case450+ {"/PATH/", http.StatusMovedPermanently, "/path"}, // Fixed Case -/451+ {"/DIR", http.StatusMovedPermanently, "/dir/"}, // Fixed Case +/452+ {"/../path", http.StatusMovedPermanently, "/path"}, // Without CleanPath453+ {"/nope", http.StatusNotFound, ""}, // NotFound454 }455 for _, tr := range testRoutes {456 w := performRequest(router, "GET", tr.route)457 assert.Equal(t, tr.code, w.Code)163 unmodified lines