Skip to content

Commit

Permalink
chore: introduce backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
yashvardhan-kukreja committed Feb 29, 2024
2 parents 70c9f8f + 90d541e commit e391ca0
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions gin.go
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 != "." {
Expand All @@ -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()
Expand All @@ -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()
}

0 comments on commit e391ca0

Please sign in to comment.