repos / pico

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

commit
de1beb0
parent
431b315
author
Eric Bower
date
2024-03-03 14:54:31 +0000 UTC
fix(pgs): redirect to full url
3 files changed,  +52, -5
M pgs/api.go
+12, -0
 1@@ -177,6 +177,18 @@ func (h *AssetHandler) handle(w http.ResponseWriter, r *http.Request) {
 2 	status := http.StatusOK
 3 	attempts := []string{}
 4 	for _, fp := range routes {
 5+		if hasProtocol(fp.Filepath) {
 6+			h.Logger.Info(
 7+				"redirecting request",
 8+				"bucket", h.Bucket.Name,
 9+				"url", r.URL,
10+				"destination", fp.Filepath,
11+				"status", fp.Status,
12+			)
13+			http.Redirect(w, r, fp.Filepath, fp.Status)
14+			return
15+		}
16+
17 		attempts = append(attempts, fp.Filepath)
18 		mimeType := storage.GetMimeType(fp.Filepath)
19 		var c io.ReadCloser
M pgs/cal_route.go
+20, -5
 1@@ -5,6 +5,7 @@ import (
 2 	"net/http"
 3 	"path/filepath"
 4 	"regexp"
 5+	"strings"
 6 
 7 	"github.com/picosh/pico/shared"
 8 	"github.com/picosh/pico/shared/storage"
 9@@ -61,6 +62,11 @@ func expandRoute(projectName, fp string, status int) []*HttpReply {
10 	return routes
11 }
12 
13+func hasProtocol(url string) bool {
14+	isFullUrl := strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
15+	return isFullUrl
16+}
17+
18 func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpReply {
19 	notFound := &HttpReply{
20 		Filepath: filepath.Join(projectName, "404.html"),
21@@ -89,9 +95,16 @@ func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpRe
22 		match := rr.FindStringSubmatch(fp)
23 		if len(match) > 0 {
24 			userReply := []*HttpReply{}
25-			ruleRoute := shared.GetAssetFileName(&utils.FileEntry{
26-				Filepath: filepath.Join(projectName, redirect.To),
27-			})
28+			ruleRoute := redirect.To
29+
30+			// special case for redirects that include http(s)://
31+			isFullUrl := hasProtocol(redirect.To)
32+			if !isFullUrl {
33+				ruleRoute = shared.GetAssetFileName(&utils.FileEntry{
34+					Filepath: filepath.Join(projectName, redirect.To),
35+				})
36+			}
37+
38 			var rule *HttpReply
39 			if redirect.To != "" && redirect.To != "/" {
40 				rule = &HttpReply{
41@@ -102,8 +115,10 @@ func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpRe
42 				userReply = append(userReply, rule)
43 			}
44 
45-			expandedRoutes := expandRoute(projectName, redirect.To, redirect.Status)
46-			userReply = append(userReply, expandedRoutes...)
47+			if !isFullUrl {
48+				expandedRoutes := expandRoute(projectName, redirect.To, redirect.Status)
49+				userReply = append(userReply, expandedRoutes...)
50+			}
51 
52 			if redirect.Force {
53 				rts = userReply
M pgs/calc_route_test.go
+20, -0
 1@@ -147,6 +147,26 @@ func TestCalcRoutes(t *testing.T) {
 2 				{Filepath: "test/404.html", Status: 404},
 3 			},
 4 		},
 5+		{
 6+			Name: "redirectFullUrl",
 7+			Actual: calcRoutes(
 8+				"test",
 9+				"/wow",
10+				[]*RedirectRule{
11+					{
12+						From:   "/wow",
13+						To:     "https://pico.sh",
14+						Status: 301,
15+					},
16+				},
17+			),
18+			Expected: []*HttpReply{
19+				{Filepath: "test/wow.html", Status: 200},
20+				{Filepath: "test/wow/index.html", Status: 200},
21+				{Filepath: "https://pico.sh", Status: 301},
22+				{Filepath: "test/404.html", Status: 404},
23+			},
24+		},
25 	}
26 
27 	for _, fixture := range fixtures {