xP: disable WebSocket compression on Safari

Wildly known to be broken.
This commit is contained in:
Přemysl Eric Janouch 2022-10-04 01:16:28 +02:00
parent a7c3ed7cc1
commit 9dc3dd02f3
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 13 additions and 4 deletions

View File

@ -16,6 +16,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"strings"
"time" "time"
"nhooyr.io/websocket" "nhooyr.io/websocket"
@ -159,13 +160,21 @@ func clientWriteError(ctx context.Context, ws *websocket.Conn, err error) bool {
} }
func handleWS(w http.ResponseWriter, r *http.Request) { func handleWS(w http.ResponseWriter, r *http.Request) {
ws, err := websocket.Accept(w, r, &websocket.AcceptOptions{ opts := &websocket.AcceptOptions{
InsecureSkipVerify: true, InsecureSkipVerify: true,
// Note that Safari can be broken with compression. CompressionMode: websocket.CompressionContextTakeover,
CompressionMode: websocket.CompressionContextTakeover,
// This is for the payload; set higher to avoid overhead. // This is for the payload; set higher to avoid overhead.
CompressionThreshold: 64 << 10, CompressionThreshold: 64 << 10,
}) }
// AppleWebKit can be broken with compression.
if agent := r.UserAgent(); strings.Contains(agent, " Version/") &&
(strings.HasPrefix(agent, "Mozilla/5.0 (Macintosh; ") ||
strings.HasPrefix(agent, "Mozilla/5.0 (iPhone; ")) {
opts.CompressionMode = websocket.CompressionDisabled
}
ws, err := websocket.Accept(w, r, opts)
if err != nil { if err != nil {
log.Println("Client rejected: " + err.Error()) log.Println("Client rejected: " + err.Error())
return return