fix(kit/feature): Ensure host is overridden as a workaround for stdlib bug.

pull/18118/head
Brett Buddin 2020-05-15 15:50:17 -04:00
parent 9770d0ac44
commit 40d833f153
No known key found for this signature in database
GPG Key ID: C51265E441C4C5AC
2 changed files with 10 additions and 2 deletions

View File

@ -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)

View File

@ -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)
}
@ -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