Handshake
in package
The opening handshake: reads the HTTP request of a new connection and turns a correct one into the 101 response.
Two things here are security, not courtesy:
- Origin is checked before the socket is accepted as a client. A browser sends the cookies of the site on a WebSocket handshake, so without this check any page can open a socket in the name of the visitor (cross-site WebSocket hijacking). The whitelist is passed in $settings, never read from a global, so the caller decides what its instance accepts.
- Every field is validated before anything is registered: method, HTTP version, Upgrade, Connection, a 16-byte Sec-WebSocket-Key, version 13. A request that fails is answered with an HTTP refusal and closed; it never becomes a connection.
The historical draft-76 headers (WebSocket-Origin, WebSocket-Location) are not sent: they belong to a protocol no browser has spoken for fifteen years.
Tags
Table of Contents
Constants
- GUID : mixed = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
- Fixed key of the RFC 6455 §4.2.2 accept computation.
- MAX_REQUEST : mixed = 16384
- Largest handshake accepted, in bytes.
Methods
- accept_key() : string
- Computes the Sec-WebSocket-Accept value for a client key (RFC 6455 §4.2.2).
- origin_allowed() : bool
- Tells whether an Origin is in the whitelist.
- parse() : array<string|int, mixed>
- Splits a raw HTTP request into its request line and its headers.
- refusal() : string
- Builds a plain HTTP refusal, sent before closing the socket.
- response() : string
- Builds the 101 response for an accepted key.
- verify() : array<string|int, mixed>
- Verifies a handshake and returns the response to send.
Constants
GUID
Fixed key of the RFC 6455 §4.2.2 accept computation.
public
mixed
GUID
= '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
MAX_REQUEST
Largest handshake accepted, in bytes.
public
mixed
MAX_REQUEST
= 16384
Methods
accept_key()
Computes the Sec-WebSocket-Accept value for a client key (RFC 6455 §4.2.2).
public
static accept_key(string $key) : string
Parameters
- $key : string
-
The Sec-WebSocket-Key received, undecoded.
Return values
string —Base64 accept value.
origin_allowed()
Tells whether an Origin is in the whitelist.
public
static origin_allowed(string $origin, array<int, string> $allowed) : bool
Comparison is on the whole origin (scheme, host, port): a scheme or a
port change is another origin, and https://site.test must not accept
http://site.test. Trailing slashes are ignored, case is folded because
schemes and hostnames are case-insensitive.
Parameters
- $origin : string
-
Origin header value.
- $allowed : array<int, string>
-
Allowed origins.
Return values
boolparse()
Splits a raw HTTP request into its request line and its headers.
public
static parse(string $request) : array<string|int, mixed>
Header names are lowercased and a repeated name is joined with ", " so
that Connection: keep-alive + Connection: Upgrade behaves like the
single comma-separated line browsers send.
Parameters
- $request : string
-
Raw request, headers only (the body is never read).
Tags
Return values
array<string|int, mixed> —['method'=>string, 'path'=>string, 'version'=>string, 'headers'=>array<string,string>]
refusal()
Builds a plain HTTP refusal, sent before closing the socket.
public
static refusal(int $status[, string $message = '' ]) : string
Parameters
- $status : int
-
HTTP status, 400, 403, 426 or 503.
- $message : string = ''
-
Short reason, in the body and the log.
Return values
string —Raw response, headers and body.
response()
Builds the 101 response for an accepted key.
public
static response(string $accept) : string
Deliberately minimal: no protocol extension is negotiated, so there is no Sec-WebSocket-Extensions and no compression (permessage-deflate would add a second codec nobody asked for).
Parameters
- $accept : string
-
Value returned by accept_key().
Return values
string —Raw response, headers only.
verify()
Verifies a handshake and returns the response to send.
public
static verify(string $request, array<string|int, mixed> $settings) : array<string|int, mixed>
Parameters
- $request : string
-
Raw request headers.
- $settings : array<string|int, mixed>
-
['origins'=>array<int,string>, 'allow_no_origin'=>bool]
Tags
Return values
array<string|int, mixed> —['path'=>string, 'headers'=>array, 'key'=>string, 'origin'=>string, 'response'=>string]