Skip to content

Commit

Permalink
Comment and naming adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Oct 11, 2024
1 parent 519ed06 commit 34efb78
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
21 changes: 11 additions & 10 deletions modules/caddyhttp/reverseproxy/reverseproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,21 +569,23 @@ func (h *Handler) proxyLoopIteration(r *http.Request, origReq *http.Request, w h
return false, proxyErr
}

// only 5 headers to check
var wsHeaderMapping = map[string]string{
// Mapping of the canonical form of the headers, to the RFC 6455 form,
// i.e. `WebSocket` with uppercase 'S'.
var websocketHeaderMapping = map[string]string{
"Sec-Websocket-Accept": "Sec-WebSocket-Accept",
"Sec-Websocket-Extensions": "Sec-WebSocket-Extensions",
"Sec-Websocket-Key": "Sec-WebSocket-Key",
"Sec-Websocket-Protocol": "Sec-WebSocket-Protocol",
"Sec-Websocket-Version": "Sec-WebSocket-Version",
}

// 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
func normalizeWsHeader(header http.Header) {
for k, rk := range wsHeaderMapping {
// normalizeWebsocketHeaders ensures we use the standard casing as per
// RFC 6455, i.e. `WebSocket` with uppercase 'S'. Most servers don't
// care about this difference (read headers case insensitively), but
// some do, so this maximizes compatiblity with upstreams.

Check failure on line 585 in modules/caddyhttp/reverseproxy/reverseproxy.go

View workflow job for this annotation

GitHub Actions / lint (mac)

`compatiblity` is a misspelling of `compatibility` (misspell)
// See https://github.com/caddyserver/caddy/pull/6621
func normalizeWebsocketHeaders(header http.Header) {
for k, rk := range websocketHeaderMapping {
if v, ok := header[k]; ok {
delete(header, k)
header[rk] = v
Expand Down Expand Up @@ -677,8 +679,7 @@ 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)
normalizeWebsocketHeaders(req.Header)
}

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

// normalize websocket header
normalizeWsHeader(res.Header)
normalizeWebsocketHeaders(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

0 comments on commit 34efb78

Please sign in to comment.