Skip to content

Commit

Permalink
fix(pgs): forward query params for rewrites and redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Dec 28, 2024
1 parent c728ab4 commit 10035cd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
21 changes: 11 additions & 10 deletions pgs/web_asset_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
status := http.StatusOK
attempts := []string{}
for _, fp := range routes {
destUrl, err := url.Parse(fp.Filepath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
destUrl.RawQuery = r.URL.RawQuery

if checkIsRedirect(fp.Status) {
// hack: check to see if there's an index file in the requested directory
// before redirecting, this saves a hop that will just end up a 404
Expand All @@ -86,17 +93,17 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
logger.Info(
"redirecting request",
"destination", fp.Filepath,
"destination", destUrl.String(),
"status", fp.Status,
)
http.Redirect(w, r, fp.Filepath, fp.Status)
http.Redirect(w, r, destUrl.String(), fp.Status)
return
} else if hasProtocol(fp.Filepath) {
if !h.HasPicoPlus {
msg := "must be pico+ user to fetch content from external source"
logger.Error(
msg,
"destination", fp.Filepath,
"destination", destUrl.String(),
"status", fp.Status,
)
http.Error(w, msg, http.StatusUnauthorized)
Expand All @@ -105,15 +112,10 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

logger.Info(
"fetching content from external service",
"destination", fp.Filepath,
"destination", destUrl.String(),
"status", fp.Status,
)

destUrl, err := url.Parse(fp.Filepath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
proxy := httputil.NewSingleHostReverseProxy(destUrl)
oldDirector := proxy.Director
proxy.Director = func(r *http.Request) {
Expand All @@ -134,7 +136,6 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mimeType := storage.GetMimeType(fp.Filepath)
logger = logger.With("filename", fp.Filepath)
var c io.ReadCloser
var err error
if strings.HasPrefix(mimeType, "image/") {
c, contentType, err = h.Storage.ServeObject(
h.Bucket,
Expand Down
27 changes: 27 additions & 0 deletions pgs/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ApiExample struct {
name string
path string
want string
wantUrl string
status int
contentType string

Expand Down Expand Up @@ -210,6 +211,22 @@ func TestApiBasic(t *testing.T) {
},
},
},
{
name: "redirects-query-param",
path: "/anything?query=param",
want: `<a href="/about.html?query=param">Moved Permanently</a>.`,
wantUrl: "/about.html?query=param",
status: http.StatusMovedPermanently,
contentType: "text/html; charset=utf-8",

dbpool: NewPgsDb(cfg.Logger),
storage: map[string]map[string]string{
bucketName: {
"test/_redirects": "/anything /about.html 301",
"test/about.html": "hello world!",
},
},
},
}

for _, tc := range tt {
Expand All @@ -234,6 +251,16 @@ func TestApiBasic(t *testing.T) {
if body != tc.want {
t.Errorf("Want '%s', got '%s'", tc.want, body)
}

if tc.wantUrl != "" {
location, err := responseRecorder.Result().Location()
if err != nil {
t.Errorf("err: %s", err.Error())
}
if tc.wantUrl != location.String() {
t.Errorf("Want '%s', got '%s'", tc.wantUrl, location.String())
}
}
})
}
}
Expand Down

0 comments on commit 10035cd

Please sign in to comment.