xwsutil

package
v1.7.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ControlFrameHandler added in v1.6.4

func ControlFrameHandler(w io.Writer, wlock sync.Locker, state ws.State) wsutil.FrameHandlerFunc

ControlFrameHandler returns FrameHandlerFunc for handling control frames. For more info see ControlHandler docs.

func FastCipher added in v1.7.3

func FastCipher(b []byte, key [4]byte, pos int) int

func NextReader

func NextReader(r io.Reader, s ws.State) (ws.Header, io.Reader, error)

NextReader prepares next message read from r. It returns header that describes the message and io.Reader to read message's payload. It returns non-nil error when it is not possible to read message's initial frame.

Note that next NextReader() on the same r should be done after reading all bytes from previously returned io.Reader. For more performant way to discard message use Reader and its Discard() method.

Note that it will not handle any "intermediate" frames, that possibly could be received between text/binary continuation frames. That is, if peer sent text/binary frame with fin flag "false", then it could send ping frame, and eventually remaining part of text/binary frame with fin "true" – with NextReader() the ping frame will be dropped without any notice. To handle this rare, but possible situation (and if you do not know exactly which frames peer could send), you could use Reader with OnIntermediate field set.

func ReadHeader added in v1.7.3

func ReadHeader(r io.Reader, bts []byte) (h ws.Header, err error)

ReadHeader reads a frame header from r.

Types

type CipherReader added in v1.7.3

type CipherReader struct {
	// contains filtered or unexported fields
}

CipherReader implements io.Reader that applies xor-cipher to the bytes read from source. It could help to unmask WebSocket frame payload on the fly.

func NewCipherReader added in v1.7.3

func NewCipherReader(r io.Reader, mask [4]byte) *CipherReader

NewCipherReader creates xor-cipher reader from r with given mask.

func (*CipherReader) Read added in v1.7.3

func (c *CipherReader) Read(p []byte) (n int, err error)

Read implements io.Reader interface. It applies mask given during initialization to every read byte.

func (*CipherReader) Reset added in v1.7.3

func (c *CipherReader) Reset(r io.Reader, mask [4]byte)

Reset resets CipherReader to read from r with given mask.

type CipherWriter added in v1.7.3

type CipherWriter struct {
	// contains filtered or unexported fields
}

CipherWriter implements io.Writer that applies xor-cipher to the bytes written to the destination writer. It does not modify the original bytes.

func NewCipherWriter added in v1.7.3

func NewCipherWriter(w io.Writer, mask [4]byte) *CipherWriter

NewCipherWriter creates xor-cipher writer to w with given mask.

func (*CipherWriter) Reset added in v1.7.3

func (c *CipherWriter) Reset(w io.Writer, mask [4]byte)

Reset reset CipherWriter to write to w with given mask.

func (*CipherWriter) Write added in v1.7.3

func (c *CipherWriter) Write(p []byte) (n int, err error)

Write implements io.Writer interface. It applies masking during initialization to every sent byte. It does not modify original slice.

type ControlHandler added in v1.6.4

type ControlHandler struct {
	Src          io.Reader
	Dst          io.Writer
	State        ws.State
	WriterLocker sync.Locker

	// DisableSrcCiphering disables unmasking payload data read from Src.
	// It is useful when wsutil.Reader is used or when frame payload already
	// pulled and ciphered out from the connection (and introduced by
	// bytes.Reader, for example).
	DisableSrcCiphering bool
}

ControlHandler contains logic of handling control frames.

The intentional way to use it is to read the next frame header from the connection, optionally check its validity via ws.CheckHeader() and if it is not a ws.OpText of ws.OpBinary (or ws.OpContinuation) – pass it to Handle() method.

That is, passed header should be checked to get rid of unexpected errors.

The Handle() method will read out all control frame payload (if any) and write necessary bytes as a rfc compatible response.

func (ControlHandler) Handle added in v1.6.4

func (c ControlHandler) Handle(h ws.Header) error

Handle handles control frames regarding to the c.State and writes responses to the c.Dst when needed.

It returns ErrNotControlFrame when given header is not of ws.OpClose, ws.OpPing or ws.OpPong operation code.

func (ControlHandler) HandleClose added in v1.6.4

func (c ControlHandler) HandleClose(h ws.Header) error

HandleClose handles close frame, makes protocol validity checks and writes specification compatible response to the c.Dst.

func (ControlHandler) HandlePing added in v1.6.4

func (c ControlHandler) HandlePing(h ws.Header) error

HandlePing handles ping frame and writes specification compatible response to the c.Dst.

func (ControlHandler) HandlePong added in v1.6.4

func (c ControlHandler) HandlePong(h ws.Header) error

HandlePong handles pong frame by discarding it.

type Reader

type Reader struct {
	Source io.Reader
	State  ws.State

	// SkipHeaderCheck disables checking header bits to be RFC6455 compliant.
	SkipHeaderCheck bool

	// CheckUTF8 enables UTF-8 checks for text frames payload. If incoming
	// bytes are not valid UTF-8 sequence, ErrInvalidUTF8 returned.
	CheckUTF8 bool

	// Extensions is a list of negotiated extensions for reader Source.
	// It is used to meet the specs and clear appropriate bits in fragment
	// header RSV segment.
	Extensions []wsutil.RecvExtension

	// MaxFrameSize controls the maximum frame size in bytes
	// that can be read. A message exceeding that size will return
	// a ErrFrameTooLarge to the application.
	//
	// Not setting this field means there is no limit.
	MaxFrameSize int64

	OnContinuation wsutil.FrameHandlerFunc
	OnIntermediate wsutil.FrameHandlerFunc

	GetFlateReader func(reader io.Reader) *xwsflate.Reader
	PutFlateReader func(reader *xwsflate.Reader)
	// contains filtered or unexported fields
}

Reader is a wrapper around source io.Reader which represents WebSocket connection. It contains options for reading messages from source.

Reader implements io.Reader, which Read() method reads payload of incoming WebSocket frames. It also takes care on fragmented frames and possibly intermediate control frames between them.

Note that Reader's methods are not goroutine safe.

func NewClientSideReader

func NewClientSideReader(r io.Reader) *Reader

NewClientSideReader is a helper function that calls NewReader with r and ws.StateClientSide.

func NewReader

func NewReader(r io.Reader, s ws.State) *Reader

NewReader creates new frame reader that reads from r keeping given state to make some protocol validity checks when it needed.

func NewServerSideReader

func NewServerSideReader(r io.Reader) *Reader

NewServerSideReader is a helper function that calls NewReader with r and ws.StateServerSide.

func (*Reader) Discard

func (r *Reader) Discard() (err error)

Discard discards current message unread bytes. It discards all frames of fragmented message.

func (*Reader) NextFrame

func (r *Reader) NextFrame() (hdr ws.Header, err error)

NextFrame prepares r to read next message. It returns received frame header and non-nil error on failure.

Note that next NextFrame() call must be done after receiving or discarding all current message bytes.

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read implements io.Reader. It reads the next message payload into p. It takes care on fragmented messages.

The error is io.EOF only if all of message bytes were read. If an io.EOF happens during reading some but not all the message bytes Read() returns io.ErrUnexpectedEOF.

The error is ErrNoFrameAdvance if no NextFrame() call was made before reading next message bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL