Skip to content

Commit

Permalink
fix(pgs): wildcard with splat suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Dec 25, 2024
1 parent 6a7c21b commit e3ce3b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pgs/calc_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down Expand Up @@ -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])
Expand Down
38 changes: 38 additions & 0 deletions pgs/calc_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e3ce3b9

Please sign in to comment.