Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverseproxy: Rewrite requests and responses for websocket over http2 #6567

Merged
merged 11 commits into from
Dec 6, 2024
Prev Previous commit
Next Next commit
set request variable to track if it's a h2 websocket
  • Loading branch information
WeidiDeng committed Sep 10, 2024
commit 460c3e5d2aa359235749273f3d242088eca12d80
1 change: 1 addition & 0 deletions modules/caddyhttp/reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
// websocket over http2, assuming backend doesn't support this, the request will be modifided to http1.1 upgrade
// TODO: once we can reliably detect backend support this, it can be removed for those backends
if r.ProtoMajor == 2 && r.Method == http.MethodConnect && r.Header.Get(":protocol") != "" {
caddyhttp.SetVar(clonedReq.Context(), "h2_websocket", true)
clonedReq.Header.Del(":protocol")
clonedReq.Method = http.MethodGet
clonedReq.Header.Set("Upgrade", r.Header.Get(":protocol"))
Expand Down
3 changes: 2 additions & 1 deletion modules/caddyhttp/reverseproxy/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"errors"
"fmt"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"io"
weakrand "math/rand"
"mime"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, wg *sync.WaitGroup,
)
// websocket over http2, assuming backend doesn't support this, the request will be modifided to http1.1 upgrade
// TODO: once we can reliably detect backend support this, it can be removed for those backends
if req.ProtoMajor == 2 && req.Method == http.MethodConnect && req.Header.Get(":protocol") != "" {
if b, ok := caddyhttp.GetVar(req.Context(), "h2_websocket").(bool); ok && b {
rw.Header().Del("Upgrade")
rw.Header().Del("Connection")
rw.Header().Del("Sec-Websocket-Accept")
Expand Down
Loading