sigildocs

(sigil websocket server)

(sigil websocket server) - Server-side WebSocket handshake helpers.

Pairs with (sigil websocket frame) for full server support. The handshake flow on the listener:

  1. Read bytes from the client until you have the full HTTP request header block (terminated by CRLF CRLF).
  2. Pass the header block to parse-handshake-request. It returns either a ws-handshake-request record or a ws-handshake-error record describing what was wrong (which the listener turns into a 400/426 response).
  3. Pass the request to build-handshake-response to obtain the 101 Switching Protocols response. Optionally pass a chosen subprotocol (one of the values from ws-handshake-request-subprotocols).
  4. Write the response to the socket. Subsequent reads are raw WebSocket frames; pass them to decode-frame and reply with the unmasked encode-server-*-frame encoders.

This module deliberately does not own a connection record or a read loop — every server has its own per-connection abstractions. What this module owns is the wire-format-correct handshake.

Exports

Compute the Sec-WebSocket-Accept value from the client's Sec-WebSocket-Key per RFC 6455 §4.2.2: base64(SHA-1(key ++ guid))

sigil-crypto's sha1 already returns the raw 20-byte digest as a bytevector (despite the older docstring suggesting hex — the runtime returns a bytevector for SHA family hashes that base64-encode accepts directly).

Compute the Sec-WebSocket-Accept value from the client's Sec-WebSocket-Key per RFC 6455 §4.2.2: base64(SHA-1(key ++ guid))

sigil-crypto's sha1 already returns the raw 20-byte digest as a bytevector (despite the older docstring suggesting hex — the runtime returns a bytevector for SHA family hashes that base64-encode accepts directly).

Construct a ws-handshake-request struct.

Test if a value is a ws-handshake-request struct.

Get the method field of a ws-handshake-request struct.

Get the path field of a ws-handshake-request struct.

Get the host field of a ws-handshake-request struct.

Get the key field of a ws-handshake-request struct.

Get the version field of a ws-handshake-request struct.

Get the subprotocols field of a ws-handshake-request struct.

Get the headers field of a ws-handshake-request struct.

Get the x-forwarded-for field of a ws-handshake-request struct.

Get the x-forwarded-for field of a ws-handshake-request struct.

Construct a ws-handshake-error struct.

Test if a value is a ws-handshake-error struct.

Get the status field of a ws-handshake-error struct.

Get the reason field of a ws-handshake-error struct.

Has the client sent a complete header block yet? Looks for CRLF CRLF or LF LF in the buffer.

Parse a complete handshake request. Returns either a ws-handshake-request or a ws-handshake-error.

Caller is responsible for first checking that the header block is complete with handshake-request-bytes-needed?.

Build the 101 Switching Protocols response for a successfully- validated handshake. subprotocol, when supplied, is echoed back in the Sec-WebSocket-Protocol header.

Build a short HTTP error response. Used when the request isn't a valid WebSocket upgrade — listeners should send the corresponding status (e.g. 400, 426) and close.

ws-guidvariable

(No description)