Module

whiplash/websocket

src/whiplash/websocket.pooka

WebSocket, the conversation that stays open (RFC 6455).

Sans-IO like its siblings: a Reader turns bytes into messages, reassembling fragments and answering nothing on its own; the frame writers turn messages into bytes. The handshake helpers check an upgrade request and compute Sec-WebSocket-Accept, over HTTP/1.1 (RFC 6455 section 4) or an HTTP/2 extended CONNECT (RFC 8441).

permessage-deflate (RFC 7692) is negotiated by negotiate_deflate and applied by the reader and writer when both ends agreed to it.

API reference

enum

Opcode

src/whiplash/websocket.pooka:39
enum Opcode

The opcodes of RFC 6455 section 5.2.

variant

Continuation

src/whiplash/websocket.pooka:40
Opcode.Continuation
variant

Text

src/whiplash/websocket.pooka:41
Opcode.Text
variant

Binary

src/whiplash/websocket.pooka:42
Opcode.Binary
variant

Close

src/whiplash/websocket.pooka:43
Opcode.Close
variant

Ping

src/whiplash/websocket.pooka:44
Opcode.Ping
variant

Pong

src/whiplash/websocket.pooka:45
Opcode.Pong
class

Frame

src/whiplash/websocket.pooka:49
class Frame(fin: Bool, rsv1: Bool, opcode: Opcode, payload: BitArray)

One frame as it is on the wire, after unmasking.

field

fin

src/whiplash/websocket.pooka:49
fin: Bool
INFERREDBool
field

rsv1

src/whiplash/websocket.pooka:49
rsv1: Bool
INFERREDBool
field

opcode

src/whiplash/websocket.pooka:49
opcode: Opcode
field

payload

src/whiplash/websocket.pooka:49
payload: BitArray
INFERREDBitArray
constructor

new

src/whiplash/websocket.pooka:49
INFERREDfn(Bool, Bool, whiplash/websocket.Opcode, BitArray) -> whiplash/websocket.Frame
class

Message

src/whiplash/websocket.pooka:52
class Message

A complete message, once its fragments have arrived.

class

TextMessage

src/whiplash/websocket.pooka:54
class TextMessage(text: String) is Message
field

text

src/whiplash/websocket.pooka:54
text: String
INFERREDString
constructor

new

src/whiplash/websocket.pooka:54
INFERREDfn(String) -> whiplash/websocket.TextMessage
class

BinaryMessage

src/whiplash/websocket.pooka:56
class BinaryMessage(data: BitArray) is Message
field

data

src/whiplash/websocket.pooka:56
data: BitArray
INFERREDBitArray
constructor

new

src/whiplash/websocket.pooka:56
INFERREDfn(BitArray) -> whiplash/websocket.BinaryMessage
class

PingMessage

src/whiplash/websocket.pooka:58
class PingMessage(data: BitArray) is Message
field

data

src/whiplash/websocket.pooka:58
data: BitArray
INFERREDBitArray
constructor

new

src/whiplash/websocket.pooka:58
INFERREDfn(BitArray) -> whiplash/websocket.PingMessage
class

PongMessage

src/whiplash/websocket.pooka:60
class PongMessage(data: BitArray) is Message
field

data

src/whiplash/websocket.pooka:60
data: BitArray
INFERREDBitArray
constructor

new

src/whiplash/websocket.pooka:60
INFERREDfn(BitArray) -> whiplash/websocket.PongMessage
class

CloseMessage

src/whiplash/websocket.pooka:63
class CloseMessage(code: Option(Int), reason: String) is Message

The peer is closing: a code, when it sent one, and a reason.

field

code

src/whiplash/websocket.pooka:63
code: Option(Int)
INFERREDOption(Int)
field

reason

src/whiplash/websocket.pooka:63
reason: String
INFERREDString
constructor

new

src/whiplash/websocket.pooka:63
INFERREDfn(Option(Int), String) -> whiplash/websocket.CloseMessage
class

WebsocketError

src/whiplash/websocket.pooka:68
class WebsocketError(code: Int, message: String)

Why the bytes were refused, with the close code to answer with: 1002 for a protocol error, 1007 for text that is not UTF-8, 1009 for a message too big.

field

code

