Skip to content

Commit

Permalink
Merge pull request #6001 from influxdata/6000/base_path_fix
Browse files Browse the repository at this point in the history
fix: repair UI served under BASEPATH
  • Loading branch information
sranka authored Sep 13, 2022
2 parents 88e037c + 693fcf9 commit 406065e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Bug Fixes

1. [#6001](https://github.com/influxdata/chronograf/pull/6001): Repair UI served under BASEPATH.

### Other

## v1.10.0 [2022-08-23]
Expand Down
6 changes: 2 additions & 4 deletions server/url_prefixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (up *URLPrefixer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
return
}

isSVG, _ := regexp.Match(".svg$", []byte(r.URL.String()))
if isSVG {
// do not process JS or SVG files, it only harms them
if isIgnored, _ := regexp.Match("\\.(svg|js)$", []byte(r.URL.String())); isIgnored {
up.Next.ServeHTTP(rw, r)
return
}
Expand Down Expand Up @@ -186,8 +186,6 @@ func NewDefaultURLPrefixer(prefix string, next http.Handler, lg chronograf.Logge
[]byte(`src="`),
[]byte(`href="`),
[]byte(`url(`),
[]byte(`new Worker("`),
[]byte(`new Worker('`),
[]byte(`data-basepath="`), // for forwarding basepath to frontend
},
}
Expand Down
34 changes: 34 additions & 0 deletions server/url_prefixer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,37 @@ func Test_Server_Prefixer_NoPrefixingWithoutFlusther(t *testing.T) {
t.Error("No Flusher", ":\n Expected Error Message: \"", server.ErrNotFlusher, "\" but saw none. Msgs:", tl.Messages)
}
}

func Test_Server_Prefixer_IgnoreJsAndSvg(t *testing.T) {
expected := "Keep it the same please"
backend := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, expected)
})

tl := &mocks.TestLogger{}
pfx := &server.URLPrefixer{
Prefix: " error",
Next: backend,
Logger: tl,
Attrs: [][]byte{
[]byte("same"),
},
}

ts := httptest.NewServer(pfx)
defer ts.Close()

for _, fileName := range []string{"/test.js", "/test.svg"} {
res, err := http.Get(ts.URL + fileName)
if err != nil {
t.Fatal("Unexpected error fetching from prefixer: err:", err)
}
actual, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal("Unable to read prefixed body: err:", err)
}
if string(actual) != expected {
t.Error("Prefixing changed content of ", fileName, ":\n\t\tWant:\n", expected, "\n\t\tGot:\n", string(actual))
}
}
}

0 comments on commit 406065e

Please sign in to comment.