From 12ed1b171560397e5c1b922f0ae91cd0c19a75e6 Mon Sep 17 00:00:00 2001 From: Atennop1 Date: Thu, 15 Jan 2026 16:38:12 +0300 Subject: [PATCH] feat: RedirectTrailingSlash(false) now assumes that /path and /path/ are the same --- gin.go | 40 +++++++++++++++++++++++++++++----------- routes_test.go | 8 ++++---- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/gin.go b/gin.go index 2e033bf3..bacd8bfa 100644 --- a/gin.go +++ b/gin.go @@ -712,21 +712,39 @@ func (engine *Engine) handleHTTPRequest(c *Context) { } root := t[i].root // Find route in tree - value := root.getValue(rPath, c.params, c.skippedNodes, unescape) - if value.params != nil { - c.Params = *value.params + isRouteFound := func(value nodeValue) bool { + if value.params != nil { + c.Params = *value.params + } + if value.handlers != nil { + c.handlers = value.handlers + c.fullPath = value.fullPath + c.Next() + c.writermem.WriteHeaderNow() + return true + } + return false } - if value.handlers != nil { - c.handlers = value.handlers - c.fullPath = value.fullPath - c.Next() - c.writermem.WriteHeaderNow() + + value := root.getValue(rPath, c.params, c.skippedNodes, unescape) + if isRouteFound(value) { return } + if httpMethod != http.MethodConnect && rPath != "/" { - if value.tsr && engine.RedirectTrailingSlash { - redirectTrailingSlash(c) - return + if value.tsr { + if engine.engine.RedirectTrailingSlash { + redirectTrailingSlash(c) + return + } + newRPath := rPath + "/" + if rPath[len(rPath)-1] == '/' { + newRPath = rPath[:len(rPath)-1] + } + value = root.getValue(newRPath, c.params, c.skippedNodes, unescape) + if isRouteFound(value) { + return + } } if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) { return diff --git a/routes_test.go b/routes_test.go index 1cae3fce..1b71dace 100644 --- a/routes_test.go +++ b/routes_test.go @@ -237,13 +237,13 @@ func TestRouteRedirectTrailingSlash(t *testing.T) { router.RedirectTrailingSlash = false w = PerformRequest(router, http.MethodGet, "/path/") - assert.Equal(t, http.StatusNotFound, w.Code) + assert.Equal(t, http.StatusOK, w.Code) w = PerformRequest(router, http.MethodGet, "/path2") - assert.Equal(t, http.StatusNotFound, w.Code) + assert.Equal(t, http.StatusOK, w.Code) w = PerformRequest(router, http.MethodPost, "/path3/") - assert.Equal(t, http.StatusNotFound, w.Code) + assert.Equal(t, http.StatusOK, w.Code) w = PerformRequest(router, http.MethodPut, "/path4") - assert.Equal(t, http.StatusNotFound, w.Code) + assert.Equal(t, http.StatusOK, w.Code) } func TestRouteRedirectFixedPath(t *testing.T) {