Skip to content

Commit

Permalink
contrib/dimfeld/httptreemux.v5: fix route and name for 30X redirects (#…
Browse files Browse the repository at this point in the history
…2685)

Co-authored-by: De Ville Weppenaar <[email protected]>
  • Loading branch information
darccio and devillecodes authored May 8, 2024
1 parent 3a426ca commit 7f6597f
Show file tree
Hide file tree
Showing 4 changed files with 616 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ jobs:
ports:
- 2181:2181
kafka:
image: wurstmeister/kafka:2.13-2.8.1
image: darccio/kafka:2.13-2.8.1
env:
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
Expand Down
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 7f6597f

Please sign in to comment.