wsproto

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 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 wsproto implements the RFC 6455 WebSocket wire format shared by the netplay transports and pkg/online/wsclient: frame reading and writing with slow-loris-safe allocation, the opening handshake's accept key, and the native client upgrade. It is internal to pkg/online; games use netplay or wsclient.

Index

Constants

View Source
const (
	OpContinuation = 0x0
	OpText         = 0x1
	OpBinary       = 0x2
	OpClose        = 0x8
	OpPing         = 0x9
	OpPong         = 0xA
	// MaxControlPayload bounds a control frame's payload (RFC 6455 §5.5).
	MaxControlPayload = 125
	// MaxMessageSize is the largest frame payload ReadFrame reads and WriteFrame writes.
	MaxMessageSize = 4 << 20 // 4 MiB safety cap
	// EagerAlloc is the largest payload ReadFrameLimit allocates before its bytes arrive. A larger payload grows with the bytes actually received, so a peer that announces a 4 MiB frame and then trickles it (a slow-loris attack) holds memory only for what it has sent (#546).
	EagerAlloc = 64 << 10
)

Variables

View Source
var (
	// ErrPayloadTooLarge is returned when a frame's payload exceeds the reader's limit or MaxMessageSize on write.
	ErrPayloadTooLarge = errors.New("net: payload too large")
	// ErrInvalidPacket is returned when a frame breaks RFC 6455: a fragmented or oversized control frame, or a reserved control opcode.
	ErrInvalidPacket = errors.New("net: invalid packet")
)

Functions

func AcceptKey

func AcceptKey(key string) string

AcceptKey derives the Sec-WebSocket-Accept response value for a client's Sec-WebSocket-Key (RFC 6455 §4.2.2).

func ClientUpgrade

func ClientUpgrade(w io.Writer, br *bufio.Reader, u *url.URL, header http.Header, protocols []string) (string, error)

ClientUpgrade writes the RFC 6455 opening handshake for u to w and validates the server's response read from br, returning the subprotocol the server selected, or "" for none. header carries extra request headers, such as Authorization; entries naming a header the handshake writes itself are skipped. A server that answers a subprotocol the client did not offer is a protocol error.

func HostPort

func HostPort(u *url.URL) (string, error)

HostPort joins the URL's host and port, defaulting by scheme and keeping IPv6 literals bracketed; checking for a colon cannot tell a port from an IPv6 address (#546).

func ReadFrame

func ReadFrame(r *bufio.Reader) (fin bool, opcode byte, payload []byte, masked bool, err error)

ReadFrame reads one frame of at most MaxMessageSize payload bytes.

func ReadFrameLimit

func ReadFrameLimit(r *bufio.Reader, limit uint64) (fin bool, opcode byte, payload []byte, masked bool, err error)

ReadFrameLimit reads one frame whose payload is at most limit bytes.

func WriteFrame

func WriteFrame(w io.Writer, opcode byte, masked bool, payload []byte) error

WriteFrame writes one unfragmented frame of at most MaxMessageSize payload bytes.

func WriteFrameLimit

func WriteFrameLimit(w io.Writer, opcode byte, masked bool, payload []byte, limit uint64) error

WriteFrameLimit writes one unfragmented frame whose payload is at most limit bytes. Clients mask, servers do not (RFC 6455 §5.3).

Types

This section is empty.

Jump to

Keyboard shortcuts

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