]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: attempt deadlock fix in TestDisableKeepAliveUpgrade
authorAnmol Sethi <hi@nhooyr.io>
Fri, 11 Dec 2020 23:01:12 +0000 (23:01 +0000)
committerDamien Neil <dneil@google.com>
Mon, 14 Dec 2020 19:19:09 +0000 (19:19 +0000)
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 #43073

Change-Id: I9977a18b33b8747ef847a8d11bb7b4f2d8053b8c
GitHub-Last-Rev: f809cebb139df4f5560a8456973351c95a3dfa97
GitHub-Pull-Request: golang/go#43086
Reviewed-on: https://go-review.googlesource.com/c/go/+/276375
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Trust: Dmitri Shuralyov <dmitshur@golang.org>

src/net/http/response.go
src/net/http/serve_test.go
src/net/http/transport.go

index b95abae646b10a9c9549a142cdb79cb57d48d35d..b8985da3c80fc413ef30f640bbd8fd282e65465e 100644 (file)
@@ -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")
 }
index b1bf8e6c5e808824fc742d4f4156dc80b2d11aef..95e6bf4adbaeec057324b96b73f00b9357439c47 100644 (file)
@@ -6481,6 +6481,10 @@ func TestDisableKeepAliveUpgrade(t *testing.T) {
        }
        defer resp.Body.Close()
 
+       if resp.StatusCode != 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)
index a5830703afc56c1a08d408ad7d395a3bce5688d0..6358c3897ec4774c20e496b6f71ee3cd60e71942 100644 (file)
@@ -2566,7 +2566,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")
        }