Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: protocol parsing errors with ws compression and pongs #1790

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ var wsGUID = []byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
var compressFinalBlock = []byte{0x00, 0x00, 0xff, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff}

type websocketReader struct {
r io.Reader
pending [][]byte
ib []byte
ff bool
fc bool
nl bool
dc *wsDecompressor
nc *Conn
r io.Reader
pending [][]byte
compress bool
ib []byte
ff bool
fc bool
nl bool
dc *wsDecompressor
nc *Conn
}

type wsDecompressor struct {
Expand Down Expand Up @@ -312,6 +313,8 @@ func (r *websocketReader) Read(p []byte) (int, error) {
}
r.fc = false
}
} else if r.compress {
b = bytes.Clone(b)
}
// Add to the pending list if dealing with uncompressed frames or
// after we have received the full compressed message and decompressed it.
Expand Down Expand Up @@ -647,6 +650,7 @@ func (nc *Conn) wsInitHandshake(u *url.URL) error {

wsr := wsNewReader(nc.br.r)
wsr.nc = nc
wsr.compress = compress
// We have to slurp whatever is in the bufio reader and copy to br.r
if n := br.Buffered(); n != 0 {
wsr.ib, _ = br.Peek(n)
Expand Down