package ws
import (
"bytes"
"encoding/binary"
"math/rand"
)
const (
MaxControlFramePayloadSize = 125
)
type OpCode byte
const (
OpContinuation OpCode = 0x0
OpText OpCode = 0x1
OpBinary OpCode = 0x2
OpClose OpCode = 0x8
OpPing OpCode = 0x9
OpPong OpCode = 0xa
)
func (c OpCode ) IsControl () bool {
return c &0x8 != 0
}
func (c OpCode ) IsData () bool {
return c &0x8 == 0
}
func (c OpCode ) IsReserved () bool {
return (0x3 <= c && c <= 0x7 ) || (0xb <= c && c <= 0xf )
}
type StatusCode uint16
type StatusCodeRange struct {
Min, Max StatusCode
}
var (
StatusRangeNotInUse = StatusCodeRange {0 , 999 }
StatusRangeProtocol = StatusCodeRange {1000 , 2999 }
StatusRangeApplication = StatusCodeRange {3000 , 3999 }
StatusRangePrivate = StatusCodeRange {4000 , 4999 }
)
const (
StatusNormalClosure StatusCode = 1000
StatusGoingAway StatusCode = 1001
StatusProtocolError StatusCode = 1002
StatusUnsupportedData StatusCode = 1003
StatusNoMeaningYet StatusCode = 1004
StatusInvalidFramePayloadData StatusCode = 1007
StatusPolicyViolation StatusCode = 1008
StatusMessageTooBig StatusCode = 1009
StatusMandatoryExt StatusCode = 1010
StatusInternalServerError StatusCode = 1011
StatusTLSHandshake StatusCode = 1015
StatusAbnormalClosure StatusCode = 1006
StatusNoStatusRcvd StatusCode = 1005
)
func (s StatusCode ) In (r StatusCodeRange ) bool {
return r .Min <= s && s <= r .Max
}
func (s StatusCode ) Empty () bool {
return s == 0
}
func (s StatusCode ) IsNotUsed () bool {
return s .In (StatusRangeNotInUse )
}
func (s StatusCode ) IsApplicationSpec () bool {
return s .In (StatusRangeApplication )
}
func (s StatusCode ) IsPrivateSpec () bool {
return s .In (StatusRangePrivate )
}
func (s StatusCode ) IsProtocolSpec () bool {
return s .In (StatusRangeProtocol )
}
func (s StatusCode ) IsProtocolDefined () bool {
switch s {
case StatusNormalClosure ,
StatusGoingAway ,
StatusProtocolError ,
StatusUnsupportedData ,
StatusInvalidFramePayloadData ,
StatusPolicyViolation ,
StatusMessageTooBig ,
StatusMandatoryExt ,
StatusInternalServerError ,
StatusNoStatusRcvd ,
StatusAbnormalClosure ,
StatusTLSHandshake :
return true
}
return false
}
func (s StatusCode ) IsProtocolReserved () bool {
switch s {
case StatusNoStatusRcvd , StatusAbnormalClosure , StatusTLSHandshake :
return true
default :
return false
}
}
var (
CompiledPing = MustCompileFrame (NewPingFrame (nil ))
CompiledPong = MustCompileFrame (NewPongFrame (nil ))
CompiledClose = MustCompileFrame (NewCloseFrame (nil ))
CompiledCloseNormalClosure = MustCompileFrame (closeFrameNormalClosure )
CompiledCloseGoingAway = MustCompileFrame (closeFrameGoingAway )
CompiledCloseProtocolError = MustCompileFrame (closeFrameProtocolError )
CompiledCloseUnsupportedData = MustCompileFrame (closeFrameUnsupportedData )
CompiledCloseNoMeaningYet = MustCompileFrame (closeFrameNoMeaningYet )
CompiledCloseInvalidFramePayloadData = MustCompileFrame (closeFrameInvalidFramePayloadData )
CompiledClosePolicyViolation = MustCompileFrame (closeFramePolicyViolation )
CompiledCloseMessageTooBig = MustCompileFrame (closeFrameMessageTooBig )
CompiledCloseMandatoryExt = MustCompileFrame (closeFrameMandatoryExt )
CompiledCloseInternalServerError = MustCompileFrame (closeFrameInternalServerError )
CompiledCloseTLSHandshake = MustCompileFrame (closeFrameTLSHandshake )
)
type Header struct {
Fin bool
Rsv byte
OpCode OpCode
Masked bool
Mask [4 ]byte
Length int64
}
func (h Header ) Rsv1 () bool { return h .Rsv &bit5 != 0 }
func (h Header ) Rsv2 () bool { return h .Rsv &bit6 != 0 }
func (h Header ) Rsv3 () bool { return h .Rsv &bit7 != 0 }
func Rsv (r1 , r2 , r3 bool ) (rsv byte ) {
if r1 {
rsv |= bit5
}
if r2 {
rsv |= bit6
}
if r3 {
rsv |= bit7
}
return rsv
}
func RsvBits (rsv byte ) (r1 , r2 , r3 bool ) {
r1 = rsv &bit5 != 0
r2 = rsv &bit6 != 0
r3 = rsv &bit7 != 0
return r1 , r2 , r3
}
type Frame struct {
Header Header
Payload []byte
}
func NewFrame (op OpCode , fin bool , p []byte ) Frame {
return Frame {
Header : Header {
Fin : fin ,
OpCode : op ,
Length : int64 (len (p )),
},
Payload : p ,
}
}
func NewTextFrame (p []byte ) Frame {
return NewFrame (OpText , true , p )
}
func NewBinaryFrame (p []byte ) Frame {
return NewFrame (OpBinary , true , p )
}
func NewPingFrame (p []byte ) Frame {
return NewFrame (OpPing , true , p )
}
func NewPongFrame (p []byte ) Frame {
return NewFrame (OpPong , true , p )
}
func NewCloseFrame (p []byte ) Frame {
return NewFrame (OpClose , true , p )
}
func NewCloseFrameBody (code StatusCode , reason string ) []byte {
n := min (2 +len (reason ), MaxControlFramePayloadSize )
p := make ([]byte , n )
crop := min (MaxControlFramePayloadSize -2 , len (reason ))
PutCloseFrameBody (p , code , reason [:crop ])
return p
}
func PutCloseFrameBody (p []byte , code StatusCode , reason string ) {
_ = p [1 +len (reason )]
binary .BigEndian .PutUint16 (p , uint16 (code ))
copy (p [2 :], reason )
}
func MaskFrame (f Frame ) Frame {
return MaskFrameWith (f , NewMask ())
}
func MaskFrameWith (f Frame , mask [4 ]byte ) Frame {
p := make ([]byte , len (f .Payload ))
copy (p , f .Payload )
f .Payload = p
return MaskFrameInPlaceWith (f , mask )
}
func MaskFrameInPlace (f Frame ) Frame {
return MaskFrameInPlaceWith (f , NewMask ())
}
var zeroMask [4 ]byte
func UnmaskFrame (f Frame ) Frame {
p := make ([]byte , len (f .Payload ))
copy (p , f .Payload )
f .Payload = p
return UnmaskFrameInPlace (f )
}
func UnmaskFrameInPlace (f Frame ) Frame {
Cipher (f .Payload , f .Header .Mask , 0 )
f .Header .Masked = false
f .Header .Mask = zeroMask
return f
}
func MaskFrameInPlaceWith (f Frame , m [4 ]byte ) Frame {
f .Header .Masked = true
f .Header .Mask = m
Cipher (f .Payload , m , 0 )
return f
}
func NewMask () (ret [4 ]byte ) {
binary .BigEndian .PutUint32 (ret [:], rand .Uint32 ())
return ret
}
func CompileFrame (f Frame ) (bts []byte , err error ) {
buf := bytes .NewBuffer (make ([]byte , 0 , 16 ))
err = WriteFrame (buf , f )
bts = buf .Bytes ()
return bts , err
}
func MustCompileFrame (f Frame ) []byte {
bts , err := CompileFrame (f )
if err != nil {
panic (err )
}
return bts
}
func makeCloseFrame(code StatusCode ) Frame {
return NewCloseFrame (NewCloseFrameBody (code , "" ))
}
var (
closeFrameNormalClosure = makeCloseFrame (StatusNormalClosure )
closeFrameGoingAway = makeCloseFrame (StatusGoingAway )
closeFrameProtocolError = makeCloseFrame (StatusProtocolError )
closeFrameUnsupportedData = makeCloseFrame (StatusUnsupportedData )
closeFrameNoMeaningYet = makeCloseFrame (StatusNoMeaningYet )
closeFrameInvalidFramePayloadData = makeCloseFrame (StatusInvalidFramePayloadData )
closeFramePolicyViolation = makeCloseFrame (StatusPolicyViolation )
closeFrameMessageTooBig = makeCloseFrame (StatusMessageTooBig )
closeFrameMandatoryExt = makeCloseFrame (StatusMandatoryExt )
closeFrameInternalServerError = makeCloseFrame (StatusInternalServerError )
closeFrameTLSHandshake = makeCloseFrame (StatusTLSHandshake )
)
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .