Skip to content

Commit

Permalink
net/http: attempt deadlock fix in TestDisableKeepAliveUpgrade
Browse files Browse the repository at this point in the history
1. The test now checks the response status code.
2. The transport has been changed to not set "Connection: Close" if
   DisableKeepAlive is set and the request is a HTTP/1.1 protocol
   upgrade.

Updates golang#43073
  • Loading branch information
nhooyr committed Dec 11, 2020
1 parent 31496cf commit fa61250
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/net/http/response.go
Expand Up @@ -361,7 +361,12 @@ func (r *Response) isProtocolSwitch() bool {
// isProtocolSwitchResponse reports whether the response code and
// response header indicate a successful protocol upgrade response.
func isProtocolSwitchResponse(code int, h Header) bool {
return code == StatusSwitchingProtocols &&
h.Get("Upgrade") != "" &&
return code == StatusSwitchingProtocols && isProtocolSwitchHeader(h)
}

// isProtocolSwitchHeader reports whether the request or response header
// is for a protocol switch.
func isProtocolSwitchHeader(h Header) bool {
return h.Get("Upgrade") != "" &&
httpguts.HeaderValuesContainsToken(h["Connection"], "Upgrade")
}
4 changes: 4 additions & 0 deletions src/net/http/serve_test.go
Expand Up @@ -6481,6 +6481,10 @@ func TestDisableKeepAliveUpgrade(t *testing.T) {
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusSwitchingProtocols {
t.Fatalf("unexpected status code: %v", resp.StatusCode)
}

rwc, ok := resp.Body.(io.ReadWriteCloser)
if !ok {
t.Fatalf("Response.Body is not a io.ReadWriteCloser: %T", resp.Body)
Expand Down
4 changes: 3 additions & 1 deletion src/net/http/transport.go
Expand Up @@ -2564,7 +2564,9 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
continueCh = make(chan struct{}, 1)
}

if pc.t.DisableKeepAlives && !req.wantsClose() {
if pc.t.DisableKeepAlives &&
!req.wantsClose() &&
!isProtocolSwitchHeader(req.Header) {
req.extraHeaders().Set("Connection", "close")
}

Expand Down

0 comments on commit fa61250

Please sign in to comment.