src/whiplash/websocket.pooka:68
code: Int
INFERREDInt
field

message

src/whiplash/websocket.pooka:68
message: String
INFERREDString
constructor

new

src/whiplash/websocket.pooka:68
INFERREDfn(Int, String) -> whiplash/websocket.WebsocketError
enum

Role

src/whiplash/websocket.pooka:72
enum Role

Which end of the connection this is. A server requires masked frames from its clients; a client requires unmasked ones.

variant

Server

src/whiplash/websocket.pooka:73
Role.Server
variant

Client

src/whiplash/websocket.pooka:74
Role.Client
class

Deflate

src/whiplash/websocket.pooka:78
class Deflate(
  server_no_context_takeover: Bool,
  client_no_context_takeover: Bool,
  server_max_window_bits: Int,
  client_max_window_bits: Int
)

permessage-deflate's parameters, as both ends agreed them.

field

server_no_context_takeover

src/whiplash/websocket.pooka:79
server_no_context_takeover: Bool
INFERREDBool
field

client_no_context_takeover

src/whiplash/websocket.pooka:80
client_no_context_takeover: Bool
INFERREDBool
field

server_max_window_bits

src/whiplash/websocket.pooka:81
server_max_window_bits: Int
INFERREDInt
field

client_max_window_bits

src/whiplash/websocket.pooka:82
client_max_window_bits: Int
INFERREDInt
constructor

new

src/whiplash/websocket.pooka:79
INFERREDfn(Bool, Bool, Int, Int) -> whiplash/websocket.Deflate
class

Reader

src/whiplash/websocket.pooka:86
class Reader(
  role: Role,
  max_message: Int,
  deflate: Option(Deflate),
  buffer: BitArray,
  fragments: List(Frame),
  kind: Option(Opcode),
  size: Int,
  utf8_tail: BitArray,
  closed: Bool,
  compressed: Bool,
  dictionary: BitArray
)

A reader of one connection's frames, with a message half assembled.

field

role

src/whiplash/websocket.pooka:87
role: Role
field

max_message

src/whiplash/websocket.pooka:88
max_message: Int
INFERREDInt
field

deflate

src/whiplash/websocket.pooka:89
deflate: Option(Deflate)
field

buffer

src/whiplash/websocket.pooka:90
buffer: BitArray
INFERREDBitArray
field

fragments

src/whiplash/websocket.pooka:91
fragments: List(Frame)
field

kind

src/whiplash/websocket.pooka:92
kind: Option(Opcode)
field

size

src/whiplash/websocket.pooka:93
size: Int
INFERREDInt
field

utf8_tail

src/whiplash/websocket.pooka:94
utf8_tail: BitArray
INFERREDBitArray
field

closed

src/whiplash/websocket.pooka:95
closed: Bool
INFERREDBool
field

compressed

src/whiplash/websocket.pooka:96
compressed: Bool
INFERREDBool
field

dictionary

src/whiplash/websocket.pooka:97
dictionary: BitArray
INFERREDBitArray
constructor

new

src/whiplash/websocket.pooka:87
INFERREDfn(whiplash/websocket.Role, Int, Option(whiplash/websocket.Deflate), BitArray, List(whiplash/websocket.Frame), Option(whiplash/websocket.Opcode), Int, BitArray, Bool, Bool, BitArray) -> whiplash/websocket.Reader
class

Upgrade

src/whiplash/websocket.pooka:101
class Upgrade(key: String, protocols: List(String), extensions: List(String))

What an upgrade request asked for, once checked.

field

key

src/whiplash/websocket.pooka:101
key: String
INFERREDString
field

protocols

src/whiplash/websocket.pooka:101
protocols: List(String)
INFERREDList(String)
field

extensions

src/whiplash/websocket.pooka:101
extensions: List(String)
INFERREDList(String)
constructor

new

src/whiplash/websocket.pooka:101
INFERREDfn(String, List(String), List(String)) -> whiplash/websocket.Upgrade
function

reader

src/whiplash/websocket.pooka:105
reader(role: Role, max_message: Int) -> Reader

A reader for one end of a connection, refusing messages over max_message bytes.

function

with_deflate

src/whiplash/websocket.pooka:110
with_deflate(reader: Reader, deflate: Deflate) -> Reader

