From 0af4ea3074310e009b118427d03474f943d8b4cd Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Wed, 18 Dec 2024 14:09:17 -0300 Subject: [PATCH] fix(router): trailing slash in mux rest pattern --- lib/router/router.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/router/router.go b/lib/router/router.go index fb33977..9f01999 100644 --- a/lib/router/router.go +++ b/lib/router/router.go @@ -197,9 +197,24 @@ func (r *defaultRouter) parsePath(p string) (method, host, pth string) { // first split it between "[METHOD ][HOST]" and "[PATH]" ps := strings.Split(p, "/") + pth = path.Join("/", strings.Join(ps[1:], "/")) + + // path.Join adds a trailing slash, if the original path doesn't has one, the parsed + // path shouldn't also + if !strings.HasSuffix(p, "/") { + pth = strings.TrimSuffix(pth, "/") + } + + // Since path.Join adds a trailing slash, it can break the {pattern...} syntax. + // So we check if it has the suffix "...}/" to see if it ends in "/{pattern...}/" + if strings.HasSuffix(pth, "...}/") { + // If it does, we remove the any possible trailing slash + pth = strings.TrimSuffix(pth, "/") + } + // If "[METHOD ][HOST]" is empty, we just have the path and can send it back if ps[0] == "" { - return "", "", path.Join("/", strings.Join(ps[1:], "/")) + return "", "", pth } // Split string again, if method is not defined, this will end up being just []string{"[HOST]"} @@ -209,8 +224,8 @@ func (r *defaultRouter) parsePath(p string) (method, host, pth string) { // If slice is of length 1, this means it is []string{"[HOST]"} if len(mh) == 1 { - return "", host, path.Join("/", strings.Join(ps[1:], "/")) + return "", host, pth } - return mh[0], mh[1], path.Join("/", strings.Join(ps[1:], "/")) + return mh[0], mh[1], pth }