Skip to content

Commit 49e3373

Browse files
committed
compression extension negotiation
1 parent d33ee90 commit 49e3373

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ func (c *connector) handshake() (*Conn, *http.Response, error) {
123123
if err != nil {
124124
return nil, c.resp, err
125125
}
126-
var compressEnabled = c.option.CompressEnabled && strings.Contains(c.resp.Header.Get(internal.SecWebSocketExtensions.Key), "permessage-deflate")
126+
127+
extensions := c.resp.Header.Get(internal.SecWebSocketExtensions.Key)
128+
var compressEnabled = c.option.CompressEnabled && strings.Contains(extensions, internal.PermessageDeflate)
129+
if compressEnabled && strings.Contains(extensions, "_max_window_bits") {
130+
return nil, c.resp, ErrCompressionNegotiation
131+
}
127132
return serveWebSocket(false, c.option.getConfig(), c.option.NewSessionStorage(), c.conn, br, c.eventHandler, compressEnabled, subprotocol), c.resp, nil
128133
}
129134

@@ -132,7 +137,7 @@ func (c *connector) getSubProtocol() (string, error) {
132137
b := internal.Split(c.resp.Header.Get(internal.SecWebSocketProtocol.Key), ",")
133138
subprotocol := internal.GetIntersectionElem(a, b)
134139
if len(a) > 0 && subprotocol == "" {
135-
return "", ErrHandshake
140+
return "", ErrSubprotocolNegotiation
136141
}
137142
return subprotocol, nil
138143
}

internal/others.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"net"
77
)
88

9+
const PermessageDeflate = "permessage-deflate"
10+
911
type Pair struct {
1012
Key string
1113
Val string

protocol.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ var (
4747
// Handshake error, request header does not pass checksum.
4848
ErrHandshake = errors.New("handshake error")
4949

50+
// ErrCompressionNegotiation 压缩拓展协商失败
51+
// 客户端不支持server_max_window_bits参数
52+
// The client does not support the server_max_window_bits parameter
53+
ErrCompressionNegotiation = errors.New("invalid compression negotiation")
54+
55+
// ErrSubprotocolNegotiation 子协议协商失败
56+
// Sub-protocol negotiation failed
57+
ErrSubprotocolNegotiation = errors.New("sub-protocol negotiation failed")
58+
5059
// ErrTextEncoding 文本消息编码错误(必须是utf8编码)
5160
// Text message encoding error (must be utf8)
5261
ErrTextEncoding = errors.New("invalid text encoding")

updrader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (c *Upgrader) connectHandshake(r *http.Request, responseHeader http.Header,
2929
if len(c.option.SubProtocols) > 0 {
3030
subprotocol = internal.GetIntersectionElem(internal.Split(r.Header.Get(internal.SecWebSocketProtocol.Key), ","), c.option.SubProtocols)
3131
if subprotocol == "" {
32-
return "", ErrHandshake
32+
return "", ErrSubprotocolNegotiation
3333
}
3434
responseHeader.Set(internal.SecWebSocketProtocol.Key, subprotocol)
3535
}

0 commit comments

Comments
 (0)