websocket

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package websocket reads the frames of a WebSocket connection (RFC 6455 §5), so that the engine's reverse proxy can inspect a client's messages before it forwards them (#276).

A Reader reads what a client sends: every frame masked, no extension (the proxy removes Sec-WebSocket-Extensions from the handshake, so permessage-deflate is never negotiated and a frame with an RSV bit is an error), control frames whole and at most 125 bytes, interleaved with the fragments of a message. It returns each frame as it came, raw, so that it can be forwarded unchanged, with the payload unmasked. A Tracker follows what a server sends, to find the boundaries between its frames.

Index

Constants

View Source
const (
	OpContinuation = 0x0
	OpText         = 0x1
	OpBinary       = 0x2
	OpClose        = 0x8
	OpPing         = 0x9
	OpPong         = 0xa
)

Opcodes (RFC 6455 §5.2).

View Source
const (
	CloseGoingAway       = 1001
	CloseProtocolError   = 1002
	CloseUnsupportedData = 1003
	CloseInvalidPayload  = 1007
	ClosePolicyViolation = 1008
	CloseMessageTooBig   = 1009
)

Close codes (RFC 6455 §7.4.1).

Variables

This section is empty.

Functions

func CloseFrame

func CloseFrame(code int, reason string, key *[4]byte) []byte

CloseFrame returns a close frame with code and reason (at most 123 bytes), masked with key when key isn't nil (a client's frame), unmasked otherwise (a server's).

func Mask

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

Mask XORs b with key, starting at offset pos in the payload: masking and unmasking are the same operation.

Types

type Error

type Error struct {
	Code   int
	Reason string
}

Error is a frame that breaks the protocol, and the close code that answers it.

func (*Error) Error

func (e *Error) Error() string

type Frame

type Frame struct {
	Fin    bool
	Opcode byte
	// Raw is the frame as it came: header, masking key and masked payload.
	Raw []byte
	// Payload is the unmasked payload, or as much of it as fits the
	// Reader's limit for its message (Truncated).
	Payload []byte
	// Truncated: the message is over the limit, and Raw and Payload end
	// where it was reached. The rest of the frame is still to be read, raw,
	// with ReadRest.
	Truncated bool
}

Frame is one frame a client sent.

func (*Frame) Control

func (f *Frame) Control() bool

Control reports whether f is a control frame (close, ping, pong).

type Reader

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

Reader reads a client's frames.

func NewReader

func NewReader(r *bufio.Reader, limit int64) *Reader

NewReader returns a Reader of the frames on r, keeping at most limit payload bytes of each data message (the frames past it are returned raw, with Truncated set).

func (*Reader) Next

func (r *Reader) Next() (*Frame, error)

Next reads the next frame. It returns an *Error for a frame that breaks the protocol, io.EOF when the connection ends between frames, and io.ErrUnexpectedEOF when it ends inside one.

func (*Reader) Pending

func (r *Reader) Pending() uint64

Pending returns how many raw bytes of a truncated frame are left to read.

func (*Reader) ReadRest

func (r *Reader) ReadRest(b []byte) (int, error)

ReadRest reads raw bytes of the rest of a truncated frame into b; io.EOF once there are none.

type Tracker

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

Tracker follows a server's frames as their bytes pass, to tell when the stream is between two frames, where a frame of the proxy's own can go.

func (*Tracker) AtBoundary

func (t *Tracker) AtBoundary() bool

AtBoundary reports whether the bytes seen so far end with a whole frame.

func (*Tracker) Split

func (t *Tracker) Split(b []byte) int

Split returns how many bytes of b complete the current frame (all of b when it doesn't), and records them as seen.

Jump to

Keyboard shortcuts

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