(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:
- Read bytes from the client until you have the full HTTP request header block (terminated by CRLF CRLF).
- Pass the header block to
parse-handshake-request. It returns either aws-handshake-requestrecord or aws-handshake-errorrecord describing what was wrong (which the listener turns into a 400/426 response). - Pass the request to
build-handshake-responseto obtain the 101 Switching Protocols response. Optionally pass a chosen subprotocol (one of the values fromws-handshake-request-subprotocols). - Write the response to the socket. Subsequent reads are raw WebSocket frames; pass them to
decode-frameand reply with the unmaskedencode-server-*-frameencoders.
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-accept-keyprocedureCompute 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).
%ws-handshake-request--typevariableCompute 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).
ws-handshake-requestprocedureConstruct a ws-handshake-request struct.
ws-handshake-request?procedureTest if a value is a ws-handshake-request struct.
ws-handshake-request-methodprocedureGet the method field of a ws-handshake-request struct.
ws-handshake-request-pathprocedureGet the path field of a ws-handshake-request struct.
ws-handshake-request-hostprocedureGet the host field of a ws-handshake-request struct.
ws-handshake-request-keyprocedureGet the key field of a ws-handshake-request struct.
ws-handshake-request-versionprocedureGet the version field of a ws-handshake-request struct.
ws-handshake-request-subprotocolsprocedureGet the subprotocols field of a ws-handshake-request struct.
ws-handshake-request-headersprocedureGet the headers field of a ws-handshake-request struct.
ws-handshake-request-x-forwarded-forprocedureGet the x-forwarded-for field of a ws-handshake-request struct.
%ws-handshake-error--typevariableGet the x-forwarded-for field of a ws-handshake-request struct.
ws-handshake-errorprocedureConstruct a ws-handshake-error struct.
ws-handshake-error?procedureTest if a value is a ws-handshake-error struct.
ws-handshake-error-statusprocedureGet the status field of a ws-handshake-error struct.
ws-handshake-error-reasonprocedureGet the reason field of a ws-handshake-error struct.
handshake-request-bytes-needed?procedureHas the client sent a complete header block yet? Looks for CRLF CRLF or LF LF in the buffer.
parse-handshake-requestprocedureParse 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-handshake-responsevariableBuild the 101 Switching Protocols response for a successfully- validated handshake. subprotocol, when supplied, is echoed back in the Sec-WebSocket-Protocol header.
build-handshake-error-responseprocedureBuild 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)