Skip to content

Commit 7fb39ec

Browse files
tonyb486WeidiDeng
andauthored
reverseproxy: Use http1.1 upgrade for websocket for extended connect of http2 and http3 (#7305)
Co-authored-by: WeidiDeng <weidi_deng@icloud.com>
1 parent 10ac7da commit 7fb39ec

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

modules/caddyhttp/reverseproxy/reverseproxy.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,16 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
409409
return caddyhttp.Error(http.StatusInternalServerError,
410410
fmt.Errorf("preparing request for upstream round-trip: %v", err))
411411
}
412-
// websocket over http2, assuming backend doesn't support this, the request will be modified to http1.1 upgrade
412+
413+
// websocket over http2 or http3 if extended connect is enabled, assuming backend doesn't support this, the request will be modified to http1.1 upgrade
414+
// Both use the same upgrade mechanism: server advertizes extended connect support, and client sends the pseudo header :protocol in a CONNECT request
415+
// The quic-go http3 implementation also puts :protocol in r.Proto for CONNECT requests (quic-go/http3/headers.go@70-72,185,203)
413416
// TODO: once we can reliably detect backend support this, it can be removed for those backends
414-
if r.ProtoMajor == 2 && r.Method == http.MethodConnect && r.Header.Get(":protocol") == "websocket" {
417+
if (r.ProtoMajor == 2 && r.Method == http.MethodConnect && r.Header.Get(":protocol") == "websocket") ||
418+
(r.ProtoMajor == 3 && r.Method == http.MethodConnect && r.Proto == "websocket") {
415419
clonedReq.Header.Del(":protocol")
416420
// keep the body for later use. http1.1 upgrade uses http.NoBody
417-
caddyhttp.SetVar(clonedReq.Context(), "h2_websocket_body", clonedReq.Body)
421+
caddyhttp.SetVar(clonedReq.Context(), "extended_connect_websocket_body", clonedReq.Body)
418422
clonedReq.Body = http.NoBody
419423
clonedReq.Method = http.MethodGet
420424
clonedReq.Header.Set("Upgrade", "websocket")

modules/caddyhttp/reverseproxy/streaming.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, wg *sync.WaitGroup,
9494
conn io.ReadWriteCloser
9595
brw *bufio.ReadWriter
9696
)
97-
// websocket over http2, assuming backend doesn't support this, the request will be modified to http1.1 upgrade
97+
// websocket over http2 or http3 if extended connect is enabled, assuming backend doesn't support this, the request will be modified to http1.1 upgrade
9898
// TODO: once we can reliably detect backend support this, it can be removed for those backends
99-
if body, ok := caddyhttp.GetVar(req.Context(), "h2_websocket_body").(io.ReadCloser); ok {
99+
if body, ok := caddyhttp.GetVar(req.Context(), "extended_connect_websocket_body").(io.ReadCloser); ok {
100100
req.Body = body
101101
rw.Header().Del("Upgrade")
102102
rw.Header().Del("Connection")

0 commit comments

Comments
 (0)