fix: apply API.HTTPHeaders to /webui redirect
Henrique Dias committed
Mar 17, 2023 at 15:14 UTC
63b2a0e069e4e37b0c1c36a73a3d62b452b2c7a5
2 files changed
+30
-2
core/corehttp/redirect.go
+13
-2
@@ -8,8 +8,14 @@ import (
8
)
9
10
func RedirectOption(path string, redirect string) ServeOption {
11
- handler := &redirectHandler{redirect}
11
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
12
+ cfg, err := n.Repo.Config()
13
+ if err != nil {
14
+ return nil, err
15
+ }
16
+
17
+ handler := &redirectHandler{redirect, cfg.API.HTTPHeaders}
18
+
19
if len(path) > 0 {
20
mux.Handle("/"+path+"/", handler)
21
} else {
@@ -20,9 +26,14 @@ func RedirectOption(path string, redirect string) ServeOption {
26
}
27
28
type redirectHandler struct {
23
- path string
29
+ path string
30
+ headers map[string][]string
31
}
32
33
func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
34
+ for k, v := range i.headers {
35
+ w.Header()[k] = v
36
+ }
37
+
38
http.Redirect(w, r, i.path, http.StatusFound)
39
}
test/cli/gateway_test.go
+17
@@ -219,6 +219,23 @@ func TestGateway(t *testing.T) {
219
assert.Contains(t, []int{302, 301}, resp.StatusCode)
220
})
221
222
+ t.Run("GET /webui/ returns user-specified headers", func(t *testing.T) {
223
+ t.Parallel()
224
+
225
+ header := "Access-Control-Allow-Origin"
226
+ values := []string{"http://localhost:3000", "https://webui.ipfs.io"}
227
+
228
+ node := harness.NewT(t).NewNode().Init()
229
+ node.UpdateConfig(func(cfg *config.Config) {
230
+ cfg.API.HTTPHeaders = map[string][]string{header: values}
231
+ })
232
+ node.StartDaemon()
233
+
234
+ resp := node.APIClient().DisableRedirects().Get("/webui/")
235
+ assert.Equal(t, resp.Headers.Values(header), values)
236
+ assert.Contains(t, []int{302, 301}, resp.StatusCode)
237
+ })
238
+
239
t.Run("GET /logs returns logs", func(t *testing.T) {
240
t.Parallel()
241
apiClient := node.APIClient()