Skip to content

Commit 662e2f7

Browse files
nhooyrtmessi
authored andcommitted
netconn: Disable read limit on WebSocket
Closes coder#245
1 parent 3604edc commit 662e2f7

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

netconn.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ import (
3737
//
3838
// A received StatusNormalClosure or StatusGoingAway close frame will be translated to
3939
// io.EOF when reading.
40+
//
41+
// Furthermore, the ReadLimit is set to -1 to disable it.
4042
func NetConn(ctx context.Context, c *Conn, msgType MessageType) net.Conn {
43+
c.SetReadLimit(-1)
44+
4145
nc := &netConn{
4246
c: c,
4347
msgType: msgType,

read.go

Lines changed: 17 additions & 4 deletions
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
@@ -453,7 +459,11 @@ func (lr *limitReader) reset(r io.Reader) {
453459
}
454460

455461
func (lr *limitReader) Read(p []byte) (int, error) {
456-
if lr.n <= 0 {
462+
if lr.n < 0 {
463+
return lr.r.Read(p)
464+
}
465+
466+
if lr.n == 0 {
457467
err := fmt.Errorf("read limited at %v bytes", lr.limit.Load())
458468
lr.c.writeError(StatusMessageTooBig, err)
459469
return 0, err
@@ -464,6 +474,9 @@ func (lr *limitReader) Read(p []byte) (int, error) {
464474
}
465475
n, err := lr.r.Read(p)
466476
lr.n -= int64(n)
477+
if lr.n < 0 {
478+
lr.n = 0
479+
}
467480
return n, err
468481
}
469482

0 commit comments

Comments
 (0)