From 50b16f9de590822a04ec8d6cbac476366c1bde32 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sat, 24 Oct 2020 00:40:41 +0000 Subject: net/http: allow upgrading non keepalive connections 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 change ensures net/http's Server does not override the connection header for successful protocol switch responses. Fixes #36381. Change-Id: I882aad8539e6c87ff5f37c20e20b3a7fa1a30357 GitHub-Last-Rev: dc0de83201dc26236527b68bd49dffc53dd0389b GitHub-Pull-Request: golang/go#36382 Reviewed-on: https://go-review.googlesource.com/c/go/+/213277 Trust: Emmanuel Odeke Trust: Brad Fitzpatrick Reviewed-by: Brad Fitzpatrick --- src/net/http/response.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src/net/http/response.go') diff --git a/src/net/http/response.go b/src/net/http/response.go index 72812f0642..b95abae646 100644 --- a/src/net/http/response.go +++ b/src/net/http/response.go @@ -352,10 +352,16 @@ func (r *Response) bodyIsWritable() bool { return ok } -// isProtocolSwitch reports whether r is a response to a successful -// protocol upgrade. +// isProtocolSwitch reports whether the response code and header +// indicate a successful protocol upgrade response. func (r *Response) isProtocolSwitch() bool { - return r.StatusCode == StatusSwitchingProtocols && - r.Header.Get("Upgrade") != "" && - httpguts.HeaderValuesContainsToken(r.Header["Connection"], "Upgrade") + return isProtocolSwitchResponse(r.StatusCode, r.Header) +} + +// 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") != "" && + httpguts.HeaderValuesContainsToken(h["Connection"], "Upgrade") } -- cgit v1.3