Skip to content

Commit

Permalink
Merge branch 'main' into mtoff/dbstats-invalid-uds
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio authored May 8, 2024
2 parents b39d51c + 7f6597f commit 68300cf
Show file tree
Hide file tree
Showing 3 changed files with 615 additions and 26 deletions.
39 changes: 30 additions & 9 deletions contrib/dimfeld/httptreemux.v5/httptreemux.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,26 @@ func getRoute(router *httptreemux.TreeMux, w http.ResponseWriter, req *http.Requ
if !found {
return "", false
}
routeLen := len(route)
trailingSlash := route[routeLen-1] == '/' && routeLen > 1

// Check for redirecting route due to trailing slash for parameters.
// The redirecting behaviour originates from httptreemux router.
if lr.StatusCode == http.StatusMovedPermanently && strings.HasSuffix(route, "/") {
// Retry the population of lookup result parameters.
// If the initial attempt to populate the parameters fails, clone the request and modify the URI and URL Path.
// Depending on whether the route has a trailing slash or not, it will either add or remove the trailing slash and retry the lookup.
if routerRedirectEnabled(router) && isSupportedRedirectStatus(lr.StatusCode) && lr.Params == nil {
rReq := req.Clone(req.Context())
rReq.RequestURI = strings.TrimSuffix(rReq.RequestURI, "/")
rReq.URL.Path = strings.TrimSuffix(rReq.URL.Path, "/")

lr, found = router.Lookup(w, rReq)
if !found {
return "", false
if trailingSlash {
// if the route has a trailing slash, remove it
rReq.RequestURI = strings.TrimSuffix(rReq.RequestURI, "/")
rReq.URL.Path = strings.TrimSuffix(rReq.URL.Path, "/")
} else {
// if the route does not have a trailing slash, add one
rReq.RequestURI = rReq.RequestURI + "/"
rReq.URL.Path = rReq.URL.Path + "/"
}
// no need to check found again
// we already matched a route and we are only trying to populate the lookup result params
lr, _ = router.Lookup(w, rReq)
}

for k, v := range lr.Params {
Expand All @@ -141,3 +149,16 @@ func getRoute(router *httptreemux.TreeMux, w http.ResponseWriter, req *http.Requ
}
return route, true
}

// isSupportedRedirectStatus checks if the given HTTP status code is a supported redirect status.
func isSupportedRedirectStatus(status int) bool {
return status == http.StatusMovedPermanently ||
status == http.StatusTemporaryRedirect ||
status == http.StatusPermanentRedirect
}

// routerRedirectEnabled checks if the redirection is enabled on the router.
func routerRedirectEnabled(router *httptreemux.TreeMux) bool {
return (router.RedirectCleanPath || router.RedirectTrailingSlash) &&
router.RedirectBehavior != httptreemux.UseHandler
}
Loading

0 comments on commit 68300cf

Please sign in to comment.