Skip to content

Commit

Permalink
net/http/httputil: copy response header back to http.ResponseWriter i…
Browse files Browse the repository at this point in the history
…n ReverseProxy.handleUpgradeResponse

Fixes: #41634

Change-Id: Ib78cc37a4d2ca0753d567eafb616238e4103484e
Reviewed-on: https://go-review.googlesource.com/c/go/+/257777
Reviewed-by: Damien Neil <dneil@google.com>
Trust: Damien Neil <dneil@google.com>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Damien Neil <dneil@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
  • Loading branch information
aofei authored and neild committed Oct 19, 2020
1 parent 2a206c7 commit 9c017ff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/net/http/httputil/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,6 @@ func (p *ReverseProxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.R
return
}

copyHeader(res.Header, rw.Header())

hj, ok := rw.(http.Hijacker)
if !ok {
p.getErrorHandler()(rw, req, fmt.Errorf("can't switch protocols using non-Hijacker ResponseWriter type %T", rw))
Expand Down Expand Up @@ -581,6 +579,10 @@ func (p *ReverseProxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.R
return
}
defer conn.Close()

copyHeader(rw.Header(), res.Header)

res.Header = rw.Header()
res.Body = nil // so res.Write only writes the headers; we have res.Body in backConn above
if err := res.Write(brw); err != nil {
p.getErrorHandler()(rw, req, fmt.Errorf("response write: %v", err))
Expand Down
3 changes: 3 additions & 0 deletions src/net/http/httputil/reverseproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,9 @@ func TestReverseProxyWebSocket(t *testing.T) {
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("X-Header", "X-Value")
rproxy.ServeHTTP(rw, req)
if got, want := rw.Header().Get("X-Modified"), "true"; got != want {
t.Errorf("response writer X-Modified header = %q; want %q", got, want)
}
})

frontendProxy := httptest.NewServer(handler)
Expand Down

0 comments on commit 9c017ff

Please sign in to comment.