From e3ce3b94c519bd0bc7591ea15f5ebc088444c88c Mon Sep 17 00:00:00 2001 From: Eric Bower Date: Wed, 25 Dec 2024 13:51:00 -0500 Subject: [PATCH] fix(pgs): wildcard with splat suffix --- pgs/calc_route.go | 12 ++++++++++-- pgs/calc_route_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/pgs/calc_route.go b/pgs/calc_route.go index 66abbd78..d5fc710f 100644 --- a/pgs/calc_route.go +++ b/pgs/calc_route.go @@ -86,7 +86,7 @@ func correlatePlaceholder(orig, pattern string) (string, string) { if strings.HasPrefix(item, ":") { nextList = append(nextList, origList[idx]) } else if strings.Contains(item, "*") { - nextList = append(nextList, strings.ReplaceAll(item, "*", "(.+)")) + nextList = append(nextList, strings.ReplaceAll(item, "*", "(.*)")) } else if item == origList[idx] { nextList = append(nextList, origList[idx]) } @@ -151,16 +151,24 @@ func genRedirectRoute(actual string, fromStr string, to string) string { if len(item) > 1 { ls = actualList[idx+1:] } + // standalone splat splat := strings.Join(ls, "/") mapper[":splat"] = splat + + // splat as a suffix to a string + place := strings.ReplaceAll(item, "*", ":splat") + mapper[place] = strings.Join(actualList[idx:], "/") break } } fin := []string{"/"} + fmt.Println(toList) for _, item := range toList { - if item == ":splat" { + fmt.Println(item) + if strings.HasSuffix(item, ":splat") { + fmt.Println(mapper[item]) fin = append(fin, mapper[item]) } else if mapper[item] != "" { fin = append(fin, mapper[item]) diff --git a/pgs/calc_route_test.go b/pgs/calc_route_test.go index 54e7e076..cf2af1ec 100644 --- a/pgs/calc_route_test.go +++ b/pgs/calc_route_test.go @@ -512,6 +512,44 @@ func TestCalcRoutes(t *testing.T) { {Filepath: "https://super.fly.sh/super/ficial.html", Status: 302}, }, }, + { + Name: "well-known-splat-suffix", + Actual: calcRoutes( + "public", + "/.well-known/nodeinfo", + []*RedirectRule{ + { + From: "/.well-known/nodeinfo*", + To: "https://some.dev/.well-known/nodeinfo:splat", + Status: 301, + }, + }, + ), + Expected: []*HttpReply{ + {Filepath: "public/.well-known/nodeinfo", Status: 200}, + {Filepath: "public/.well-known/nodeinfo.html", Status: 200}, + {Filepath: "https://some.dev/.well-known/nodeinfo", Status: 301}, + }, + }, + { + Name: "wildcard-query-param", + Actual: calcRoutes( + "public", + "/.well-known/webfinger?query=nice", + []*RedirectRule{ + { + From: "/.well-known/webfinger*", + To: "https://some.dev/.well-known/webfinger:splat", + Status: 301, + }, + }, + ), + Expected: []*HttpReply{ + {Filepath: "public/.well-known/webfinger?query=nice", Status: 200}, + {Filepath: "public/.well-known/webfinger?query=nice.html", Status: 200}, + {Filepath: "https://some.dev/.well-known/webfinger?query=nice", Status: 301}, + }, + }, } for _, fixture := range fixtures {