2014-04-19 01:25:11 +04:00
|
|
|
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
2013-10-16 20:41:47 +04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-08-26 23:01:45 +03:00
|
|
|
"crypto/rand"
|
2013-10-16 20:41:47 +04:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2022-01-02 23:16:08 +03:00
|
|
|
"strings"
|
2016-11-02 19:41:43 +03:00
|
|
|
"sync"
|
2013-10-16 20:41:47 +04:00
|
|
|
"time"
|
2016-04-28 19:34:18 +03:00
|
|
|
"unicode/utf8"
|
2013-10-16 20:41:47 +04:00
|
|
|
)
|
|
|
|
|
2014-11-05 23:26:52 +03:00
|
|
|
const (
|
2016-06-30 03:03:55 +03:00
|
|
|
// Frame header byte 0 bits from Section 5.2 of RFC 6455
|
|
|
|
finalBit = 1 << 7
|
|
|
|
rsv1Bit = 1 << 6
|
|
|
|
rsv2Bit = 1 << 5
|
|
|
|
rsv3Bit = 1 << 4
|
|
|
|
|
|
|
|
// Frame header byte 1 bits from Section 5.2 of RFC 6455
|
|
|
|
maskBit = 1 << 7
|
|
|
|
|
2014-11-05 23:26:52 +03:00
|
|
|
maxFrameHeaderSize = 2 + 8 + 4 // Fixed header + length + mask
|
|
|
|
maxControlFramePayloadSize = 125
|
2016-06-30 03:03:55 +03:00
|
|
|
|
|
|
|
writeWait = time.Second
|
2014-11-05 23:26:52 +03:00
|
|
|
|
|
|
|
defaultReadBufferSize = 4096
|
|
|
|
defaultWriteBufferSize = 4096
|
|
|
|
|
|
|
|
continuationFrame = 0
|
|
|
|
noFrame = -1
|
|
|
|
)
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
// Close codes defined in RFC 6455, section 11.7.
|
|
|
|
const (
|
|
|
|
CloseNormalClosure = 1000
|
|
|
|
CloseGoingAway = 1001
|
|
|
|
CloseProtocolError = 1002
|
|
|
|
CloseUnsupportedData = 1003
|
|
|
|
CloseNoStatusReceived = 1005
|
|
|
|
CloseAbnormalClosure = 1006
|
|
|
|
CloseInvalidFramePayloadData = 1007
|
|
|
|
ClosePolicyViolation = 1008
|
|
|
|
CloseMessageTooBig = 1009
|
|
|
|
CloseMandatoryExtension = 1010
|
|
|
|
CloseInternalServerErr = 1011
|
2016-04-28 19:34:18 +03:00
|
|
|
CloseServiceRestart = 1012
|
|
|
|
CloseTryAgainLater = 1013
|
2013-10-16 20:41:47 +04:00
|
|
|
CloseTLSHandshake = 1015
|
|
|
|
)
|
|
|
|
|
|
|
|
// The message types are defined in RFC 6455, section 11.8.
|
|
|
|
const (
|
2013-10-27 19:34:33 +04:00
|
|
|
// TextMessage denotes a text data message. The text message payload is
|
2013-10-16 20:41:47 +04:00
|
|
|
// interpreted as UTF-8 encoded text data.
|
|
|
|
TextMessage = 1
|
|
|
|
|
|
|
|
// BinaryMessage denotes a binary data message.
|
|
|
|
BinaryMessage = 2
|
|
|
|
|
|
|
|
// CloseMessage denotes a close control message. The optional message
|
|
|
|
// payload contains a numeric code and text. Use the FormatCloseMessage
|
|
|
|
// function to format a close message payload.
|
|
|
|
CloseMessage = 8
|
|
|
|
|
|
|
|
// PingMessage denotes a ping control message. The optional message payload
|
|
|
|
// is UTF-8 encoded text.
|
|
|
|
PingMessage = 9
|
|
|
|
|
2017-10-07 23:29:39 +03:00
|
|
|
// PongMessage denotes a pong control message. The optional message payload
|
2013-10-16 20:41:47 +04:00
|
|
|
// is UTF-8 encoded text.
|
|
|
|
PongMessage = 10
|
|
|
|
)
|
|
|
|
|
2014-11-05 23:26:52 +03:00
|
|
|
// ErrCloseSent is returned when the application writes a message to the
|
|
|
|
// connection after sending a close message.
|
|
|
|
var ErrCloseSent = errors.New("websocket: close sent")
|
2014-04-20 18:20:03 +04:00
|
|
|
|
2014-11-05 23:26:52 +03:00
|
|
|
// ErrReadLimit is returned when reading a message that is larger than the
|
|
|
|
// read limit set for the connection.
|
|
|
|
var ErrReadLimit = errors.New("websocket: read limit exceeded")
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2014-11-01 00:52:20 +03:00
|
|
|
// netError satisfies the net Error interface.
|
|
|
|
type netError struct {
|
2013-12-24 00:01:25 +04:00
|
|
|
msg string
|
|
|
|
temporary bool
|
|
|
|
timeout bool
|
|
|
|
}
|
|
|
|
|
2014-11-01 00:52:20 +03:00
|
|
|
func (e *netError) Error() string { return e.msg }
|
|
|
|
func (e *netError) Temporary() bool { return e.temporary }
|
|
|
|
func (e *netError) Timeout() bool { return e.timeout }
|
|
|
|
|
2018-01-10 17:15:25 +03:00
|
|
|
// CloseError represents a close message.
|
2015-08-11 20:14:32 +03:00
|
|
|
type CloseError struct {
|
|
|
|
// Code is defined in RFC 6455, section 11.7.
|
|
|
|
Code int
|
|
|
|
|
|
|
|
// Text is the optional text payload.
|
|
|
|
Text string
|
2014-11-01 00:52:20 +03:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:14:32 +03:00
|
|
|
func (e *CloseError) Error() string {
|
2016-01-28 19:48:56 +03:00
|
|
|
s := []byte("websocket: close ")
|
|
|
|
s = strconv.AppendInt(s, int64(e.Code), 10)
|
|
|
|
switch e.Code {
|
|
|
|
case CloseNormalClosure:
|
|
|
|
s = append(s, " (normal)"...)
|
|
|
|
case CloseGoingAway:
|
|
|
|
s = append(s, " (going away)"...)
|
|
|
|
case CloseProtocolError:
|
|
|
|
s = append(s, " (protocol error)"...)
|
|
|
|
case CloseUnsupportedData:
|
|
|
|
s = append(s, " (unsupported data)"...)
|
|
|
|
case CloseNoStatusReceived:
|
|
|
|
s = append(s, " (no status)"...)
|
|
|
|
case CloseAbnormalClosure:
|
|
|
|
s = append(s, " (abnormal closure)"...)
|
|
|
|
case CloseInvalidFramePayloadData:
|
|
|
|
s = append(s, " (invalid payload data)"...)
|
|
|
|
case ClosePolicyViolation:
|
|
|
|
s = append(s, " (policy violation)"...)
|
|
|
|
case CloseMessageTooBig:
|
|
|
|
s = append(s, " (message too big)"...)
|
|
|
|
case CloseMandatoryExtension:
|
|
|
|
s = append(s, " (mandatory extension missing)"...)
|
|
|
|
case CloseInternalServerErr:
|
|
|
|
s = append(s, " (internal server error)"...)
|
|
|
|
case CloseTLSHandshake:
|
|
|
|
s = append(s, " (TLS handshake error)"...)
|
|
|
|
}
|
|
|
|
if e.Text != "" {
|
|
|
|
s = append(s, ": "...)
|
|
|
|
s = append(s, e.Text...)
|
|
|
|
}
|
|
|
|
return string(s)
|
2014-11-01 00:52:20 +03:00
|
|
|
}
|
2013-12-24 00:01:25 +04:00
|
|
|
|
2016-01-19 20:20:21 +03:00
|
|
|
// IsCloseError returns boolean indicating whether the error is a *CloseError
|
|
|
|
// with one of the specified codes.
|
|
|
|
func IsCloseError(err error, codes ...int) bool {
|
|
|
|
if e, ok := err.(*CloseError); ok {
|
|
|
|
for _, code := range codes {
|
|
|
|
if e.Code == code {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-01-20 00:06:29 +03:00
|
|
|
// IsUnexpectedCloseError returns boolean indicating whether the error is a
|
|
|
|
// *CloseError with a code not in the list of expected codes.
|
|
|
|
func IsUnexpectedCloseError(err error, expectedCodes ...int) bool {
|
|
|
|
if e, ok := err.(*CloseError); ok {
|
|
|
|
for _, code := range expectedCodes {
|
|
|
|
if e.Code == code {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
var (
|
2015-10-15 20:14:19 +03:00
|
|
|
errWriteTimeout = &netError{msg: "websocket: write timeout", timeout: true, temporary: true}
|
2015-08-11 20:14:32 +03:00
|
|
|
errUnexpectedEOF = &CloseError{Code: CloseAbnormalClosure, Text: io.ErrUnexpectedEOF.Error()}
|
2013-10-16 20:41:47 +04:00
|
|
|
errBadWriteOpCode = errors.New("websocket: bad write message type")
|
|
|
|
errWriteClosed = errors.New("websocket: write closed")
|
|
|
|
errInvalidControlFrame = errors.New("websocket: invalid control frame")
|
|
|
|
)
|
|
|
|
|
2023-08-26 23:01:45 +03:00
|
|
|
// maskRand is an io.Reader for generating mask bytes. The reader is initialized
|
|
|
|
// to crypto/rand Reader. Tests swap the reader to a math/rand reader for
|
|
|
|
// reproducible results.
|
|
|
|
var maskRand = rand.Reader
|
|
|
|
|
|
|
|
// newMaskKey returns a new 32 bit value for masking client frames.
|
2017-01-01 07:13:41 +03:00
|
|
|
func newMaskKey() [4]byte {
|
2023-08-26 23:01:45 +03:00
|
|
|
var k [4]byte
|
|
|
|
_, _ = io.ReadFull(maskRand, k[:])
|
|
|
|
return k
|
2017-01-01 07:13:41 +03:00
|
|
|
}
|
|
|
|
|
2014-06-06 20:12:15 +04:00
|
|
|
func hideTempErr(err error) error {
|
2023-08-26 23:01:45 +03:00
|
|
|
if e, ok := err.(net.Error); ok {
|
2014-11-07 03:56:58 +03:00
|
|
|
err = &netError{msg: e.Error(), timeout: e.Timeout()}
|
2014-06-06 20:12:15 +04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
func isControl(frameType int) bool {
|
|
|
|
return frameType == CloseMessage || frameType == PingMessage || frameType == PongMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func isData(frameType int) bool {
|
|
|
|
return frameType == TextMessage || frameType == BinaryMessage
|
|
|
|
}
|
|
|
|
|
2016-04-28 19:34:18 +03:00
|
|
|
var validReceivedCloseCodes = map[int]bool{
|
|
|
|
// see http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
|
|
|
|
|
|
|
|
CloseNormalClosure: true,
|
|
|
|
CloseGoingAway: true,
|
|
|
|
CloseProtocolError: true,
|
|
|
|
CloseUnsupportedData: true,
|
|
|
|
CloseNoStatusReceived: false,
|
|
|
|
CloseAbnormalClosure: false,
|
|
|
|
CloseInvalidFramePayloadData: true,
|
|
|
|
ClosePolicyViolation: true,
|
|
|
|
CloseMessageTooBig: true,
|
|
|
|
CloseMandatoryExtension: true,
|
|
|
|
CloseInternalServerErr: true,
|
|
|
|
CloseServiceRestart: true,
|
|
|
|
CloseTryAgainLater: true,
|
|
|
|
CloseTLSHandshake: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValidReceivedCloseCode(code int) bool {
|
|
|
|
return validReceivedCloseCodes[code] || (code >= 3000 && code <= 4999)
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:50:34 +03:00
|
|
|
// BufferPool represents a pool of buffers. The *sync.Pool type satisfies this
|
|
|
|
// interface. The type of the value stored in a pool is not specified.
|
|
|
|
type BufferPool interface {
|
|
|
|
// Get gets a value from the pool or returns nil if the pool is empty.
|
|
|
|
Get() interface{}
|
|
|
|
// Put adds a value to the pool.
|
|
|
|
Put(interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// writePoolData is the type added to the write buffer pool. This wrapper is
|
|
|
|
// used to prevent applications from peeking at and depending on the values
|
|
|
|
// added to the pool.
|
|
|
|
type writePoolData struct{ buf []byte }
|
|
|
|
|
2016-12-08 03:21:58 +03:00
|
|
|
// The Conn type represents a WebSocket connection.
|
2013-10-16 20:41:47 +04:00
|
|
|
type Conn struct {
|
2013-12-14 19:48:43 +04:00
|
|
|
conn net.Conn
|
|
|
|
isServer bool
|
|
|
|
subprotocol string
|
2013-10-16 20:41:47 +04:00
|
|
|
|
|
|
|
// Write fields
|
2020-03-19 16:52:00 +03:00
|
|
|
mu chan struct{} // used as mutex to protect write to conn
|
|
|
|
writeBuf []byte // frame is constructed in this buffer.
|
2018-08-18 05:50:34 +03:00
|
|
|
writePool BufferPool
|
|
|
|
writeBufSize int
|
2016-11-02 19:41:43 +03:00
|
|
|
writeDeadline time.Time
|
|
|
|
writer io.WriteCloser // the current writer returned to the application
|
|
|
|
isWriting bool // for best-effort concurrent write detection
|
|
|
|
|
|
|
|
writeErrMu sync.Mutex
|
|
|
|
writeErr error
|
2016-06-30 03:03:55 +03:00
|
|
|
|
|
|
|
enableWriteCompression bool
|
2017-01-19 22:00:23 +03:00
|
|
|
compressionLevel int
|
|
|
|
newCompressionWriter func(io.WriteCloser, int) io.WriteCloser
|
2013-10-16 20:41:47 +04:00
|
|
|
|
|
|
|
// Read fields
|
2019-08-25 04:17:28 +03:00
|
|
|
reader io.ReadCloser // the current reader returned to the application
|
|
|
|
readErr error
|
|
|
|
br *bufio.Reader
|
|
|
|
// bytes remaining in current frame.
|
|
|
|
// set setReadRemaining to safely update this value and prevent overflow
|
|
|
|
readRemaining int64
|
2013-10-16 20:41:47 +04:00
|
|
|
readFinal bool // true the current message has more frames.
|
|
|
|
readLength int64 // Message size.
|
|
|
|
readLimit int64 // Maximum message size.
|
|
|
|
readMaskPos int
|
|
|
|
readMaskKey [4]byte
|
|
|
|
handlePong func(string) error
|
|
|
|
handlePing func(string) error
|
2016-11-02 23:04:59 +03:00
|
|
|
handleClose func(int, string) error
|
2016-02-17 02:03:54 +03:00
|
|
|
readErrCount int
|
2016-06-30 03:03:55 +03:00
|
|
|
messageReader *messageReader // the current low-level reader
|
|
|
|
|
|
|
|
readDecompress bool // whether last read frame had RSV1 set
|
2016-12-18 02:33:06 +03:00
|
|
|
newDecompressionReader func(io.Reader) io.ReadCloser
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2018-08-18 05:50:34 +03:00
|
|
|
func newConn(conn net.Conn, isServer bool, readBufferSize, writeBufferSize int, writeBufferPool BufferPool, br *bufio.Reader, writeBuf []byte) *Conn {
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2017-03-01 20:36:54 +03:00
|
|
|
if br == nil {
|
2017-03-03 01:46:13 +03:00
|
|
|
if readBufferSize == 0 {
|
|
|
|
readBufferSize = defaultReadBufferSize
|
2018-08-18 05:50:34 +03:00
|
|
|
} else if readBufferSize < maxControlFramePayloadSize {
|
|
|
|
// must be large enough for control frame
|
2017-03-03 01:46:13 +03:00
|
|
|
readBufferSize = maxControlFramePayloadSize
|
|
|
|
}
|
2017-03-01 20:36:54 +03:00
|
|
|
br = bufio.NewReaderSize(conn, readBufferSize)
|
|
|
|
}
|
|
|
|
|
2018-08-18 05:50:34 +03:00
|
|
|
if writeBufferSize <= 0 {
|
|
|
|
writeBufferSize = defaultWriteBufferSize
|
2017-03-03 01:46:13 +03:00
|
|
|
}
|
2018-08-18 05:50:34 +03:00
|
|
|
writeBufferSize += maxFrameHeaderSize
|
2017-03-03 01:46:13 +03:00
|
|
|
|
2018-08-18 05:50:34 +03:00
|
|
|
if writeBuf == nil && writeBufferPool == nil {
|
|
|
|
writeBuf = make([]byte, writeBufferSize)
|
2014-11-05 23:26:52 +03:00
|
|
|
}
|
|
|
|
|
2020-03-19 16:52:00 +03:00
|
|
|
mu := make(chan struct{}, 1)
|
|
|
|
mu <- struct{}{}
|
2013-10-16 20:41:47 +04:00
|
|
|
c := &Conn{
|
2016-06-30 03:03:55 +03:00
|
|
|
isServer: isServer,
|
2017-03-01 20:36:54 +03:00
|
|
|
br: br,
|
2016-06-30 03:03:55 +03:00
|
|
|
conn: conn,
|
|
|
|
mu: mu,
|
|
|
|
readFinal: true,
|
2017-03-03 01:46:13 +03:00
|
|
|
writeBuf: writeBuf,
|
2018-08-18 05:50:34 +03:00
|
|
|
writePool: writeBufferPool,
|
|
|
|
writeBufSize: writeBufferSize,
|
2016-06-30 03:03:55 +03:00
|
|
|
enableWriteCompression: true,
|
2017-01-19 22:00:23 +03:00
|
|
|
compressionLevel: defaultCompressionLevel,
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2016-11-02 23:04:59 +03:00
|
|
|
c.SetCloseHandler(nil)
|
2013-10-16 20:41:47 +04:00
|
|
|
c.SetPingHandler(nil)
|
|
|
|
c.SetPongHandler(nil)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-08-25 04:17:28 +03:00
|
|
|
// setReadRemaining tracks the number of bytes remaining on the connection. If n
|
|
|
|
// overflows, an ErrReadLimit is returned.
|
|
|
|
func (c *Conn) setReadRemaining(n int64) error {
|
|
|
|
if n < 0 {
|
|
|
|
return ErrReadLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
c.readRemaining = n
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-14 19:48:43 +04:00
|
|
|
// Subprotocol returns the negotiated protocol for the connection.
|
|
|
|
func (c *Conn) Subprotocol() string {
|
|
|
|
return c.subprotocol
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:15:25 +03:00
|
|
|
// Close closes the underlying network connection without sending or waiting
|
|
|
|
// for a close message.
|
2013-10-16 20:41:47 +04:00
|
|
|
func (c *Conn) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the local network address.
|
|
|
|
func (c *Conn) LocalAddr() net.Addr {
|
|
|
|
return c.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address.
|
|
|
|
func (c *Conn) RemoteAddr() net.Addr {
|
|
|
|
return c.conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write methods
|
|
|
|
|
2016-11-02 19:41:43 +03:00
|
|
|
func (c *Conn) writeFatal(err error) error {
|
|
|
|
err = hideTempErr(err)
|
|
|
|
c.writeErrMu.Lock()
|
|
|
|
if c.writeErr == nil {
|
|
|
|
c.writeErr = err
|
|
|
|
}
|
|
|
|
c.writeErrMu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-23 00:10:37 +03:00
|
|
|
func (c *Conn) read(n int) ([]byte, error) {
|
|
|
|
p, err := c.br.Peek(n)
|
|
|
|
if err == io.EOF {
|
|
|
|
err = errUnexpectedEOF
|
|
|
|
}
|
2023-08-26 23:01:45 +03:00
|
|
|
if _, err := c.br.Discard(len(p)); err != nil {
|
|
|
|
return p, err
|
|
|
|
}
|
2018-08-23 00:10:37 +03:00
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
2018-03-06 21:15:48 +03:00
|
|
|
func (c *Conn) write(frameType int, deadline time.Time, buf0, buf1 []byte) error {
|
2013-10-16 20:41:47 +04:00
|
|
|
<-c.mu
|
2020-03-19 16:52:00 +03:00
|
|
|
defer func() { c.mu <- struct{}{} }()
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2016-11-02 19:41:43 +03:00
|
|
|
c.writeErrMu.Lock()
|
|
|
|
err := c.writeErr
|
|
|
|
c.writeErrMu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := c.conn.SetWriteDeadline(deadline); err != nil {
|
|
|
|
return c.writeFatal(err)
|
|
|
|
}
|
2018-03-06 21:15:48 +03:00
|
|
|
if len(buf1) == 0 {
|
|
|
|
_, err = c.conn.Write(buf0)
|
|
|
|
} else {
|
|
|
|
err = c.writeBufs(buf0, buf1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return c.writeFatal(err)
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
if frameType == CloseMessage {
|
2023-08-26 23:01:45 +03:00
|
|
|
_ = c.writeFatal(ErrCloseSent)
|
2016-11-02 19:41:43 +03:00
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 02:53:55 +03:00
|
|
|
func (c *Conn) writeBufs(bufs ...[]byte) error {
|
|
|
|
b := net.Buffers(bufs)
|
|
|
|
_, err := b.WriteTo(c.conn)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
// WriteControl writes a control message with the given deadline. The allowed
|
|
|
|
// message types are CloseMessage, PingMessage and PongMessage.
|
|
|
|
func (c *Conn) WriteControl(messageType int, data []byte, deadline time.Time) error {
|
|
|
|
if !isControl(messageType) {
|
|
|
|
return errBadWriteOpCode
|
|
|
|
}
|
|
|
|
if len(data) > maxControlFramePayloadSize {
|
|
|
|
return errInvalidControlFrame
|
|
|
|
}
|
|
|
|
|
|
|
|
b0 := byte(messageType) | finalBit
|
|
|
|
b1 := byte(len(data))
|
|
|
|
if !c.isServer {
|
|
|
|
b1 |= maskBit
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 0, maxFrameHeaderSize+maxControlFramePayloadSize)
|
|
|
|
buf = append(buf, b0, b1)
|
|
|
|
|
|
|
|
if c.isServer {
|
|
|
|
buf = append(buf, data...)
|
|
|
|
} else {
|
|
|
|
key := newMaskKey()
|
|
|
|
buf = append(buf, key[:]...)
|
|
|
|
buf = append(buf, data...)
|
|
|
|
maskBytes(key, 0, buf[6:])
|
|
|
|
}
|
|
|
|
|
2020-03-19 17:01:23 +03:00
|
|
|
d := 1000 * time.Hour
|
2013-10-16 20:41:47 +04:00
|
|
|
if !deadline.IsZero() {
|
2023-08-26 23:01:45 +03:00
|
|
|
d = time.Until(deadline)
|
2013-10-16 20:41:47 +04:00
|
|
|
if d < 0 {
|
|
|
|
return errWriteTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.mu:
|
2023-12-08 12:24:58 +03:00
|
|
|
default:
|
|
|
|
timer := time.NewTimer(d)
|
|
|
|
select {
|
|
|
|
case <-c.mu:
|
|
|
|
timer.Stop()
|
|
|
|
case <-timer.C:
|
|
|
|
return errWriteTimeout
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2023-12-08 12:24:58 +03:00
|
|
|
|
2020-03-19 16:52:00 +03:00
|
|
|
defer func() { c.mu <- struct{}{} }()
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2016-11-02 19:41:43 +03:00
|
|
|
c.writeErrMu.Lock()
|
|
|
|
err := c.writeErr
|
|
|
|
c.writeErrMu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := c.conn.SetWriteDeadline(deadline); err != nil {
|
|
|
|
return c.writeFatal(err)
|
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
_, err = c.conn.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
return c.writeFatal(err)
|
|
|
|
}
|
|
|
|
if messageType == CloseMessage {
|
2023-08-26 23:01:45 +03:00
|
|
|
_ = c.writeFatal(ErrCloseSent)
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
return err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2018-09-25 02:26:12 +03:00
|
|
|
// beginMessage prepares a connection and message writer for a new message.
|
|
|
|
func (c *Conn) beginMessage(mw *messageWriter, messageType int) error {
|
2016-06-30 03:03:55 +03:00
|
|
|
// Close previous writer if not already closed by the application. It's
|
|
|
|
// probably better to return an error in this situation, but we cannot
|
|
|
|
// change this without breaking existing applications.
|
|
|
|
if c.writer != nil {
|
2023-11-30 00:16:16 +03:00
|
|
|
_ = c.writer.Close()
|
2016-11-02 19:41:43 +03:00
|
|
|
c.writer = nil
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !isControl(messageType) && !isData(messageType) {
|
2016-12-08 03:21:58 +03:00
|
|
|
return errBadWriteOpCode
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:41:43 +03:00
|
|
|
c.writeErrMu.Lock()
|
|
|
|
err := c.writeErr
|
|
|
|
c.writeErrMu.Unlock()
|
2018-08-18 05:50:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-25 02:26:12 +03:00
|
|
|
mw.c = c
|
|
|
|
mw.frameType = messageType
|
|
|
|
mw.pos = maxFrameHeaderSize
|
|
|
|
|
2018-08-18 05:50:34 +03:00
|
|
|
if c.writeBuf == nil {
|
|
|
|
wpd, ok := c.writePool.Get().(writePoolData)
|
|
|
|
if ok {
|
|
|
|
c.writeBuf = wpd.buf
|
|
|
|
} else {
|
|
|
|
c.writeBuf = make([]byte, c.writeBufSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2016-12-08 03:21:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NextWriter returns a writer for the next message to send. The writer's Close
|
|
|
|
// method flushes the complete message to the network.
|
|
|
|
//
|
|
|
|
// There can be at most one open writer on a connection. NextWriter closes the
|
|
|
|
// previous writer if the application has not already done so.
|
2017-10-10 19:16:43 +03:00
|
|
|
//
|
|
|
|
// All message types (TextMessage, BinaryMessage, CloseMessage, PingMessage and
|
|
|
|
// PongMessage) are supported.
|
2016-12-08 03:21:58 +03:00
|
|
|
func (c *Conn) NextWriter(messageType int) (io.WriteCloser, error) {
|
2018-09-25 02:26:12 +03:00
|
|
|
var mw messageWriter
|
|
|
|
if err := c.beginMessage(&mw, messageType); err != nil {
|
2016-11-02 19:41:43 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-25 02:26:12 +03:00
|
|
|
c.writer = &mw
|
2016-06-30 03:03:55 +03:00
|
|
|
if c.newCompressionWriter != nil && c.enableWriteCompression && isData(messageType) {
|
2017-01-19 22:00:23 +03:00
|
|
|
w := c.newCompressionWriter(c.writer, c.compressionLevel)
|
2016-11-02 19:41:43 +03:00
|
|
|
mw.compress = true
|
|
|
|
c.writer = w
|
2016-06-30 03:03:55 +03:00
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
return c.writer, nil
|
|
|
|
}
|
2016-06-30 03:03:55 +03:00
|
|
|
|
2016-11-02 19:41:43 +03:00
|
|
|
type messageWriter struct {
|
|
|
|
c *Conn
|
|
|
|
compress bool // whether next call to flushFrame should set RSV1
|
|
|
|
pos int // end of data in writeBuf.
|
|
|
|
frameType int // type of the current frame.
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2018-09-25 02:26:12 +03:00
|
|
|
func (w *messageWriter) endMessage(err error) error {
|
2016-11-02 19:41:43 +03:00
|
|
|
if w.err != nil {
|
2018-09-25 02:26:12 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
c := w.c
|
|
|
|
w.err = err
|
|
|
|
c.writer = nil
|
|
|
|
if c.writePool != nil {
|
|
|
|
c.writePool.Put(writePoolData{buf: c.writeBuf})
|
|
|
|
c.writeBuf = nil
|
2016-11-02 19:41:43 +03:00
|
|
|
}
|
|
|
|
return err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2016-06-30 03:03:55 +03:00
|
|
|
// flushFrame writes buffered data and extra as a frame to the network. The
|
|
|
|
// final argument indicates that this is the last frame in the message.
|
2016-11-02 19:41:43 +03:00
|
|
|
func (w *messageWriter) flushFrame(final bool, extra []byte) error {
|
|
|
|
c := w.c
|
|
|
|
length := w.pos - maxFrameHeaderSize + len(extra)
|
2013-10-16 20:41:47 +04:00
|
|
|
|
|
|
|
// Check for invalid control frames.
|
2016-11-02 19:41:43 +03:00
|
|
|
if isControl(w.frameType) &&
|
2013-10-16 20:41:47 +04:00
|
|
|
(!final || length > maxControlFramePayloadSize) {
|
2018-09-25 02:26:12 +03:00
|
|
|
return w.endMessage(errInvalidControlFrame)
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:41:43 +03:00
|
|
|
b0 := byte(w.frameType)
|
2013-10-16 20:41:47 +04:00
|
|
|
if final {
|
|
|
|
b0 |= finalBit
|
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
if w.compress {
|
2016-06-30 03:03:55 +03:00
|
|
|
b0 |= rsv1Bit
|
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
w.compress = false
|
2016-06-30 03:03:55 +03:00
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
b1 := byte(0)
|
|
|
|
if !c.isServer {
|
|
|
|
b1 |= maskBit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume that the frame starts at beginning of c.writeBuf.
|
|
|
|
framePos := 0
|
|
|
|
if c.isServer {
|
|
|
|
// Adjust up if mask not included in the header.
|
|
|
|
framePos = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case length >= 65536:
|
|
|
|
c.writeBuf[framePos] = b0
|
|
|
|
c.writeBuf[framePos+1] = b1 | 127
|
|
|
|
binary.BigEndian.PutUint64(c.writeBuf[framePos+2:], uint64(length))
|
|
|
|
case length > 125:
|
|
|
|
framePos += 6
|
|
|
|
c.writeBuf[framePos] = b0
|
|
|
|
c.writeBuf[framePos+1] = b1 | 126
|
|
|
|
binary.BigEndian.PutUint16(c.writeBuf[framePos+2:], uint16(length))
|
|
|
|
default:
|
|
|
|
framePos += 8
|
|
|
|
c.writeBuf[framePos] = b0
|
|
|
|
c.writeBuf[framePos+1] = b1 | byte(length)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.isServer {
|
|
|
|
key := newMaskKey()
|
|
|
|
copy(c.writeBuf[maxFrameHeaderSize-4:], key[:])
|
2016-11-02 19:41:43 +03:00
|
|
|
maskBytes(key, 0, c.writeBuf[maxFrameHeaderSize:w.pos])
|
2013-10-16 20:41:47 +04:00
|
|
|
if len(extra) > 0 {
|
2018-09-25 02:26:12 +03:00
|
|
|
return w.endMessage(c.writeFatal(errors.New("websocket: internal error, extra used in client mode")))
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 02:03:54 +03:00
|
|
|
// Write the buffers to the connection with best-effort detection of
|
|
|
|
// concurrent writes. See the concurrency section in the package
|
|
|
|
// documentation for more info.
|
|
|
|
|
|
|
|
if c.isWriting {
|
|
|
|
panic("concurrent write to websocket connection")
|
|
|
|
}
|
|
|
|
c.isWriting = true
|
|
|
|
|
2016-11-02 19:41:43 +03:00
|
|
|
err := c.write(w.frameType, c.writeDeadline, c.writeBuf[framePos:w.pos], extra)
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2016-02-17 02:03:54 +03:00
|
|
|
if !c.isWriting {
|
|
|
|
panic("concurrent write to websocket connection")
|
|
|
|
}
|
|
|
|
c.isWriting = false
|
|
|
|
|
2016-11-02 19:41:43 +03:00
|
|
|
if err != nil {
|
2018-09-25 02:26:12 +03:00
|
|
|
return w.endMessage(err)
|
2016-11-02 19:41:43 +03:00
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
if final {
|
2023-08-26 23:01:45 +03:00
|
|
|
_ = w.endMessage(errWriteClosed)
|
2016-11-02 19:41:43 +03:00
|
|
|
return nil
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2016-11-02 19:41:43 +03:00
|
|
|
// Setup for next frame.
|
|
|
|
w.pos = maxFrameHeaderSize
|
|
|
|
w.frameType = continuationFrame
|
2013-10-16 20:41:47 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
func (w *messageWriter) ncopy(max int) (int, error) {
|
2016-11-02 19:41:43 +03:00
|
|
|
n := len(w.c.writeBuf) - w.pos
|
2013-10-16 20:41:47 +04:00
|
|
|
if n <= 0 {
|
2016-11-02 19:41:43 +03:00
|
|
|
if err := w.flushFrame(false, nil); err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
return 0, err
|
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
n = len(w.c.writeBuf) - w.pos
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
if n > max {
|
|
|
|
n = max
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2016-06-30 03:03:55 +03:00
|
|
|
func (w *messageWriter) Write(p []byte) (int, error) {
|
2016-11-02 19:41:43 +03:00
|
|
|
if w.err != nil {
|
|
|
|
return 0, w.err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(p) > 2*len(w.c.writeBuf) && w.c.isServer {
|
|
|
|
// Don't buffer large messages.
|
2016-11-02 19:41:43 +03:00
|
|
|
err := w.flushFrame(false, p)
|
2013-10-16 20:41:47 +04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nn := len(p)
|
|
|
|
for len(p) > 0 {
|
|
|
|
n, err := w.ncopy(len(p))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
copy(w.c.writeBuf[w.pos:], p[:n])
|
|
|
|
w.pos += n
|
2013-10-16 20:41:47 +04:00
|
|
|
p = p[n:]
|
|
|
|
}
|
|
|
|
return nn, nil
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
func (w *messageWriter) WriteString(p string) (int, error) {
|
2016-11-02 19:41:43 +03:00
|
|
|
if w.err != nil {
|
|
|
|
return 0, w.err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nn := len(p)
|
|
|
|
for len(p) > 0 {
|
|
|
|
n, err := w.ncopy(len(p))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
copy(w.c.writeBuf[w.pos:], p[:n])
|
|
|
|
w.pos += n
|
2013-10-16 20:41:47 +04:00
|
|
|
p = p[n:]
|
|
|
|
}
|
|
|
|
return nn, nil
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
func (w *messageWriter) ReadFrom(r io.Reader) (nn int64, err error) {
|
2016-11-02 19:41:43 +03:00
|
|
|
if w.err != nil {
|
|
|
|
return 0, w.err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
for {
|
2016-11-02 19:41:43 +03:00
|
|
|
if w.pos == len(w.c.writeBuf) {
|
|
|
|
err = w.flushFrame(false, nil)
|
2013-10-16 20:41:47 +04:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var n int
|
2016-11-02 19:41:43 +03:00
|
|
|
n, err = r.Read(w.c.writeBuf[w.pos:])
|
|
|
|
w.pos += n
|
2013-10-16 20:41:47 +04:00
|
|
|
nn += int64(n)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nn, err
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
func (w *messageWriter) Close() error {
|
2016-11-02 19:41:43 +03:00
|
|
|
if w.err != nil {
|
|
|
|
return w.err
|
|
|
|
}
|
2019-04-27 07:02:11 +03:00
|
|
|
return w.flushFrame(true, nil)
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2017-02-14 20:41:18 +03:00
|
|
|
// WritePreparedMessage writes prepared message into connection.
|
|
|
|
func (c *Conn) WritePreparedMessage(pm *PreparedMessage) error {
|
|
|
|
frameType, frameData, err := pm.frame(prepareKey{
|
|
|
|
isServer: c.isServer,
|
|
|
|
compress: c.newCompressionWriter != nil && c.enableWriteCompression && isData(pm.messageType),
|
|
|
|
compressionLevel: c.compressionLevel,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.isWriting {
|
|
|
|
panic("concurrent write to websocket connection")
|
|
|
|
}
|
|
|
|
c.isWriting = true
|
|
|
|
err = c.write(frameType, c.writeDeadline, frameData, nil)
|
|
|
|
if !c.isWriting {
|
|
|
|
panic("concurrent write to websocket connection")
|
|
|
|
}
|
|
|
|
c.isWriting = false
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
// WriteMessage is a helper method for getting a writer using NextWriter,
|
|
|
|
// writing the message and closing the writer.
|
|
|
|
func (c *Conn) WriteMessage(messageType int, data []byte) error {
|
2016-12-08 03:21:58 +03:00
|
|
|
|
|
|
|
if c.isServer && (c.newCompressionWriter == nil || !c.enableWriteCompression) {
|
|
|
|
// Fast path with no allocations and single frame.
|
|
|
|
|
2018-09-25 02:26:12 +03:00
|
|
|
var mw messageWriter
|
|
|
|
if err := c.beginMessage(&mw, messageType); err != nil {
|
2016-12-08 03:21:58 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-11-02 19:41:43 +03:00
|
|
|
n := copy(c.writeBuf[mw.pos:], data)
|
|
|
|
mw.pos += n
|
2016-05-31 15:14:41 +03:00
|
|
|
data = data[n:]
|
2016-12-08 03:21:58 +03:00
|
|
|
return mw.flushFrame(true, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := c.NextWriter(messageType)
|
|
|
|
if err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
return err
|
|
|
|
}
|
2016-05-31 15:14:41 +03:00
|
|
|
if _, err = w.Write(data); err != nil {
|
|
|
|
return err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2016-05-31 15:14:41 +03:00
|
|
|
return w.Close()
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2014-06-06 02:19:54 +04:00
|
|
|
// SetWriteDeadline sets the write deadline on the underlying network
|
|
|
|
// connection. After a write has timed out, the websocket state is corrupt and
|
|
|
|
// all future writes will return an error. A zero value for t means writes will
|
2014-11-03 18:48:34 +03:00
|
|
|
// not time out.
|
2013-10-16 20:41:47 +04:00
|
|
|
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
|
|
|
c.writeDeadline = t
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read methods
|
|
|
|
|
|
|
|
func (c *Conn) advanceFrame() (int, error) {
|
|
|
|
// 1. Skip remainder of previous frame.
|
|
|
|
|
|
|
|
if c.readRemaining > 0 {
|
2023-08-26 23:01:45 +03:00
|
|
|
if _, err := io.CopyN(io.Discard, c.br, c.readRemaining); err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
return noFrame, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Read and parse first two bytes of frame header.
|
2022-01-02 23:16:08 +03:00
|
|
|
// To aid debugging, collect and report all errors in the first two bytes
|
|
|
|
// of the header.
|
|
|
|
|
|
|
|
var errors []string
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
p, err := c.read(2)
|
|
|
|
if err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
return noFrame, err
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
frameType := int(p[0] & 0xf)
|
2022-01-02 23:16:08 +03:00
|
|
|
final := p[0]&finalBit != 0
|
|
|
|
rsv1 := p[0]&rsv1Bit != 0
|
|
|
|
rsv2 := p[0]&rsv2Bit != 0
|
|
|
|
rsv3 := p[0]&rsv3Bit != 0
|
2016-05-31 15:14:41 +03:00
|
|
|
mask := p[1]&maskBit != 0
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := c.setReadRemaining(int64(p[1] & 0x7f)); err != nil {
|
|
|
|
return noFrame, err
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2016-06-30 03:03:55 +03:00
|
|
|
c.readDecompress = false
|
2022-01-02 23:16:08 +03:00
|
|
|
if rsv1 {
|
|
|
|
if c.newDecompressionReader != nil {
|
|
|
|
c.readDecompress = true
|
|
|
|
} else {
|
|
|
|
errors = append(errors, "RSV1 set")
|
|
|
|
}
|
2016-06-30 03:03:55 +03:00
|
|
|
}
|
|
|
|
|
2022-01-02 23:16:08 +03:00
|
|
|
if rsv2 {
|
|
|
|
errors = append(errors, "RSV2 set")
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsv3 {
|
|
|
|
errors = append(errors, "RSV3 set")
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch frameType {
|
|
|
|
case CloseMessage, PingMessage, PongMessage:
|
|
|
|
if c.readRemaining > maxControlFramePayloadSize {
|
2022-01-02 23:16:08 +03:00
|
|
|
errors = append(errors, "len > 125 for control")
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
if !final {
|
2022-01-02 23:16:08 +03:00
|
|
|
errors = append(errors, "FIN not set on control")
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
case TextMessage, BinaryMessage:
|
|
|
|
if !c.readFinal {
|
2022-01-02 23:16:08 +03:00
|
|
|
errors = append(errors, "data before FIN")
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
c.readFinal = final
|
|
|
|
case continuationFrame:
|
|
|
|
if c.readFinal {
|
2022-01-02 23:16:08 +03:00
|
|
|
errors = append(errors, "continuation after FIN")
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
c.readFinal = final
|
|
|
|
default:
|
2022-01-02 23:16:08 +03:00
|
|
|
errors = append(errors, "bad opcode "+strconv.Itoa(frameType))
|
|
|
|
}
|
|
|
|
|
|
|
|
if mask != c.isServer {
|
|
|
|
errors = append(errors, "bad MASK")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return noFrame, c.handleProtocolError(strings.Join(errors, ", "))
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2019-08-25 04:17:28 +03:00
|
|
|
// 3. Read and parse frame length as per
|
|
|
|
// https://tools.ietf.org/html/rfc6455#section-5.2
|
|
|
|
//
|
|
|
|
// The length of the "Payload data", in bytes: if 0-125, that is the payload
|
|
|
|
// length.
|
|
|
|
// - If 126, the following 2 bytes interpreted as a 16-bit unsigned
|
|
|
|
// integer are the payload length.
|
|
|
|
// - If 127, the following 8 bytes interpreted as
|
|
|
|
// a 64-bit unsigned integer (the most significant bit MUST be 0) are the
|
|
|
|
// payload length. Multibyte length quantities are expressed in network byte
|
|
|
|
// order.
|
2013-10-16 20:41:47 +04:00
|
|
|
|
|
|
|
switch c.readRemaining {
|
|
|
|
case 126:
|
2016-05-31 15:14:41 +03:00
|
|
|
p, err := c.read(2)
|
|
|
|
if err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
return noFrame, err
|
|
|
|
}
|
2019-08-25 04:17:28 +03:00
|
|
|
|
|
|
|
if err := c.setReadRemaining(int64(binary.BigEndian.Uint16(p))); err != nil {
|
|
|
|
return noFrame, err
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
case 127:
|
2016-05-31 15:14:41 +03:00
|
|
|
p, err := c.read(8)
|
|
|
|
if err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
return noFrame, err
|
|
|
|
}
|
2019-08-25 04:17:28 +03:00
|
|
|
|
|
|
|
if err := c.setReadRemaining(int64(binary.BigEndian.Uint64(p))); err != nil {
|
|
|
|
return noFrame, err
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 4. Handle frame masking.
|
|
|
|
|
|
|
|
if mask {
|
|
|
|
c.readMaskPos = 0
|
2016-05-31 15:14:41 +03:00
|
|
|
p, err := c.read(len(c.readMaskKey))
|
|
|
|
if err != nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
return noFrame, err
|
|
|
|
}
|
2016-05-31 15:14:41 +03:00
|
|
|
copy(c.readMaskKey[:], p)
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 5. For text and binary messages, enforce read limit and return.
|
|
|
|
|
|
|
|
if frameType == continuationFrame || frameType == TextMessage || frameType == BinaryMessage {
|
|
|
|
|
|
|
|
c.readLength += c.readRemaining
|
2019-08-25 04:17:28 +03:00
|
|
|
// Don't allow readLength to overflow in the presence of a large readRemaining
|
|
|
|
// counter.
|
|
|
|
if c.readLength < 0 {
|
|
|
|
return noFrame, ErrReadLimit
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
if c.readLimit > 0 && c.readLength > c.readLimit {
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := c.WriteControl(CloseMessage, FormatCloseMessage(CloseMessageTooBig, ""), time.Now().Add(writeWait)); err != nil {
|
|
|
|
return noFrame, err
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
return noFrame, ErrReadLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
return frameType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 6. Read control frame payload.
|
|
|
|
|
2014-06-06 20:12:15 +04:00
|
|
|
var payload []byte
|
|
|
|
if c.readRemaining > 0 {
|
2016-05-31 15:14:41 +03:00
|
|
|
payload, err = c.read(int(c.readRemaining))
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := c.setReadRemaining(0); err != nil {
|
|
|
|
return noFrame, err
|
|
|
|
}
|
2016-05-31 15:14:41 +03:00
|
|
|
if err != nil {
|
2014-06-06 20:12:15 +04:00
|
|
|
return noFrame, err
|
|
|
|
}
|
2014-07-06 00:56:34 +04:00
|
|
|
if c.isServer {
|
|
|
|
maskBytes(c.readMaskKey, 0, payload)
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 7. Process control frame payload.
|
|
|
|
|
|
|
|
switch frameType {
|
|
|
|
case PongMessage:
|
|
|
|
if err := c.handlePong(string(payload)); err != nil {
|
|
|
|
return noFrame, err
|
|
|
|
}
|
|
|
|
case PingMessage:
|
|
|
|
if err := c.handlePing(string(payload)); err != nil {
|
|
|
|
return noFrame, err
|
|
|
|
}
|
|
|
|
case CloseMessage:
|
2014-11-01 00:52:20 +03:00
|
|
|
closeCode := CloseNoStatusReceived
|
|
|
|
closeText := ""
|
|
|
|
if len(payload) >= 2 {
|
|
|
|
closeCode = int(binary.BigEndian.Uint16(payload))
|
2016-04-28 19:34:18 +03:00
|
|
|
if !isValidReceivedCloseCode(closeCode) {
|
2022-01-02 23:16:08 +03:00
|
|
|
return noFrame, c.handleProtocolError("bad close code " + strconv.Itoa(closeCode))
|
2016-04-28 19:34:18 +03:00
|
|
|
}
|
2014-11-01 00:52:20 +03:00
|
|
|
closeText = string(payload[2:])
|
2016-04-28 19:34:18 +03:00
|
|
|
if !utf8.ValidString(closeText) {
|
|
|
|
return noFrame, c.handleProtocolError("invalid utf8 payload in close frame")
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2016-11-02 23:04:59 +03:00
|
|
|
if err := c.handleClose(closeCode, closeText); err != nil {
|
|
|
|
return noFrame, err
|
|
|
|
}
|
2015-08-11 20:14:32 +03:00
|
|
|
return noFrame, &CloseError{Code: closeCode, Text: closeText}
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return frameType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) handleProtocolError(message string) error {
|
2022-01-02 23:16:08 +03:00
|
|
|
data := FormatCloseMessage(CloseProtocolError, message)
|
|
|
|
if len(data) > maxControlFramePayloadSize {
|
|
|
|
data = data[:maxControlFramePayloadSize]
|
|
|
|
}
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := c.WriteControl(CloseMessage, data, time.Now().Add(writeWait)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
return errors.New("websocket: " + message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextReader returns the next data message received from the peer. The
|
|
|
|
// returned messageType is either TextMessage or BinaryMessage.
|
|
|
|
//
|
|
|
|
// There can be at most one open reader on a connection. NextReader discards
|
|
|
|
// the previous message if the application has not already consumed it.
|
|
|
|
//
|
2016-02-17 02:03:54 +03:00
|
|
|
// Applications must break out of the application's read loop when this method
|
|
|
|
// returns a non-nil error value. Errors returned from this method are
|
|
|
|
// permanent. Once this method returns a non-nil error, all subsequent calls to
|
|
|
|
// this method return the same error.
|
2013-10-16 20:41:47 +04:00
|
|
|
func (c *Conn) NextReader() (messageType int, r io.Reader, err error) {
|
2016-12-18 02:33:06 +03:00
|
|
|
// Close previous reader, only relevant for decompression.
|
|
|
|
if c.reader != nil {
|
2023-11-30 00:16:16 +03:00
|
|
|
_ = c.reader.Close()
|
2016-12-18 02:33:06 +03:00
|
|
|
c.reader = nil
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
c.messageReader = nil
|
2013-10-16 20:41:47 +04:00
|
|
|
c.readLength = 0
|
|
|
|
|
|
|
|
for c.readErr == nil {
|
2014-06-06 20:12:15 +04:00
|
|
|
frameType, err := c.advanceFrame()
|
|
|
|
if err != nil {
|
|
|
|
c.readErr = hideTempErr(err)
|
|
|
|
break
|
|
|
|
}
|
2019-08-25 04:17:28 +03:00
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
if frameType == TextMessage || frameType == BinaryMessage {
|
2016-06-30 03:03:55 +03:00
|
|
|
c.messageReader = &messageReader{c}
|
2016-12-18 02:33:06 +03:00
|
|
|
c.reader = c.messageReader
|
2016-06-30 03:03:55 +03:00
|
|
|
if c.readDecompress {
|
2016-12-18 02:33:06 +03:00
|
|
|
c.reader = c.newDecompressionReader(c.reader)
|
2016-06-30 03:03:55 +03:00
|
|
|
}
|
2016-12-18 02:33:06 +03:00
|
|
|
return frameType, c.reader, nil
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
}
|
2016-02-17 02:03:54 +03:00
|
|
|
|
|
|
|
// Applications that do handle the error returned from this method spin in
|
|
|
|
// tight loop on connection failure. To help application developers detect
|
|
|
|
// this error, panic on repeated reads to the failed connection.
|
|
|
|
c.readErrCount++
|
|
|
|
if c.readErrCount >= 1000 {
|
|
|
|
panic("repeated read on failed websocket connection")
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
return noFrame, nil, c.readErr
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
type messageReader struct{ c *Conn }
|
2016-05-30 11:53:33 +03:00
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
func (r *messageReader) Read(b []byte) (int, error) {
|
|
|
|
c := r.c
|
|
|
|
if c.messageReader != r {
|
2013-10-16 20:41:47 +04:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
for c.readErr == nil {
|
2013-10-16 20:41:47 +04:00
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
if c.readRemaining > 0 {
|
|
|
|
if int64(len(b)) > c.readRemaining {
|
|
|
|
b = b[:c.readRemaining]
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
2016-05-31 15:14:41 +03:00
|
|
|
n, err := c.br.Read(b)
|
|
|
|
c.readErr = hideTempErr(err)
|
|
|
|
if c.isServer {
|
|
|
|
c.readMaskPos = maskBytes(c.readMaskKey, c.readMaskPos, b[:n])
|
2014-07-06 00:56:34 +04:00
|
|
|
}
|
2019-08-25 04:17:28 +03:00
|
|
|
rem := c.readRemaining
|
|
|
|
rem -= int64(n)
|
2023-08-26 23:01:45 +03:00
|
|
|
if err := c.setReadRemaining(rem); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-05-31 15:14:41 +03:00
|
|
|
if c.readRemaining > 0 && c.readErr == io.EOF {
|
|
|
|
c.readErr = errUnexpectedEOF
|
2016-04-25 08:30:05 +03:00
|
|
|
}
|
2016-05-31 15:14:41 +03:00
|
|
|
return n, c.readErr
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
if c.readFinal {
|
|
|
|
c.messageReader = nil
|
2013-10-16 20:41:47 +04:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
frameType, err := c.advanceFrame()
|
2014-07-11 06:36:51 +04:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
2016-05-31 15:14:41 +03:00
|
|
|
c.readErr = hideTempErr(err)
|
2014-07-11 06:36:51 +04:00
|
|
|
case frameType == TextMessage || frameType == BinaryMessage:
|
2016-05-31 15:14:41 +03:00
|
|
|
c.readErr = errors.New("websocket: internal error, unexpected text or binary in Reader")
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-11 06:36:51 +04:00
|
|
|
|
2016-05-31 15:14:41 +03:00
|
|
|
err := c.readErr
|
|
|
|
if err == io.EOF && c.messageReader == r {
|
2014-11-01 00:52:20 +03:00
|
|
|
err = errUnexpectedEOF
|
2014-07-11 06:36:51 +04:00
|
|
|
}
|
|
|
|
return 0, err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
|
2016-12-18 02:33:06 +03:00
|
|
|
func (r *messageReader) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
// ReadMessage is a helper method for getting a reader using NextReader and
|
|
|
|
// reading from that reader to a buffer.
|
|
|
|
func (c *Conn) ReadMessage() (messageType int, p []byte, err error) {
|
|
|
|
var r io.Reader
|
|
|
|
messageType, r, err = c.NextReader()
|
|
|
|
if err != nil {
|
|
|
|
return messageType, nil, err
|
|
|
|
}
|
2023-08-26 23:01:45 +03:00
|
|
|
p, err = io.ReadAll(r)
|
2013-10-16 20:41:47 +04:00
|
|
|
return messageType, p, err
|
|
|
|
}
|
|
|
|
|
2014-06-06 02:19:54 +04:00
|
|
|
// SetReadDeadline sets the read deadline on the underlying network connection.
|
|
|
|
// After a read has timed out, the websocket connection state is corrupt and
|
|
|
|
// all future reads will return an error. A zero value for t means reads will
|
|
|
|
// not time out.
|
2013-10-16 20:41:47 +04:00
|
|
|
func (c *Conn) SetReadDeadline(t time.Time) error {
|
|
|
|
return c.conn.SetReadDeadline(t)
|
|
|
|
}
|
|
|
|
|
2018-10-30 17:45:53 +03:00
|
|
|
// SetReadLimit sets the maximum size in bytes for a message read from the peer. If a
|
2018-01-10 17:15:25 +03:00
|
|
|
// message exceeds the limit, the connection sends a close message to the peer
|
2013-10-16 20:41:47 +04:00
|
|
|
// and returns ErrReadLimit to the application.
|
|
|
|
func (c *Conn) SetReadLimit(limit int64) {
|
|
|
|
c.readLimit = limit
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:04:59 +03:00
|
|
|
// CloseHandler returns the current close handler
|
|
|
|
func (c *Conn) CloseHandler() func(code int, text string) error {
|
|
|
|
return c.handleClose
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCloseHandler sets the handler for close messages received from the peer.
|
|
|
|
// The code argument to h is the received close code or CloseNoStatusReceived
|
2018-01-10 17:15:25 +03:00
|
|
|
// if the close message is empty. The default close handler sends a close
|
|
|
|
// message back to the peer.
|
2017-01-16 00:09:21 +03:00
|
|
|
//
|
2018-02-01 04:52:56 +03:00
|
|
|
// The handler function is called from the NextReader, ReadMessage and message
|
|
|
|
// reader Read methods. The application must read the connection to process
|
|
|
|
// close messages as described in the section on Control Messages above.
|
2017-01-16 00:09:21 +03:00
|
|
|
//
|
2018-01-10 17:15:25 +03:00
|
|
|
// The connection read methods return a CloseError when a close message is
|
2017-01-16 00:09:21 +03:00
|
|
|
// received. Most applications should handle close messages as part of their
|
|
|
|
// normal error handling. Applications should only set a close handler when the
|
2018-01-10 17:15:25 +03:00
|
|
|
// application must perform some action before sending a close message back to
|
2017-01-16 00:09:21 +03:00
|
|
|
// the peer.
|
2016-11-02 23:04:59 +03:00
|
|
|
func (c *Conn) SetCloseHandler(h func(code int, text string) error) {
|
|
|
|
if h == nil {
|
|
|
|
h = func(code int, text string) error {
|
2017-12-28 18:29:59 +03:00
|
|
|
message := FormatCloseMessage(code, "")
|
2023-12-11 07:39:50 +03:00
|
|
|
_ = c.WriteControl(CloseMessage, message, time.Now().Add(writeWait))
|
2016-11-02 23:04:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.handleClose = h
|
|
|
|
}
|
|
|
|
|
2016-05-28 18:59:17 +03:00
|
|
|
// PingHandler returns the current ping handler
|
|
|
|
func (c *Conn) PingHandler() func(appData string) error {
|
|
|
|
return c.handlePing
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
// SetPingHandler sets the handler for ping messages received from the peer.
|
2018-01-10 17:15:25 +03:00
|
|
|
// The appData argument to h is the PING message application data. The default
|
2015-09-15 21:02:48 +03:00
|
|
|
// ping handler sends a pong to the peer.
|
2017-01-16 00:09:21 +03:00
|
|
|
//
|
2018-02-01 04:52:56 +03:00
|
|
|
// The handler function is called from the NextReader, ReadMessage and message
|
|
|
|
// reader Read methods. The application must read the connection to process
|
|
|
|
// ping messages as described in the section on Control Messages above.
|
2015-09-15 21:02:48 +03:00
|
|
|
func (c *Conn) SetPingHandler(h func(appData string) error) {
|
2013-10-16 20:41:47 +04:00
|
|
|
if h == nil {
|
|
|
|
h = func(message string) error {
|
2015-10-15 20:14:19 +03:00
|
|
|
err := c.WriteControl(PongMessage, []byte(message), time.Now().Add(writeWait))
|
|
|
|
if err == ErrCloseSent {
|
|
|
|
return nil
|
2023-08-26 23:01:45 +03:00
|
|
|
} else if _, ok := err.(net.Error); ok {
|
2015-10-15 20:14:19 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2013-10-16 20:41:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.handlePing = h
|
|
|
|
}
|
|
|
|
|
2016-05-28 18:59:17 +03:00
|
|
|
// PongHandler returns the current pong handler
|
|
|
|
func (c *Conn) PongHandler() func(appData string) error {
|
|
|
|
return c.handlePong
|
|
|
|
}
|
|
|
|
|
2015-04-18 08:58:37 +03:00
|
|
|
// SetPongHandler sets the handler for pong messages received from the peer.
|
2018-01-10 17:15:25 +03:00
|
|
|
// The appData argument to h is the PONG message application data. The default
|
2015-09-15 21:02:48 +03:00
|
|
|
// pong handler does nothing.
|
2017-01-16 00:09:21 +03:00
|
|
|
//
|
2018-02-01 04:52:56 +03:00
|
|
|
// The handler function is called from the NextReader, ReadMessage and message
|
|
|
|
// reader Read methods. The application must read the connection to process
|
|
|
|
// pong messages as described in the section on Control Messages above.
|
2015-09-15 21:02:48 +03:00
|
|
|
func (c *Conn) SetPongHandler(h func(appData string) error) {
|
2013-10-16 20:41:47 +04:00
|
|
|
if h == nil {
|
|
|
|
h = func(string) error { return nil }
|
|
|
|
}
|
|
|
|
c.handlePong = h
|
|
|
|
}
|
|
|
|
|
2022-04-17 16:01:17 +03:00
|
|
|
// NetConn returns the underlying connection that is wrapped by c.
|
|
|
|
// Note that writing to or reading from this connection directly will corrupt the
|
|
|
|
// WebSocket connection.
|
|
|
|
func (c *Conn) NetConn() net.Conn {
|
|
|
|
return c.conn
|
|
|
|
}
|
|
|
|
|
2014-03-18 18:26:10 +04:00
|
|
|
// UnderlyingConn returns the internal net.Conn. This can be used to further
|
|
|
|
// modifications to connection specific flags.
|
2022-04-17 16:01:17 +03:00
|
|
|
// Deprecated: Use the NetConn method.
|
2014-03-18 18:26:10 +04:00
|
|
|
func (c *Conn) UnderlyingConn() net.Conn {
|
|
|
|
return c.conn
|
|
|
|
}
|
|
|
|
|
2016-09-30 07:52:15 +03:00
|
|
|
// EnableWriteCompression enables and disables write compression of
|
|
|
|
// subsequent text and binary messages. This function is a noop if
|
|
|
|
// compression was not negotiated with the peer.
|
|
|
|
func (c *Conn) EnableWriteCompression(enable bool) {
|
|
|
|
c.enableWriteCompression = enable
|
|
|
|
}
|
|
|
|
|
2017-01-19 22:00:23 +03:00
|
|
|
// SetCompressionLevel sets the flate compression level for subsequent text and
|
|
|
|
// binary messages. This function is a noop if compression was not negotiated
|
2017-01-19 22:27:08 +03:00
|
|
|
// with the peer. See the compress/flate package for a description of
|
|
|
|
// compression levels.
|
2017-01-19 22:00:23 +03:00
|
|
|
func (c *Conn) SetCompressionLevel(level int) error {
|
|
|
|
if !isValidCompressionLevel(level) {
|
|
|
|
return errors.New("websocket: invalid compression level")
|
|
|
|
}
|
|
|
|
c.compressionLevel = level
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:41:47 +04:00
|
|
|
// FormatCloseMessage formats closeCode and text as a WebSocket close message.
|
2017-12-28 18:29:59 +03:00
|
|
|
// An empty message is returned for code CloseNoStatusReceived.
|
2013-10-16 20:41:47 +04:00
|
|
|
func FormatCloseMessage(closeCode int, text string) []byte {
|
2017-12-28 18:29:59 +03:00
|
|
|
if closeCode == CloseNoStatusReceived {
|
|
|
|
// Return empty message because it's illegal to send
|
|
|
|
// CloseNoStatusReceived. Return non-nil value in case application
|
|
|
|
// checks for nil.
|
|
|
|
return []byte{}
|
|
|
|
}
|
2013-10-16 20:41:47 +04:00
|
|
|
buf := make([]byte, 2+len(text))
|
|
|
|
binary.BigEndian.PutUint16(buf, uint16(closeCode))
|
|
|
|
copy(buf[2:], text)
|
|
|
|
return buf
|
|
|
|
}
|
2024-01-23 09:50:52 +03:00
|
|
|
|
|
|
|
var messageTypes = map[int]string{
|
|
|
|
TextMessage: "TextMessage",
|
|
|
|
BinaryMessage: "BinaryMessage",
|
|
|
|
CloseMessage: "CloseMessage",
|
|
|
|
PingMessage: "PingMessage",
|
|
|
|
PongMessage: "PongMessage",
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatMessageType(mt int) string {
|
|
|
|
return messageTypes[mt]
|
|
|
|
}
|