diff options
author | Sina Siadat <siadat@gmail.com> | 2016-09-04 12:20:14 +0430 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-09-08 04:37:36 +0000 |
commit | 24d8f3fa4b02784af2419eec8a28aee303aae0c5 (patch) | |
tree | 89c817b910d562bd416c70cc5345bfe50689c51d /src/net/http/httputil/reverseproxy.go | |
parent | b6f44923c0f88eb36816d90fb8fff2fd78422df5 (diff) | |
download | go-24d8f3fa4b02784af2419eec8a28aee303aae0c5.tar.gz go-24d8f3fa4b02784af2419eec8a28aee303aae0c5.zip |
net/http/httputil: copy header map if necessary in ReverseProxy
We were already making a copy of the map before removing
hop-by-hop headers. This commit does the same for proxied
headers mentioned in the "Connection" header.
A test is added to ensure request headers are not modified.
Updates #16875
Change-Id: I85329d212787958d5ad818915eb0538580a4653a
Reviewed-on: https://go-review.googlesource.com/28493
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/net/http/httputil/reverseproxy.go')
-rw-r--r-- | src/net/http/httputil/reverseproxy.go | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/net/http/httputil/reverseproxy.go b/src/net/http/httputil/reverseproxy.go index 47cd0ae97d..2b38e0fdd8 100644 --- a/src/net/http/httputil/reverseproxy.go +++ b/src/net/http/httputil/reverseproxy.go @@ -152,11 +152,20 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { p.Director(outreq) outreq.Close = false + // We are modifying the same underlying map from req (shallow + // copied above) so we only copy it if necessary. + copiedHeaders := false + // Remove headers with the same name as the connection-tokens. // See RFC 2616, section 14.10. if c := outreq.Header.Get("Connection"); c != "" { for _, f := range strings.Split(c, ",") { if f = strings.TrimSpace(f); f != "" { + if !copiedHeaders { + outreq.Header = make(http.Header) + copyHeader(outreq.Header, req.Header) + copiedHeaders = true + } outreq.Header.Del(f) } } @@ -164,10 +173,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { // Remove hop-by-hop headers to the backend. Especially // important is "Connection" because 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. - copiedHeaders := false + // connection, regardless of what the client sent to us. for _, h := range hopHeaders { if outreq.Header.Get(h) != "" { if !copiedHeaders { |