mirror of
https://github.com/golang/go
synced 2024-11-20 06:44:40 -07:00
http: remove Connection header in ReverseProxy
Fixes #2342 R=golang-dev, adg CC=golang-dev https://golang.org/cl/5302057
This commit is contained in:
parent
9c012899a9
commit
7c5d90dfbd
@ -69,6 +69,14 @@ func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
|
||||
return &ReverseProxy{Director: director}
|
||||
}
|
||||
|
||||
func copyHeader(dst, src Header) {
|
||||
for k, vv := range src {
|
||||
for _, v := range vv {
|
||||
dst.Add(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ReverseProxy) ServeHTTP(rw ResponseWriter, req *Request) {
|
||||
transport := p.Transport
|
||||
if transport == nil {
|
||||
@ -84,6 +92,16 @@ func (p *ReverseProxy) ServeHTTP(rw ResponseWriter, req *Request) {
|
||||
outreq.ProtoMinor = 1
|
||||
outreq.Close = false
|
||||
|
||||
// Remove the connection header to the backend. We want a
|
||||
// persistent connection, regardless of what the client sent
|
||||
// to us. This is modifying the same underlying map from req
|
||||
// (shallow copied above) so we only copy it if necessary.
|
||||
if outreq.Header.Get("Connection") != "" {
|
||||
outreq.Header = make(Header)
|
||||
copyHeader(outreq.Header, req.Header)
|
||||
outreq.Header.Del("Connection")
|
||||
}
|
||||
|
||||
if clientIp, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
||||
outreq.Header.Set("X-Forwarded-For", clientIp)
|
||||
}
|
||||
@ -95,12 +113,7 @@ func (p *ReverseProxy) ServeHTTP(rw ResponseWriter, req *Request) {
|
||||
return
|
||||
}
|
||||
|
||||
hdr := rw.Header()
|
||||
for k, vv := range res.Header {
|
||||
for _, v := range vv {
|
||||
hdr.Add(k, v)
|
||||
}
|
||||
}
|
||||
copyHeader(rw.Header(), res.Header)
|
||||
|
||||
rw.WriteHeader(res.StatusCode)
|
||||
|
||||
|
@ -24,6 +24,9 @@ func TestReverseProxy(t *testing.T) {
|
||||
if r.Header.Get("X-Forwarded-For") == "" {
|
||||
t.Errorf("didn't get X-Forwarded-For header")
|
||||
}
|
||||
if c := r.Header.Get("Connection"); c != "" {
|
||||
t.Errorf("handler got Connection header value %q", c)
|
||||
}
|
||||
if g, e := r.Host, "some-name"; g != e {
|
||||
t.Errorf("backend got Host header %q, want %q", g, e)
|
||||
}
|
||||
@ -43,6 +46,8 @@ func TestReverseProxy(t *testing.T) {
|
||||
|
||||
getReq, _ := NewRequest("GET", frontend.URL, nil)
|
||||
getReq.Host = "some-name"
|
||||
getReq.Header.Set("Connection", "close")
|
||||
getReq.Close = true
|
||||
res, err := DefaultClient.Do(getReq)
|
||||
if err != nil {
|
||||
t.Fatalf("Get: %v", err)
|
||||
|
Loading…
Reference in New Issue
Block a user