From e4ce40f8ff0424054045de2461fc7228c66f3c61 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 11 Apr 2022 14:49:56 -0400 Subject: reverseproxy: Sync up `handleUpgradeResponse` with stdlib (#4664) * reverseproxy: Sync up `handleUpgradeResponse` with stdlib I had left this as a TODO for when we bump to minimum 1.17, but I should've realized it was under `internal` so it couldn't be used directly. Copied the functions we needed for parity. Hopefully this is ok! * Add tests and fix godoc comments Co-authored-by: Matthew Holt --- modules/caddyhttp/reverseproxy/streaming.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'modules/caddyhttp/reverseproxy/streaming.go') diff --git a/modules/caddyhttp/reverseproxy/streaming.go b/modules/caddyhttp/reverseproxy/streaming.go index 1db352b..6bd1af2 100644 --- a/modules/caddyhttp/reverseproxy/streaming.go +++ b/modules/caddyhttp/reverseproxy/streaming.go @@ -32,10 +32,15 @@ import ( func (h Handler) handleUpgradeResponse(logger *zap.Logger, rw http.ResponseWriter, req *http.Request, res *http.Response) { reqUpType := upgradeType(req.Header) resUpType := upgradeType(res.Header) - // TODO: Update to use "net/http/internal/ascii" once we bumped - // the minimum Go version to 1.17. - // See https://github.com/golang/go/commit/5c489514bc5e61ad9b5b07bd7d8ec65d66a0512a - if reqUpType != resUpType { + + // Taken from https://github.com/golang/go/commit/5c489514bc5e61ad9b5b07bd7d8ec65d66a0512a + // We know reqUpType is ASCII, it's checked by the caller. + if !asciiIsPrint(resUpType) { + h.logger.Debug("backend tried to switch to invalid protocol", + zap.String("backend_upgrade", resUpType)) + return + } + if !asciiEqualFold(reqUpType, resUpType) { h.logger.Debug("backend tried to switch to unexpected protocol via Upgrade header", zap.String("backend_upgrade", resUpType), zap.String("requested_upgrade", reqUpType)) -- cgit v1.2.3