From 90d541ec284b55d7c86bc7a51b9740a7d1940887 Mon Sep 17 00:00:00 2001 From: Yashvardhan Kukreja Date: Wed, 28 Feb 2024 20:13:21 -0500 Subject: [PATCH] chore: introduce backwards compatibility Signed-off-by: Yashvardhan Kukreja --- gin.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/gin.go b/gin.go index 04d2959a..7cbba093 100644 --- a/gin.go +++ b/gin.go @@ -636,14 +636,15 @@ func (engine *Engine) handleHTTPRequest(c *Context) { return } if httpMethod != http.MethodConnect && rPath != "/" { + executeRedirectionMiddlewares := len(engine.redirectMethod) > 0 if value.tsr && engine.RedirectTrailingSlash { c.handlers = engine.allRedirectMethod - redirectTrailingSlash(c) + redirectTrailingSlash(c, executeRedirectionMiddlewares) return } if engine.RedirectFixedPath { c.handlers = engine.allRedirectMethod - if redirectFixedPath(c, root, engine.RedirectFixedPath) { + if redirectFixedPath(c, root, engine.RedirectFixedPath, executeRedirectionMiddlewares) { return } } @@ -694,7 +695,7 @@ func serveError(c *Context, code int, defaultMessage []byte) { c.writermem.WriteHeaderNow() } -func redirectTrailingSlash(c *Context) { +func redirectTrailingSlash(c *Context, withRedirectionMiddlewares bool) { req := c.Request p := req.URL.Path 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] == '/' { 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 rPath := req.URL.Path if fixedPath, ok := root.findCaseInsensitivePath(cleanPath(rPath), trailingSlash); ok { req.URL.Path = bytesconv.BytesToString(fixedPath) - redirectRequest(c) + redirectRequest(c, withRedirectionMiddlewares) return true } return false } -func redirectRequest(c *Context) { +func redirectRequest(c *Context, executeRequestChain bool) { req := c.Request rPath := req.URL.Path rURL := req.URL.String() @@ -732,7 +733,9 @@ func redirectRequest(c *Context) { code = http.StatusTemporaryRedirect } debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL) - c.Next() + if executeRequestChain { + c.Next() + } http.Redirect(c.Writer, req, rURL, code) c.writermem.WriteHeaderNow() }