mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-04 01:38:12 +08:00
feat: RedirectTrailingSlash(false) now assumes that /path and /path/ are the same
This commit is contained in:
parent
9914178584
commit
12ed1b1715
40
gin.go
40
gin.go
@ -712,21 +712,39 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|||||||
}
|
}
|
||||||
root := t[i].root
|
root := t[i].root
|
||||||
// Find route in tree
|
// Find route in tree
|
||||||
value := root.getValue(rPath, c.params, c.skippedNodes, unescape)
|
isRouteFound := func(value nodeValue) bool {
|
||||||
if value.params != nil {
|
if value.params != nil {
|
||||||
c.Params = *value.params
|
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
|
value := root.getValue(rPath, c.params, c.skippedNodes, unescape)
|
||||||
c.fullPath = value.fullPath
|
if isRouteFound(value) {
|
||||||
c.Next()
|
|
||||||
c.writermem.WriteHeaderNow()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if httpMethod != http.MethodConnect && rPath != "/" {
|
if httpMethod != http.MethodConnect && rPath != "/" {
|
||||||
if value.tsr && engine.RedirectTrailingSlash {
|
if value.tsr {
|
||||||
redirectTrailingSlash(c)
|
if engine.engine.RedirectTrailingSlash {
|
||||||
return
|
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) {
|
if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
|
||||||
return
|
return
|
||||||
|
|||||||
@ -237,13 +237,13 @@ func TestRouteRedirectTrailingSlash(t *testing.T) {
|
|||||||
router.RedirectTrailingSlash = false
|
router.RedirectTrailingSlash = false
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/")
|
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")
|
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/")
|
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")
|
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) {
|
func TestRouteRedirectFixedPath(t *testing.T) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user