repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
b671467
parent
8c14725
author
Eric Bower
date
2024-10-13 04:46:18 +0000 UTC
fix(pgs): various _redirects fixes with wildcards and splats
2 files changed,  +58, -3
M pgs/calc_route.go
+11, -3
 1@@ -85,6 +85,8 @@ func correlatePlaceholder(orig, pattern string) (string, string) {
 2 
 3 		if strings.HasPrefix(item, ":") {
 4 			nextList = append(nextList, origList[idx])
 5+		} else if strings.Contains(item, "*") {
 6+			nextList = append(nextList, strings.ReplaceAll(item, "*", "(.+)"))
 7 		} else if item == origList[idx] {
 8 			nextList = append(nextList, origList[idx])
 9 		}
10@@ -135,15 +137,21 @@ func genRedirectRoute(actual string, fromStr string, to string) string {
11 
12 	mapper := map[string]string{}
13 	for idx, item := range fromList {
14-		if len(actualList) <= idx {
15+		if len(actualList) < idx {
16 			continue
17 		}
18 
19 		if strings.HasPrefix(item, ":") {
20 			mapper[item] = actualList[idx]
21 		}
22-		if item == "*" {
23-			splat := strings.Join(actualList[idx:], "/")
24+		if strings.HasSuffix(item, "*") {
25+			ls := actualList[idx:]
26+			// if the * is part of other text in the segment (e.g. `/files*`)
27+			// then we don't want to include "files" in the destination
28+			if len(item) > 1 {
29+				ls = actualList[idx+1:]
30+			}
31+			splat := strings.Join(ls, "/")
32 			mapper[":splat"] = splat
33 			break
34 		}
M pgs/calc_route_test.go
+47, -0
 1@@ -465,6 +465,53 @@ func TestCalcRoutes(t *testing.T) {
 2 				{Filepath: "public/404.html", Status: 404},
 3 			},
 4 		},
 5+		{
 6+			Name: "wildcard-with-word",
 7+			Actual: calcRoutes(
 8+				"public",
 9+				"/pictures/soup",
10+				[]*RedirectRule{
11+					{
12+						From:   "/pictures*",
13+						To:     "https://super.fly.sh/:splat",
14+						Status: 200,
15+					},
16+					{
17+						From:   "/*",
18+						To:     "https://super.fly.sh/:splat",
19+						Status: 302,
20+					},
21+				},
22+			),
23+			Expected: []*HttpReply{
24+				{Filepath: "public/pictures/soup", Status: 200},
25+				{Filepath: "public/pictures/soup.html", Status: 200},
26+				{Filepath: "https://super.fly.sh/soup", Status: 200},
27+			},
28+		},
29+		{
30+			Name: "wildcard-multiple",
31+			Actual: calcRoutes(
32+				"public",
33+				"/super/ficial.html",
34+				[]*RedirectRule{
35+					{
36+						From:   "/pictures*",
37+						To:     "https://super.fly.sh/:splat",
38+						Status: 200,
39+					},
40+					{
41+						From:   "/*",
42+						To:     "https://super.fly.sh/:splat",
43+						Status: 302,
44+					},
45+				},
46+			),
47+			Expected: []*HttpReply{
48+				{Filepath: "public/super/ficial.html", Status: 200},
49+				{Filepath: "https://super.fly.sh/super/ficial.html", Status: 302},
50+			},
51+		},
52 	}
53 
54 	for _, fixture := range fixtures {