Skip to content

Commit b7c73f1

Browse files
committed
netconn: Disable read limit on WebSocket
Closes #245
1 parent 15a1523 commit b7c73f1

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Diff for: netconn.go

+4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ import (
3838
//
3939
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
4040
// io.EOF when reading.
41+
//
42+
// Furthermore, the ReadLimit is set to -1 to disable it.
4143
func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
44+
c.SetReadLimit(-1)
45+
4246
nc := &netConn{
4347
c: c,
4448
msgType: msgType,

Diff for: read.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,16 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context {
6969
// By default, the connection has a message read limit of 32768 bytes.
7070
//
7171
// When the limit is hit, the connection will be closed with StatusMessageTooBig.
72+
//
73+
// Set to -1 to disable.
7274
func (c *Conn) SetReadLimit(n int64) {
73-
// We add read one more byte than the limit in case
74-
// there is a fin frame that needs to be read.
75-
c.msgReader.limitReader.limit.Store(n + 1)
75+
if n >= 0 {
76+
// We read one more byte than the limit in case
77+
// there is a fin frame that needs to be read.
78+
n++
79+
}
80+
81+
c.msgReader.limitReader.limit.Store(n)
7682
}
7783

7884
const defaultReadLimit = 32768
@@ -450,7 +456,11 @@ func (lr *limitReader) reset(r io.Reader) {
450456
}
451457

452458
func (lr *limitReader) Read(p []byte) (int, error) {
453-
if lr.n <= 0 {
459+
if lr.n < 0 {
460+
return lr.r.Read(p)
461+
}
462+
463+
if lr.n == 0 {
454464
err := fmt.Errorf("read limited at %v bytes", lr.limit.Load())
455465
lr.c.writeError(StatusMessageTooBig, err)
456466
return 0, err
@@ -461,6 +471,9 @@ func (lr *limitReader) Read(p []byte) (int, error) {
461471
}
462472
n, err := lr.r.Read(p)
463473
lr.n -= int64(n)
474+
if lr.n < 0 {
475+
lr.n = 0
476+
}
464477
return n, err
465478
}
466479

0 commit comments

Comments
 (0)