We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
ErrMessageTooBig
1 parent c7846ea commit 7d7c644Copy full SHA for 7d7c644
conn_test.go
@@ -421,6 +421,25 @@ func TestConn(t *testing.T) {
421
err = c1.Close(websocket.StatusNormalClosure, "")
422
assert.Success(t, err)
423
})
424
+
425
+ t.Run("ReadLimitExceededReturnsErrMessageTooBig", func(t *testing.T) {
426
+ tt, c1, c2 := newConnTest(t, nil, nil)
427
428
+ c1.SetReadLimit(1024)
429
+ _ = c2.CloseRead(tt.ctx)
430
431
+ writeDone := xsync.Go(func() error {
432
+ payload := strings.Repeat("x", 4096)
433
+ return c2.Write(tt.ctx, websocket.MessageText, []byte(payload))
434
+ })
435
436
+ _, _, err := c1.Read(tt.ctx)
437
+ assert.ErrorIs(t, websocket.ErrMessageTooBig, err)
438
+ assert.Contains(t, err, "read limited at 1025 bytes")
439
440
+ _ = c2.CloseNow()
441
+ <-writeDone
442
443
}
444
445
func TestWasm(t *testing.T) {
errors.go
@@ -0,0 +1,8 @@
1
+package websocket
2
3
+import (
4
+ "errors"
5
+)
6
7
+// ErrMessageTooBig is returned when a message exceeds the read limit.
8
+var ErrMessageTooBig = errors.New("websocket: message too big")
read.go
@@ -90,7 +90,8 @@ func (c *Conn) CloseRead(ctx context.Context) context.Context {
90
//
91
// By default, the connection has a message read limit of 32768 bytes.
92
93
-// When the limit is hit, the connection will be closed with StatusMessageTooBig.
+// When the limit is hit, reads return an error wrapping ErrMessageTooBig and
94
+// the connection is closed with StatusMessageTooBig.
95
96
// Set to -1 to disable.
97
func (c *Conn) SetReadLimit(n int64) {
@@ -522,9 +523,9 @@ func (lr *limitReader) Read(p []byte) (int, error) {
522
523
524
525
if lr.n == 0 {
- err := fmt.Errorf("read limited at %v bytes", lr.limit.Load())
526
- lr.c.writeError(StatusMessageTooBig, err)
527
- return 0, err
+ reason := fmt.Errorf("read limited at %d bytes", lr.limit.Load())
+ lr.c.writeError(StatusMessageTooBig, reason)
528
+ return 0, fmt.Errorf("%w: %v", ErrMessageTooBig, reason)
529
530
531
if int64(len(p)) > lr.n {
ws_js.go
@@ -144,9 +144,9 @@ func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) {
144
145
readLimit := c.msgReadLimit.Load()
146
if readLimit >= 0 && int64(len(p)) > readLimit {
147
- err := fmt.Errorf("read limited at %v bytes", c.msgReadLimit.Load())
148
- c.Close(StatusMessageTooBig, err.Error())
149
- return 0, nil, err
+ reason := fmt.Errorf("read limited at %d bytes", c.msgReadLimit.Load())
+ c.Close(StatusMessageTooBig, reason.Error())
+ return 0, nil, fmt.Errorf("%w: %v", ErrMessageTooBig, reason)
150
151
return typ, p, nil
152
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments