Skip to content

Commit 24bd0c7

Browse files
committed
Avoid passing empty headers during upgrade
This fixes the proxy for Safari which fails with a cryptic `Invalid UTF-8 sequence in header value ` error when the request contains a header with an empty Value e.g. `Set-Cookie: `
1 parent f90542e commit 24bd0c7

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

websocketproxy.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
136136

137137
// Only pass those headers to the upgrader.
138138
upgradeHeader := http.Header{}
139-
upgradeHeader.Set("Sec-WebSocket-Protocol",
140-
resp.Header.Get(http.CanonicalHeaderKey("Sec-WebSocket-Protocol")))
141-
upgradeHeader.Set("Set-Cookie",
142-
resp.Header.Get(http.CanonicalHeaderKey("Set-Cookie")))
139+
if hdr := resp.Header.Get("Sec-WebSocket-Protocol"); hdr != "" {
140+
upgradeHeader.Set("Sec-WebSocket-Protocol", hdr)
141+
}
142+
if hdr := resp.Header.Get("Set-Cookie"); hdr != "" {
143+
upgradeHeader.Set("Set-Cookie", hdr)
144+
}
143145

144146
// Now upgrade the existing incoming request to a WebSocket connection.
145147
// Also pass the header that we gathered from the Dial handshake.

0 commit comments

Comments
 (0)