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 ¶
const Version = "13"
Version is the only WebSocket protocol version.
Variables ¶
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 Negotiate ¶
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 StatusCode ¶
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 ¶
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.