Skip to content

Commit

Permalink
websocket: fix Parse closed state check
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed May 16, 2024
1 parent ac2cee6 commit 0dbebbc
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions nbhttp/websocket/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Conn struct {
msgType MessageType
message []byte
bytesCached []byte
parsing uint32

Check failure on line 83 in nbhttp/websocket/conn.go

View workflow job for this annotation

GitHub Actions / lint (1.18.x, ubuntu-latest)

field `parsing` is unused (unused)

Engine *nbhttp.Engine
Execute func(f func()) bool
Expand Down Expand Up @@ -330,6 +331,11 @@ func (c *Conn) nextFrame(data []byte) ([]byte, MessageType, []byte, bool, bool,
// Read .
func (c *Conn) Parse(data []byte) error {
c.mux.Lock()
if c.closed {
c.mux.Unlock()
return net.ErrClosed
}

readLimit := c.Engine.ReadLimit
if readLimit > 0 && (len(c.bytesCached)+len(data) > readLimit) {
c.mux.Unlock()
Expand All @@ -356,6 +362,10 @@ func (c *Conn) Parse(data []byte) error {
func() {
c.mux.Lock()
defer c.mux.Unlock()
if c.closed {
err = net.ErrClosed
return
}
data, opcode, body, ok, fin, compress, err = c.nextFrame(data)
if err != nil {
return
Expand Down Expand Up @@ -464,6 +474,9 @@ func (c *Conn) Parse(data []byte) error {
Exit:
c.mux.Lock()
defer c.mux.Unlock()
if c.closed {
return net.ErrClosed
}
// The data bytes were not all consumed, need to recache the current bytes left:
if len(data) > 0 {
// The data bytes were appended to the tail of the previous chaced data:
Expand Down

0 comments on commit 0dbebbc

Please sign in to comment.