diff --git a/client.go b/client.go index eca03361..b2e901ee 100644 --- a/client.go +++ b/client.go @@ -131,7 +131,7 @@ func (c *connector) request() (*http.Response, *bufio.Reader, error) { func (c *connector) getPermessageDeflate(extensions string) PermessageDeflate { serverPD := permessageNegotiation(extensions) clientPD := c.option.PermessageDeflate - return PermessageDeflate{ + pd := PermessageDeflate{ Enabled: clientPD.Enabled && strings.Contains(extensions, internal.PermessageDeflate), Threshold: clientPD.Threshold, Level: clientPD.Level, @@ -141,6 +141,8 @@ func (c *connector) getPermessageDeflate(extensions string) PermessageDeflate { ServerMaxWindowBits: serverPD.ServerMaxWindowBits, ClientMaxWindowBits: serverPD.ClientMaxWindowBits, } + pd.setThreshold(false) + return pd } func (c *connector) handshake() (*Conn, *http.Response, error) { diff --git a/option.go b/option.go index 1a2eb2ea..98cc4613 100644 --- a/option.go +++ b/option.go @@ -160,6 +160,16 @@ type ( } ) +// 设置压缩阈值 +// 开启上下文接管时, 必须不论长短压缩全部消息, 否则浏览器会报错 +// when context takeover is enabled, all messages must be compressed regardless of length, +// otherwise the browser will report an error. +func (c *PermessageDeflate) setThreshold(isServer bool) { + if (isServer && c.ServerContextTakeover) || (!isServer && c.ClientContextTakeover) { + c.Threshold = 0 + } +} + func (c *ServerOption) deleteProtectedHeaders() { c.ResponseHeader.Del(internal.Upgrade.Key) c.ResponseHeader.Del(internal.Connection.Key) diff --git a/upgrader.go b/upgrader.go index da680c62..18c741f1 100644 --- a/upgrader.go +++ b/upgrader.go @@ -107,7 +107,7 @@ func (c *Upgrader) hijack(w http.ResponseWriter) (net.Conn, *bufio.Reader, error func (c *Upgrader) getPermessageDeflate(extensions string) PermessageDeflate { clientPD := permessageNegotiation(extensions) serverPD := c.option.PermessageDeflate - return PermessageDeflate{ + pd := PermessageDeflate{ Enabled: serverPD.Enabled && strings.Contains(extensions, internal.PermessageDeflate), Threshold: serverPD.Threshold, Level: serverPD.Level, @@ -117,6 +117,8 @@ func (c *Upgrader) getPermessageDeflate(extensions string) PermessageDeflate { ServerMaxWindowBits: serverPD.ServerMaxWindowBits, ClientMaxWindowBits: serverPD.ClientMaxWindowBits, } + pd.setThreshold(true) + return pd } // Upgrade