aboutsummaryrefslogtreecommitdiff
path: root/src/net/http/response.go
diff options
context:
space:
mode:
authorAnmol Sethi <hi@nhooyr.io>2020-10-24 00:40:41 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2020-12-01 19:47:12 +0000
commit50b16f9de590822a04ec8d6cbac476366c1bde32 (patch)
treefb46c24ff85b7588e475de44092b1284f58c46fa /src/net/http/response.go
parent212d385a2f723a8dd5e7d2e83efb478ddd139349 (diff)
downloadgo-50b16f9de590822a04ec8d6cbac476366c1bde32.tar.xz
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 <emmanuel@orijtech.com> Trust: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/net/http/response.go')
-rw-r--r--src/net/http/response.go16
1 files changed, 11 insertions, 5 deletions
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")
}