mirror of
https://github.com/golang/go
synced 2024-11-12 06:30:21 -07:00
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: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
a101b85e00
commit
6a6c1d9841
@ -90,6 +90,10 @@ func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
|
||||
} else {
|
||||
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}
|
||||
}
|
||||
|
@ -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 {
|
||||
get func() []byte
|
||||
put func([]byte)
|
||||
|
Loading…
Reference in New Issue
Block a user