mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
chore: introduce backwards compatibility
Signed-off-by: Yashvardhan Kukreja <yash.kukreja.98@gmail.com>
This commit is contained in:
parent
70c9f8f7b5
commit
90d541ec28
19
gin.go
19
gin.go
@ -636,14 +636,15 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if httpMethod != http.MethodConnect && rPath != "/" {
|
if httpMethod != http.MethodConnect && rPath != "/" {
|
||||||
|
executeRedirectionMiddlewares := len(engine.redirectMethod) > 0
|
||||||
if value.tsr && engine.RedirectTrailingSlash {
|
if value.tsr && engine.RedirectTrailingSlash {
|
||||||
c.handlers = engine.allRedirectMethod
|
c.handlers = engine.allRedirectMethod
|
||||||
redirectTrailingSlash(c)
|
redirectTrailingSlash(c, executeRedirectionMiddlewares)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if engine.RedirectFixedPath {
|
if engine.RedirectFixedPath {
|
||||||
c.handlers = engine.allRedirectMethod
|
c.handlers = engine.allRedirectMethod
|
||||||
if redirectFixedPath(c, root, engine.RedirectFixedPath) {
|
if redirectFixedPath(c, root, engine.RedirectFixedPath, executeRedirectionMiddlewares) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -694,7 +695,7 @@ func serveError(c *Context, code int, defaultMessage []byte) {
|
|||||||
c.writermem.WriteHeaderNow()
|
c.writermem.WriteHeaderNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
func redirectTrailingSlash(c *Context) {
|
func redirectTrailingSlash(c *Context, withRedirectionMiddlewares bool) {
|
||||||
req := c.Request
|
req := c.Request
|
||||||
p := req.URL.Path
|
p := req.URL.Path
|
||||||
if prefix := path.Clean(c.Request.Header.Get("X-Forwarded-Prefix")); prefix != "." {
|
if prefix := path.Clean(c.Request.Header.Get("X-Forwarded-Prefix")); prefix != "." {
|
||||||
@ -707,22 +708,22 @@ func redirectTrailingSlash(c *Context) {
|
|||||||
if length := len(p); length > 1 && p[length-1] == '/' {
|
if length := len(p); length > 1 && p[length-1] == '/' {
|
||||||
req.URL.Path = p[:length-1]
|
req.URL.Path = p[:length-1]
|
||||||
}
|
}
|
||||||
redirectRequest(c)
|
redirectRequest(c, withRedirectionMiddlewares)
|
||||||
}
|
}
|
||||||
|
|
||||||
func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool {
|
func redirectFixedPath(c *Context, root *node, trailingSlash, withRedirectionMiddlewares bool) bool {
|
||||||
req := c.Request
|
req := c.Request
|
||||||
rPath := req.URL.Path
|
rPath := req.URL.Path
|
||||||
|
|
||||||
if fixedPath, ok := root.findCaseInsensitivePath(cleanPath(rPath), trailingSlash); ok {
|
if fixedPath, ok := root.findCaseInsensitivePath(cleanPath(rPath), trailingSlash); ok {
|
||||||
req.URL.Path = bytesconv.BytesToString(fixedPath)
|
req.URL.Path = bytesconv.BytesToString(fixedPath)
|
||||||
redirectRequest(c)
|
redirectRequest(c, withRedirectionMiddlewares)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func redirectRequest(c *Context) {
|
func redirectRequest(c *Context, executeRequestChain bool) {
|
||||||
req := c.Request
|
req := c.Request
|
||||||
rPath := req.URL.Path
|
rPath := req.URL.Path
|
||||||
rURL := req.URL.String()
|
rURL := req.URL.String()
|
||||||
@ -732,7 +733,9 @@ func redirectRequest(c *Context) {
|
|||||||
code = http.StatusTemporaryRedirect
|
code = http.StatusTemporaryRedirect
|
||||||
}
|
}
|
||||||
debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
|
debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
|
||||||
c.Next()
|
if executeRequestChain {
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
http.Redirect(c.Writer, req, rURL, code)
|
http.Redirect(c.Writer, req, rURL, code)
|
||||||
c.writermem.WriteHeaderNow()
|
c.writermem.WriteHeaderNow()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user