handshake

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 6 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 handshake implements the WebSocket opening handshake rules without performing I/O: key and accept computation, request and response validation, permessage-deflate negotiation, and subprotocol selection. Everything takes and returns header values, so it serves net/http servers, clients, and event-driven transports alike.

Index

Constants

View Source
const Version = "13"

Version is the only WebSocket protocol version.

Variables

View Source
var (
	ErrNotWebSocket   = errors.New("ews/handshake: not a WebSocket upgrade")
	ErrBadVersion     = errors.New("ews/handshake: unsupported WebSocket version")
	ErrBadKey         = errors.New("ews/handshake: invalid Sec-WebSocket-Key")
	ErrBadExtension   = errors.New("ews/handshake: invalid Sec-WebSocket-Extensions")
	ErrBadAccept      = errors.New("ews/handshake: Sec-WebSocket-Accept mismatch")
	ErrBadStatus      = errors.New("ews/handshake: unexpected response status")
	ErrBadProtocol    = errors.New("ews/handshake: unexpected subprotocol")
	ErrInvalidOptions = errors.New("ews/handshake: invalid options")
)

Functions

func Accept

func Accept(key string) string

Accept computes Sec-WebSocket-Accept for a Sec-WebSocket-Key.

func Negotiate

func Negotiate(req Request, opts Options) (Response, Result, error)

Negotiate validates a client's request and builds the server's response. Errors map to HTTP statuses through StatusCode. An offer this server cannot honor is skipped rather than rejected; the connection then proceeds without that extension or subprotocol.

func NewKey

func NewKey() string

NewKey returns a fresh Sec-WebSocket-Key.

func StatusCode

func StatusCode(err error) int

StatusCode returns the HTTP status a server should answer a rejected request with: 426 for a version mismatch, otherwise 400.

Types

type Compress

type Compress struct {
	// Level is the flate level for outgoing messages.
	Level int
	// MinSize leaves smaller payloads uncompressed. Zero means 128 bytes,
	// below which flate encoders emit literals only, so compression could
	// not help; 1 compresses everything.
	MinSize int
	// ContextTakeover allows compression history to carry between messages
	// when the peer agrees. Disabled, both directions run without takeover,
	// which needs no per-connection compressor state.
	ContextTakeover bool
}

Compress configures permessage-deflate. A peer may ask this endpoint to compress with a window smaller than 32 KB; that is honored and reported in Compression.SendWindowBits.

type Compression

type Compression struct {
	Level                  int
	MinSize                int
	SendContextTakeover    bool
	ReceiveContextTakeover bool
	// SendWindowBits is the LZ77 window this endpoint compresses with, 8 to
	// 15, and ReceiveWindowBits the window the peer declared for its own
	// messages, which bounds how much history must be kept for decoding.
	// Zero means the full 15.
	SendWindowBits    int
	ReceiveWindowBits int
}

Compression is the negotiated permessage-deflate configuration for one connection. Directions are named from the local endpoint's point of view.

type Options

type Options struct {
	// Compression offers or accepts permessage-deflate. Nil disables it.
	Compression *Compress
	// Protocols lists supported subprotocols in preference order.
	Protocols []string
}

Options configures what an endpoint offers or accepts.

type Request

type Request struct {
	Method     string
	Upgrade    string
	Connection string
	Version    string
	Key        string
	Extensions string
	Protocols  string
}

Request holds the handshake fields of an HTTP request. Header fields with several values are joined with commas.

func NewRequest

func NewRequest(opts Options) (Request, error)

NewRequest builds a client's handshake request with a fresh key.

type Response

type Response struct {
	Status     int
	Upgrade    string
	Connection string
	Accept     string
	Extensions string
	Protocol   string
}

Response holds the handshake fields of an HTTP response.

type Result

type Result struct {
	// Compression is nil when permessage-deflate was not negotiated.
	Compression *Compression
	// Protocol is the selected subprotocol, or empty.
	Protocol string
}

Result is what a completed handshake agreed on.

func Confirm

func Confirm(req Request, resp Response, opts Options) (Result, error)

Confirm validates the server's response to req and reports what was agreed.

Jump to

Keyboard shortcuts

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