Skip to content

Commit dc0de83

Browse files
committed
net/http: allow upgrading non keepalive connections
Fixes #36381 If one was using http.Transport with DisableKeepAlives and trying to upgrade a connection against net/http's Server, the Server would not allow a "Connection: Upgrade" header to be written and instead override it to "Connection: Close" which would break the handshake. This commit ensures net/http's Server does not override the connection header for successful protocol switch responses.
1 parent 3a6cd4c commit dc0de83

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

src/net/http/response.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,16 @@ func (r *Response) bodyIsWritable() bool {
352352
return ok
353353
}
354354

355-
// isProtocolSwitch reports whether r is a response to a successful
356-
// protocol upgrade.
355+
// isProtocolSwitch reports whether the response code and header
356+
// indicate a successful protocol upgrade response.
357357
func (r *Response) isProtocolSwitch() bool {
358-
return r.StatusCode == StatusSwitchingProtocols &&
359-
r.Header.Get("Upgrade") != "" &&
360-
httpguts.HeaderValuesContainsToken(r.Header["Connection"], "Upgrade")
358+
return isProtocolSwitchResponse(r.StatusCode, r.Header)
359+
}
360+
361+
// isProtocolSwitchResponse reports whether the response code and
362+
// response header indicate a successful protocol upgrade response.
363+
func isProtocolSwitchResponse(code int, h Header) bool {
364+
return code == StatusSwitchingProtocols &&
365+
h.Get("Upgrade") != "" &&
366+
httpguts.HeaderValuesContainsToken(h["Connection"], "Upgrade")
361367
}

src/net/http/serve_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6443,3 +6443,56 @@ func BenchmarkResponseStatusLine(b *testing.B) {
64436443
}
64446444
})
64456445
}
6446+
func TestDisableKeepAliveUpgrade(t *testing.T) {
6447+
if testing.Short() {
6448+
t.Skip("skipping in short mode")
6449+
}
6450+
6451+
setParallel(t)
6452+
defer afterTest(t)
6453+
6454+
s := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {
6455+
w.Header().Set("Connection", "Upgrade")
6456+
w.Header().Set("Upgrade", "someProto")
6457+
w.WriteHeader(StatusSwitchingProtocols)
6458+
c, _, err := w.(Hijacker).Hijack()
6459+
if err != nil {
6460+
return
6461+
}
6462+
defer c.Close()
6463+
6464+
io.Copy(c, c)
6465+
}))
6466+
s.Config.SetKeepAlivesEnabled(false)
6467+
s.Start()
6468+
defer s.Close()
6469+
6470+
cl := s.Client()
6471+
cl.Transport.(*Transport).DisableKeepAlives = true
6472+
6473+
resp, err := cl.Get(s.URL)
6474+
if err != nil {
6475+
t.Fatalf("failed to perform request: %v", err)
6476+
}
6477+
defer resp.Body.Close()
6478+
6479+
rwc, ok := resp.Body.(io.ReadWriteCloser)
6480+
if !ok {
6481+
t.Fatalf("Response.Body is not a io.ReadWriteCloser: %T", resp.Body)
6482+
}
6483+
6484+
_, err = rwc.Write([]byte("hello"))
6485+
if err != nil {
6486+
t.Fatalf("failed to write to body: %v", err)
6487+
}
6488+
6489+
b := make([]byte, 5)
6490+
_, err = io.ReadFull(rwc, b)
6491+
if err != nil {
6492+
t.Fatalf("failed to read from body: %v", err)
6493+
}
6494+
6495+
if string(b) != "hello" {
6496+
t.Fatalf("unexpected value read from body:\ngot: %q\nwant: %q", b, "hello")
6497+
}
6498+
}

src/net/http/server.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,13 @@ func (cw *chunkWriter) writeHeader(p []byte) {
14481448
return
14491449
}
14501450

1451-
if w.closeAfterReply && (!keepAlivesEnabled || !hasToken(cw.header.get("Connection"), "close")) {
1451+
// Only override the Connection header if it is not a successful
1452+
// protocol switch response and if KeepAlives are not enabled.
1453+
// See https://golang.org/issue/36381.
1454+
delConnectionHeader := w.closeAfterReply &&
1455+
(!keepAlivesEnabled || !hasToken(cw.header.get("Connection"), "close")) &&
1456+
!isProtocolSwitchResponse(w.status, header)
1457+
if delConnectionHeader {
14521458
delHeader("Connection")
14531459
if w.req.ProtoAtLeast(1, 1) {
14541460
setHeader.connection = "close"

0 commit comments

Comments
 (0)