mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-07 04:38:19 +08:00
Merge 8596512f4fdb118a3c134de8300d7d917a9e3d78 into 5f4f9643258dc2a65e684b63f12c8d543c936c67
This commit is contained in:
commit
5bd9e3061b
24
gin.go
24
gin.go
@ -101,6 +101,8 @@ type Engine struct {
|
|||||||
// For example if /foo/ is requested but a route only exists for /foo, the
|
// For example if /foo/ is requested but a route only exists for /foo, the
|
||||||
// client is redirected to /foo with http status code 301 for GET requests
|
// client is redirected to /foo with http status code 301 for GET requests
|
||||||
// and 307 for all other request methods.
|
// and 307 for all other request methods.
|
||||||
|
// If this field is false then /foo and /foo/ are assumed the same and no redirection will happen,
|
||||||
|
// both endpoints will use the same handlers.
|
||||||
RedirectTrailingSlash bool
|
RedirectTrailingSlash bool
|
||||||
|
|
||||||
// RedirectFixedPath if enabled, the router tries to fix the current request path, if no
|
// RedirectFixedPath if enabled, the router tries to fix the current request path, if no
|
||||||
@ -712,7 +714,7 @@ 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
|
||||||
}
|
}
|
||||||
@ -721,13 +723,31 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|||||||
c.fullPath = value.fullPath
|
c.fullPath = value.fullPath
|
||||||
c.Next()
|
c.Next()
|
||||||
c.writermem.WriteHeaderNow()
|
c.writermem.WriteHeaderNow()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
value := root.getValue(rPath, c.params, c.skippedNodes, unescape)
|
||||||
|
if isRouteFound(value) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if httpMethod != http.MethodConnect && rPath != "/" {
|
if httpMethod != http.MethodConnect && rPath != "/" {
|
||||||
if value.tsr && engine.RedirectTrailingSlash {
|
if value.tsr {
|
||||||
|
if engine.engine.RedirectTrailingSlash {
|
||||||
redirectTrailingSlash(c)
|
redirectTrailingSlash(c)
|
||||||
return
|
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