Skip to content

Commit

Permalink
disable compression threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
lixizan committed Jun 11, 2024
1 parent 903f940 commit fb7dc86
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -117,6 +117,8 @@ func (c *Upgrader) getPermessageDeflate(extensions string) PermessageDeflate {
ServerMaxWindowBits: serverPD.ServerMaxWindowBits,
ClientMaxWindowBits: serverPD.ClientMaxWindowBits,
}
pd.setThreshold(true)
return pd
}

// Upgrade
Expand Down

0 comments on commit fb7dc86

Please sign in to comment.