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: Use correct cases for websocket related headers #6621

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions modules/caddyhttp/reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,23 @@ func (h *Handler) proxyLoopIteration(r *http.Request, origReq *http.Request, w h
return false, proxyErr
}

const canonicalWsHeaderPrefix = "Sec-Websocket"

// websocket header replacer, normally server doesn't care about this difference,
// but some do, and RFC use the case too
// gorilla/websocket, gobwas/ws all use this case too.
// see https://caddy.community/t/websockets-fail-venus-os-cerbo-gx-victron/25685
var wsHeaderReplacer = strings.NewReplacer(canonicalWsHeaderPrefix, "Sec-WebSocket")

func normalizeWsHeader(header http.Header) {
for k, v := range header {
WeidiDeng marked this conversation as resolved.
Show resolved Hide resolved
if strings.HasPrefix(k, canonicalWsHeaderPrefix) {
header[wsHeaderReplacer.Replace(k)] = v
delete(header, k)
}
}
}

// prepareRequest clones req so that it can be safely modified without
// changing the original request or introducing data races. It then
// modifies it so that it is ready to be proxied, except for directing
Expand Down Expand Up @@ -655,6 +672,8 @@ func (h Handler) prepareRequest(req *http.Request, repl *caddy.Replacer) (*http.
if reqUpType != "" {
req.Header.Set("Connection", "Upgrade")
req.Header.Set("Upgrade", reqUpType)
// use the correct case for websocket headers
normalizeWsHeader(req.Header)
}

// Set up the PROXY protocol info
Expand Down
2 changes: 2 additions & 0 deletions modules/caddyhttp/reverseproxy/streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, wg *sync.WaitGroup,
return
}

// normalize websocket header
normalizeWsHeader(res.Header)
// write header first, response headers should not be counted in size
// like the rest of handler chain.
copyHeader(rw.Header(), res.Header)
Expand Down
Loading