chore: introduce backwards compatibility

Signed-off-by: Yashvardhan Kukreja <yash.kukreja.98@gmail.com>
This commit is contained in:
Yashvardhan Kukreja 2024-02-28 20:13:21 -05:00
parent 70c9f8f7b5
commit 90d541ec28
No known key found for this signature in database
1 changed files with 11 additions and 8 deletions

19
gin.go
View File

@ -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()
}