Skip to content

Commit

Permalink
Merge pull request #18118 from influxdata/bb/fix-reverse-proxy
Browse files Browse the repository at this point in the history
fix(kit/feature): Ensure host is overridden as a workaround for stdlib bug.
  • Loading branch information
brettbuddin authored May 15, 2020
2 parents 9770d0a + 40d833f commit 35c1583
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions kit/feature/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ func newReverseProxy(dest *url.URL, enablerKey string) *httputil.ReverseProxy {
defaultDirector := proxy.Director
proxy.Director = func(r *http.Request) {
defaultDirector(r)

r.Header.Set(headerProxyFlag, enablerKey)

// Override r.Host to prevent us sending this request back to ourselves.
// A bug in the stdlib causes this value to be preferred over the
// r.URL.Host (which is set in the default Director) if r.Host isn't
// empty (which it isn't).
// https://github.com/golang/go/issues/28168
r.Host = dest.Host
}
proxy.ModifyResponse = func(r *http.Response) error {
r.Header.Set(headerProxyFlag, enablerKey)
Expand Down
4 changes: 2 additions & 2 deletions kit/feature/http_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestHTTPProxy_RequestHeader(t *testing.T) {
proxy := NewHTTPProxy(sURL, logger, en)

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "http://example.com/foo", nil)
r := httptest.NewRequest(http.MethodGet, "http://example.com/foo", nil)
srcHandler(proxy)(w, r)
}

Expand All @@ -104,7 +104,7 @@ func testHTTPProxy(logger *zap.Logger, enabler ProxyEnabler) (*http.Response, er
proxy := NewHTTPProxy(sURL, logger, enabler)

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "http://example.com/foo", nil)
r := httptest.NewRequest(http.MethodGet, "http://example.com/foo", nil)
srcHandler(proxy)(w, r)

return w.Result(), nil
Expand Down

0 comments on commit 35c1583

Please sign in to comment.