The same reader, inflating compressed messages.

function

feed

src/whiplash/websocket.pooka:127
feed(reader: Reader, bytes: BitArray) -> Result(
  #(List(Message), Reader),
  WebsocketError
)

Feed bytes, and get every message they complete.

function

decode_frame

src/whiplash/websocket.pooka:139
decode_frame(role: Role, bytes: BitArray) -> Result(
  Option(#(Frame, BitArray)),
  WebsocketError
)
INFERREDfn(whiplash/websocket.Role, BitArray) -> Result(Option(#(whiplash/websocket.Frame, BitArray)), whiplash/websocket.WebsocketError)

One frame, from its bytes, and the bytes after it. Ok(None) when more bytes are needed.

function

encode_frame

src/whiplash/websocket.pooka:148
encode_frame(frame: Frame, mask: Option(BitArray)) -> BitArray
INFERREDfn(whiplash/websocket.Frame, Option(BitArray)) -> BitArray

A frame's bytes. A client passes the four bytes of its mask; a server passes None.

function

text

src/whiplash/websocket.pooka:182
text(text: String) -> BitArray
INFERREDfn(String) -> BitArray

A text message as one unmasked frame, as servers send them.

function

binary

src/whiplash/websocket.pooka:187
binary(data: BitArray) -> BitArray
INFERREDfn(BitArray) -> BitArray

A binary message as one unmasked frame.

function

close

src/whiplash/websocket.pooka:192
close(code: Int, reason: String) -> BitArray
INFERREDfn(Int, String) -> BitArray

A close frame with a code and a reason.

function

accept_key

src/whiplash/websocket.pooka:198
accept_key(key: String) -> String
INFERREDfn(String) -> String

Sec-WebSocket-Accept for a Sec-WebSocket-Key: the key and the GUID of RFC 6455, hashed with SHA-1, in base64.

function

check_upgrade

src/whiplash/websocket.pooka:204
check_upgrade(method: String, headers: List(#(String, String))) -> Result(
  Upgrade,
  WebsocketError
)
INFERREDfn(String, List(#(String, String))) -> Result(whiplash/websocket.Upgrade, whiplash/websocket.WebsocketError)

Check an HTTP/1.1 upgrade request's method and headers (RFC 6455 section 4.2.1).

function

check_connect

src/whiplash/websocket.pooka:236
check_connect(headers: List(#(String, String))) -> Result(
  Upgrade,
  WebsocketError
)
INFERREDfn(List(#(String, String))) -> Result(whiplash/websocket.Upgrade, whiplash/websocket.WebsocketError)

Check an HTTP/2 extended CONNECT's pseudo-headers and headers (RFC 8441 section 4). There is no key, so Upgrade.key is empty.

function

negotiate_deflate

src/whiplash/websocket.pooka:273
negotiate_deflate(offers: List(String)) -> Option(#(Deflate, String))
INFERREDfn(List(String)) -> Option(#(whiplash/websocket.Deflate, String))

Agree to a client's permessage-deflate offer, or not (RFC 7692 section 7.1).

class

Writer

src/whiplash/websocket.pooka:735
class Writer(role: Role, deflate: Option(Deflate), dictionary: BitArray)

Compression's outgoing dictionary, kept as ordinary bytes. Values do not hide live zlib handles: taking a new Writer never mutates its predecessor.

field

role

src/whiplash/websocket.pooka:735
role: Role
field

deflate

src/whiplash/websocket.pooka:735
deflate: Option(Deflate)
field

dictionary

src/whiplash/websocket.pooka:735
dictionary: BitArray
INFERREDBitArray
constructor

new

src/whiplash/websocket.pooka:735
function

writer

src/whiplash/websocket.pooka:737
writer(role: Role, deflate: Option(Deflate)) -> Writer
function

write

src/whiplash/websocket.pooka:748
write(writer: Writer, message: Message, mask: Option(BitArray)) -> Result(
  #(BitArray, Writer),
  WebsocketError
)

Encode a message and return the writer for the next one. A client supplies a fresh unpredictable four-byte mask for each call; a server supplies None.

Pooka
let state = websocket.writer(websocket.Role.Server, None)
let assert Ok(#(bytes, _)) = websocket.write(state, TextMessage("hello"), None)