Commit 6a6c1d98 authored by Artyom Pervukhin's avatar Artyom Pervukhin Committed by Brad Fitzpatrick

net/http/httputil: don't add User-Agent header by proxy made with NewSingleHostReverseProxy

If client does not provided User-Agent header, do not set default one
used by net/http package when doing request to backend.

Fixes #15524

Change-Id: I9a46bb3b7ec106bc7c3071e235b872d279994d67
Reviewed-on: https://go-review.googlesource.com/23089
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent a101b85e
...@@ -90,6 +90,10 @@ func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy { ...@@ -90,6 +90,10 @@ func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
} else { } else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
} }
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
} }
return &ReverseProxy{Director: director} return &ReverseProxy{Director: director}
} }
......
...@@ -348,6 +348,49 @@ func TestNilBody(t *testing.T) { ...@@ -348,6 +348,49 @@ func TestNilBody(t *testing.T) {
} }
} }
// Issue 15524
func TestUserAgentHeader(t *testing.T) {
const explicitUA = "explicit UA"
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/noua" {
if c := r.Header.Get("User-Agent"); c != "" {
t.Errorf("handler got non-empty User-Agent header %q", c)
}
return
}
if c := r.Header.Get("User-Agent"); c != explicitUA {
t.Errorf("handler got unexpected User-Agent header %q", c)
}
}))
defer backend.Close()
backendURL, err := url.Parse(backend.URL)
if err != nil {
t.Fatal(err)
}
proxyHandler := NewSingleHostReverseProxy(backendURL)
proxyHandler.ErrorLog = log.New(ioutil.Discard, "", 0) // quiet for tests
frontend := httptest.NewServer(proxyHandler)
defer frontend.Close()
getReq, _ := http.NewRequest("GET", frontend.URL, nil)
getReq.Header.Set("User-Agent", explicitUA)
getReq.Close = true
res, err := http.DefaultClient.Do(getReq)
if err != nil {
t.Fatalf("Get: %v", err)
}
res.Body.Close()
getReq, _ = http.NewRequest("GET", frontend.URL+"/noua", nil)
getReq.Header.Set("User-Agent", "")
getReq.Close = true
res, err = http.DefaultClient.Do(getReq)
if err != nil {
t.Fatalf("Get: %v", err)
}
res.Body.Close()
}
type bufferPool struct { type bufferPool struct {
get func() []byte get func() []byte
put func([]byte) put func([]byte)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment