repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

commit
10035cd
parent
c728ab4
author
Eric Bower
date
2024-12-28 15:00:22 -0500 EST
fix(pgs): forward query params for rewrites and redirects
2 files changed,  +38, -10
M pgs/web_asset_handler.go
+11, -10
 1@@ -74,6 +74,13 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 2 	status := http.StatusOK
 3 	attempts := []string{}
 4 	for _, fp := range routes {
 5+		destUrl, err := url.Parse(fp.Filepath)
 6+		if err != nil {
 7+			http.Error(w, err.Error(), http.StatusInternalServerError)
 8+			return
 9+		}
10+		destUrl.RawQuery = r.URL.RawQuery
11+
12 		if checkIsRedirect(fp.Status) {
13 			// hack: check to see if there's an index file in the requested directory
14 			// before redirecting, this saves a hop that will just end up a 404
15@@ -86,17 +93,17 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
16 			}
17 			logger.Info(
18 				"redirecting request",
19-				"destination", fp.Filepath,
20+				"destination", destUrl.String(),
21 				"status", fp.Status,
22 			)
23-			http.Redirect(w, r, fp.Filepath, fp.Status)
24+			http.Redirect(w, r, destUrl.String(), fp.Status)
25 			return
26 		} else if hasProtocol(fp.Filepath) {
27 			if !h.HasPicoPlus {
28 				msg := "must be pico+ user to fetch content from external source"
29 				logger.Error(
30 					msg,
31-					"destination", fp.Filepath,
32+					"destination", destUrl.String(),
33 					"status", fp.Status,
34 				)
35 				http.Error(w, msg, http.StatusUnauthorized)
36@@ -105,15 +112,10 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
37 
38 			logger.Info(
39 				"fetching content from external service",
40-				"destination", fp.Filepath,
41+				"destination", destUrl.String(),
42 				"status", fp.Status,
43 			)
44 
45-			destUrl, err := url.Parse(fp.Filepath)
46-			if err != nil {
47-				http.Error(w, err.Error(), http.StatusInternalServerError)
48-				return
49-			}
50 			proxy := httputil.NewSingleHostReverseProxy(destUrl)
51 			oldDirector := proxy.Director
52 			proxy.Director = func(r *http.Request) {
53@@ -134,7 +136,6 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
54 		mimeType := storage.GetMimeType(fp.Filepath)
55 		logger = logger.With("filename", fp.Filepath)
56 		var c io.ReadCloser
57-		var err error
58 		if strings.HasPrefix(mimeType, "image/") {
59 			c, contentType, err = h.Storage.ServeObject(
60 				h.Bucket,
M pgs/web_test.go
+27, -0
 1@@ -23,6 +23,7 @@ type ApiExample struct {
 2 	name        string
 3 	path        string
 4 	want        string
 5+	wantUrl     string
 6 	status      int
 7 	contentType string
 8 
 9@@ -210,6 +211,22 @@ func TestApiBasic(t *testing.T) {
10 				},
11 			},
12 		},
13+		{
14+			name:        "redirects-query-param",
15+			path:        "/anything?query=param",
16+			want:        `<a href="/about.html?query=param">Moved Permanently</a>.`,
17+			wantUrl:     "/about.html?query=param",
18+			status:      http.StatusMovedPermanently,
19+			contentType: "text/html; charset=utf-8",
20+
21+			dbpool: NewPgsDb(cfg.Logger),
22+			storage: map[string]map[string]string{
23+				bucketName: {
24+					"test/_redirects": "/anything /about.html 301",
25+					"test/about.html": "hello world!",
26+				},
27+			},
28+		},
29 	}
30 
31 	for _, tc := range tt {
32@@ -234,6 +251,16 @@ func TestApiBasic(t *testing.T) {
33 			if body != tc.want {
34 				t.Errorf("Want '%s', got '%s'", tc.want, body)
35 			}
36+
37+			if tc.wantUrl != "" {
38+				location, err := responseRecorder.Result().Location()
39+				if err != nil {
40+					t.Errorf("err: %s", err.Error())
41+				}
42+				if tc.wantUrl != location.String() {
43+					t.Errorf("Want '%s', got '%s'", tc.wantUrl, location.String())
44+				}
45+			}
46 		})
47 	}
48 }