Skip to content

Commit

Permalink
fix: apply API.HTTPHeaders to /webui redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias authored and galargh committed Mar 20, 2023
1 parent 010e22b commit 6cdc3c8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
15 changes: 13 additions & 2 deletions core/corehttp/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import (
)

func RedirectOption(path string, redirect string) ServeOption {
handler := &redirectHandler{redirect}
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
cfg, err := n.Repo.Config()
if err != nil {
return nil, err
}

handler := &redirectHandler{redirect, cfg.API.HTTPHeaders}

if len(path) > 0 {
mux.Handle("/"+path+"/", handler)
} else {
Expand All @@ -20,9 +26,14 @@ func RedirectOption(path string, redirect string) ServeOption {
}

type redirectHandler struct {
path string
path string
headers map[string][]string
}

func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for k, v := range i.headers {
w.Header()[k] = v
}

http.Redirect(w, r, i.path, http.StatusFound)
}
17 changes: 17 additions & 0 deletions test/cli/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ func TestGateway(t *testing.T) {
assert.Contains(t, []int{302, 301}, resp.StatusCode)
})

t.Run("GET /webui/ returns user-specified headers", func(t *testing.T) {
t.Parallel()

header := "Access-Control-Allow-Origin"
values := []string{"http://localhost:3000", "https://webui.ipfs.io"}

node := harness.NewT(t).NewNode().Init()
node.UpdateConfig(func(cfg *config.Config) {
cfg.API.HTTPHeaders = map[string][]string{header: values}
})
node.StartDaemon()

resp := node.APIClient().DisableRedirects().Get("/webui/")
assert.Equal(t, resp.Headers.Values(header), values)
assert.Contains(t, []int{302, 301}, resp.StatusCode)
})

t.Run("GET /logs returns logs", func(t *testing.T) {
t.Parallel()
apiClient := node.APIClient()
Expand Down

0 comments on commit 6cdc3c8

Please sign in to comment.