From 9dc3dd02f397f83941fbcc7d5271a33857cb5df3 Mon Sep 17 00:00:00 2001
From: Přemysl Eric Janouch <p@janouch.name>
Date: Tue, 4 Oct 2022 01:16:28 +0200
Subject: xP: disable WebSocket compression on Safari

Wildly known to be broken.
---
 xP/xP.go | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

(limited to 'xP')

diff --git a/xP/xP.go b/xP/xP.go
index 20117b2..3effaf0 100644
--- a/xP/xP.go
+++ b/xP/xP.go
@@ -16,6 +16,7 @@ import (
 	"net"
 	"net/http"
 	"os"
+	"strings"
 	"time"
 
 	"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) {
-	ws, err := websocket.Accept(w, r, &websocket.AcceptOptions{
+	opts := &websocket.AcceptOptions{
 		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.
 		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 {
 		log.Println("Client rejected: " + err.Error())
 		return
-- 
cgit v1.2.3-70-g09d2