~/bend-docscommunity

pure.bend checks

raw source on the hub · import bend-kit-websocket@0.4.1.5/pure.bend as Pure

Pure RFC 6455 handshake validation, frame codec, and message assembly.

2 imports
import Base
import bend-kit-bytes@0.3.3.1/bytes.bend as Bytes

Types

type Split source · line 209 · raw

Type

An HTTP head cut at its blank line, with the bytes after it.

type Frame source · line 242 · raw

@-P:Type -> Type

The payload P is Bytes at run time and a List of octets in the proofs.

type Decode source · line 247 · raw

@-P:Type -> @-S:Type -> Type

Need: the input does not hold a whole frame yet, and comes back intact. Got: one frame, and the octets after it. Bad: the input breaks §5.2 whatever comes next.

type Partial source · line 627 · raw

Type

type Msg source · line 631 · raw

Type

Definitions

def guid source · line 12 · raw

String

def accept.input source · line 16 · raw

@key:String -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

The octets SHA-1 hashes for Sec-WebSocket-Accept: the key, then the GUID.

def accept.of source · line 20 · raw

@digest:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> String

Sec-WebSocket-Accept from the SHA-1 digest of accept.input(key).

def accept.fin source · line 23 · raw

@r:Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)> -> Result<&1, &1, Pair(U32, String), String>

def key.of source · line 31 · raw

@nonce:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> String

Sec-WebSocket-Key from a 16-octet nonce.

def key.fin source · line 34 · raw

@r:Result<&1, &1, Pair(U32, String), Pair(U32, Array<U32>)> -> Result<&1, &1, Pair(U32, String), String>

def first source · line 41 · raw

@m:Maybe<&1, Pair(U32, String)> -> @n:Maybe<&1, Pair(U32, String)> -> Maybe<&1, Pair(U32, String)>

def reject source · line 48 · raw

@bad:Bool -> @+code:U32 -> @+why:String -> Maybe<&1, Pair(U32, String)>

def verdict source · line 55 · raw

@-A:Type -> @m:Maybe<&1, Pair(U32, String)> -> @v:A -> Result<&1, &1, Pair(U32, String), A>

def visible source · line 63 · raw

@s:String -> Bool

Every char is visible ASCII (0x21-0x7E): no space, CR, LF, or other control.

def request source · line 73 · raw

@+host:String -> @+path:String -> @+key:String -> Result<&1, &1, Pair(U32, String), 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

The GET that asks to upgrade. host is the Host header (with :port when not the default); path is the origin-form target ("/" and on, percent-encoded). A char outside visible ASCII could split the head and inject headers, so it fails with EINVAL (22).

def fields.line source · line 82 · raw

@parts:List<&2, String> -> @+name:String -> @+later:List<&2, String> -> List<&2, String>

def fields.go source · line 90 · raw

@ls:List<&2, String> -> @+name:String -> List<&2, String>

def after.first source · line 97 · raw

@ls:List<&2, String> -> List<&2, String>

def headers source · line 105 · raw

@head:String -> List<&2, String>

The header lines of an HTTP head: every line after the start line.

def fields source · line 109 · raw

@head:String -> @+name:String -> List<&2, String>

The trimmed values of every header line named name (lowercase), in order.

def only source · line 112 · raw

@xs:List<&2, String> -> Maybe<&2, String>

def field source · line 120 · raw

@head:String -> @+name:String -> Maybe<&2, String>

The value of the header named name when exactly one line has it.

def line.parts source · line 125 · raw

@parts:List<&2, String> -> Bool

A header line is name ":" value, with a non-empty name that holds no space or tab (RFC 9112 §5.1). That rules out obsolete line folding and bare text.

def lines.ok source · line 132 · raw

@ls:List<&2, String> -> Bool

def status.parts source · line 139 · raw

@parts:List<&2, String> -> Bool

def status source · line 147 · raw

@ls:List<&2, String> -> Bool

The first line is exactly "HTTP/1.1 101" and a reason, with nothing before it.

def token source · line 154 · raw

@xs:List<&2, String> -> @+want:String -> Bool

def value.is source · line 161 · raw

@m:Maybe<&2, String> -> @+want:String -> Bool

def value.lower source · line 168 · raw

@m:Maybe<&2, String> -> Maybe<&2, String>

def tokens source · line 176 · raw

@xs:List<&2, String> -> @+want:String -> Bool

Connection is a list header: its lines join into one comma list (RFC 9110 §5.3).

def none source · line 183 · raw

@xs:List<&2, String> -> Bool

def check source · line 193 · raw

@+head:String -> @+want:String -> Result<&1, &1, Pair(U32, String), Unit>

Does a server's head (up to its blank line) accept the upgrade? want is accept(key). Upgrade and Sec-WebSocket-Accept must each come once. No extension or subprotocol was asked for, so the server may name none (§4.1).

def with_slice source · line 204 · raw

@-R:Type -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> @k:(@_:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @_:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> R) -> R

def split.long source · line 214 · raw

@long:Bool -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Split

def split.short source · line 221 · raw

@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Split

def split.found source · line 225 · raw

@r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Maybe<&2, U32>) -> Split

def split source · line 236 · raw

@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Split

Found once b holds "\r\n\r\n"; Long when 16 KiB came without it.

def mask.go source · line 252 · raw

@n:Nat -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @+key:U32 -> Array<U32>

def mask.trim.word source · line 262 · raw

@+k:U32 -> @+m:U32 -> @r:Pair(Array<U32>, U32) -> Array<U32>

def mask.trim.if source · line 266 · raw

@whole:Bool -> @+k:U32 -> @+m:U32 -> @a:Array<U32> -> Array<U32>

def mask.trim source · line 274 · raw

@+len:U32 -> @a:Array<U32> -> Array<U32>

Bytes at or past len stay 0, as Bytes requires.

def mask source · line 280 · raw

@+key:U32 -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

The payload XOR the mask key (§5.3), a word at a time. key's byte k (low first) is mask octet k. Masking twice gives the payload back.

def known source · line 284 · raw

@+op:U32 -> Bool

def rules.big source · line 303 · raw

@+fin:Bool -> @+rsv:U32 -> @+op:U32 -> @big:Bool -> Maybe<&1, Pair(U32, String)>

The §5.2 and §5.5 rules on a frame header. No extension is in use, so RSV must be 0. big: the payload is over 125 octets.

def rules source · line 309 · raw

@+fin:Bool -> @+rsv:U32 -> @+op:U32 -> @+len:U32 -> Maybe<&1, Pair(U32, String)>

def encode.ext source · line 312 · raw

@+ext:U32 -> @+len:U32 -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

def encode.mkey source · line 321 · raw

@+keylen:U32 -> @+at:U32 -> @+key:U32 -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

def encode.copy source · line 328 · raw

@+len:U32 -> @src:Array<U32> -> @+hs:U32 -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

def encode.frame source · line 333 · raw

@+b0:U32 -> @+mbit:U32 -> @+keylen:U32 -> @+key:U32 -> @p:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

Payload lengths use the shortest form (§5.2): 7 bits, then 16, then 64.

def encode.key source · line 341 · raw

@key:Maybe<&2, U32> -> @+b0:U32 -> @p:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

def encode.checked source · line 348 · raw

@err:Maybe<&1, Pair(U32, String)> -> @+fin:Bool -> @+op:U32 -> @key:Maybe<&2, U32> -> @p:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Result<&1, &1, Pair(U32, String), 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

def encode source · line 357 · raw

@f:Frame<0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> @key:Maybe<&2, U32> -> Result<&1, &1, Pair(U32, String), 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes>

A frame's octets. A client passes Some{mask key} and the payload goes masked; None sends it clear, as a server does. Fails on a frame that breaks rules.

def header.verdict source · line 369 · raw

@err:Maybe<&1, Pair(U32, String)> -> @+fin:Bool -> @+op:U32 -> @+m:Bool -> @+key:U32 -> @+hs:U32 -> @+len:U32 -> Header

def header.err source · line 377 · raw

@+masked:Bool -> @+b0:U32 -> @+m:Bool -> @+hi:U32 -> @+len:U32 -> Maybe<&1, Pair(U32, String)>

A 64-bit length with a high word is over any U32 max, and is a control frame over 125.

def header.check source · line 382 · raw

@+masked:Bool -> @+b0:U32 -> @+m:Bool -> @+key:U32 -> @+hs:U32 -> @+hi:U32 -> @+len:U32 -> Header

def be32 source · line 386 · raw

@+a:U32 -> @+b:U32 -> @+c:U32 -> @+d:U32 -> U32

Four octets, most significant first. Multiply and add, not shift and or: the proofs reason about them over Nat.

def header.key source · line 390 · raw

@m:Bool -> @t:List<&2, U32> -> @+masked:Bool -> @+b0:U32 -> @+hs:U32 -> @+hi:U32 -> @+len:U32 -> Header

The mask key (§5.3) follows the length; its first octet is the key's low byte.

def header.ext16 source · line 401 · raw

@t:List<&2, U32> -> @+masked:Bool -> @+b0:U32 -> @+m:Bool -> Header

def header.ext64 source · line 408 · raw

@t:List<&2, U32> -> @+masked:Bool -> @+b0:U32 -> @+m:Bool -> Header

def header.ext source · line 416 · raw

@short:Bool -> @mid:Bool -> @t:List<&2, U32> -> @+masked:Bool -> @+b0:U32 -> @+m:Bool -> @+l7:U32 -> Header

short: no extended length (l7 < 126); mid: 2 octets of it (l7 == 126); else 8.

def header.b1 source · line 427 · raw

@m:Bool -> @t:List<&2, U32> -> @+masked:Bool -> @+b0:U32 -> @+b1:U32 -> Header

def bytes.prefix.con source · line 495 · raw

@+v:U32 -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, List<&2, U32>) -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, List<&2, U32>)

def bytes.prefix.go source · line 499 · raw

@n:Nat -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Maybe<&2, U32>) -> @+i:U32 -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, List<&2, U32>)

def bytes.prefix.back source · line 512 · raw

@+pos:U32 -> @+start:U32 -> @+end:U32 -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, List<&2, U32>) -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor, List<&2, U32>)

def bytes.prefix source · line 516 · raw

@c:0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor, List<&2, U32>)

def bytes.fits source · line 521 · raw

@c:0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor -> @+hs:U32 -> @+len:U32 -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor, Bool)

hs + len octets fit in end - pos exactly when this holds (Bytes law fits_exact); see law cut_exact.

def bytes.cut.rest source · line 525 · raw

@+pos:U32 -> @+start:U32 -> @+end:U32 -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes) -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor)

def bytes.cut source · line 529 · raw

@c:0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor -> @+hs:U32 -> @+len:U32 -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor)

def bytes.unmask source · line 534 · raw

@+key:U32 -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

def parse source · line 538 · raw

@+masked:Bool -> @+max:U32 -> @c:0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor -> Decode<0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, 0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor>

The decoder over Bytes, as recv runs it.

def push.when source · line 541 · raw

@whole:Bool -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @+pos:U32 -> @+end:U32 -> @read:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor

def push source · line 550 · raw

@c:0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor -> @read:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Cursor

The input with a read after it. Decoded octets are dropped first, so each octet is copied once for the frame it ends up in; when there are none, the read goes into the same buffer.

def list.prefix source · line 556 · raw

@xs:List<&2, U32> -> Pair(List<&2, U32>, List<&2, U32>)

def list.fits source · line 560 · raw

@xs:List<&2, U32> -> @+hs:U32 -> @+len:U32 -> Pair(List<&2, U32>, Bool)

def list.cut source · line 564 · raw

@xs:List<&2, U32> -> @+hs:U32 -> @+len:U32 -> Pair(List<&2, U32>, List<&2, U32>)

def list.unmask.go source · line 570 · raw

@xs:List<&2, U32> -> @+a:U32 -> @+b:U32 -> @+c:U32 -> @+d:U32 -> List<&2, U32>

Octet i goes XOR key octet i mod 4 (§5.3): a, b, c, d are the key's octets, low first, and turn one place per octet.

def list.unmask source · line 577 · raw

@+key:U32 -> @xs:List<&2, U32> -> List<&2, U32>

def list.next source · line 580 · raw

@+masked:Bool -> @+max:U32 -> @xs:List<&2, U32> -> Decode<List<&2, U32>, List<&2, U32>>

def st source · line 585 · raw

@+need:U32 -> @+lo:U32 -> @+hi:U32 -> U32

UTF-8 (RFC 3629) validity. The state packs need | lo << 8 | hi << 16: continuation bytes still needed and the range the next one must fall in. need 255 is a failure.

def utf8.lead source · line 588 · raw

@+b:U32 -> U32

def utf8.next source · line 599 · raw

@+s:U32 -> @+b:U32 -> U32

def utf8.go source · line 606 · raw

@n:Nat -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @+s:U32 -> Pair(Array<U32>, Bool)

def utf8.fin source · line 616 · raw

@+len:U32 -> @r:Pair(Array<U32>, Bool) -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Bool)

def utf8.valid source · line 621 · raw

@b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Bool)

b back, and whether it is well-formed UTF-8.

def done.text source · line 636 · raw

@ok:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Bool) -> Result<&1, &1, Pair(U32, String), Pair(Partial, Msg)>

def finish source · line 644 · raw

@+op:U32 -> @d:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Result<&1, &1, Pair(U32, String), Pair(Partial, Msg)>

def feed.more source · line 651 · raw

@fin:Bool -> @+op:U32 -> @d:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Result<&1, &1, Pair(U32, String), Pair(Partial, Msg)>

def feed.size source · line 658 · raw

@over:Bool -> @+fin:Bool -> @+op:U32 -> @d:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Result<&1, &1, Pair(U32, String), Pair(Partial, Msg)>

def feed.join source · line 665 · raw

@+fin:Bool -> @+op:U32 -> @d:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @+max:U32 -> Result<&1, &1, Pair(U32, String), Pair(Partial, Msg)>

def feed.data source · line 669 · raw

@p:Partial -> @+fin:Bool -> @+op:U32 -> @d:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @+max:U32 -> Result<&1, &1, Pair(U32, String), Pair(Partial, Msg)>

def feed.kind source · line 684 · raw

@ctl:Bool -> @p:Partial -> @+fin:Bool -> @+op:U32 -> @d:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> @+max:U32 -> Result<&1, &1, Pair(U32, String), Pair(Partial, Msg)>

def feed source · line 693 · raw

@p:Partial -> @f:Frame<0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes> -> @+max:U32 -> Result<&1, &1, Pair(U32, String), Pair(Partial, Msg)>

One parsed frame into the message being built. A whole text message must be UTF-8 (1007); max caps a message's total size (1009). Keep max at or below 2^31.

def close.payload source · line 700 · raw

@+code:U32 -> @reason:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes

code (big-endian) then a UTF-8 reason.

def close.code.ok source · line 704 · raw

@+c:U32 -> Bool

Codes a peer may send: §7.4.1, the IANA registry through 1014, and 3000-4999.

def close.utf8 source · line 709 · raw

@+c:U32 -> @r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Bool) -> Result<&1, &1, Pair(U32, String), Pair(U32, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes)>

def close.reason source · line 717 · raw

@ok:Bool -> @+c:U32 -> @b:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Result<&1, &1, Pair(U32, String), Pair(U32, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes)>

def close.code source · line 726 · raw

@r:Pair(0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes, Maybe<&2, U32>) -> Result<&1, &1, Pair(U32, String), Pair(U32, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes)>

def close.parse source · line 735 · raw

@p:0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes -> Result<&1, &1, Pair(U32, String), Pair(U32, 0xec25e4adfb02e59e4b817801115988ee/bytes.Bytes)>

The code and reason of a close frame. An empty payload is 1005 (no status).

Templates

template unmask.when source · line 447 · raw

@-P:Type -> @-unmask:(@+key:U32 -> @_:P -> P) -> @m:Bool -> @+key:U32 -> @p:P -> P

The decoder, over any input S with four operations, giving payloads P: prefix gives at least the first 14 octets as a list; fits tells if hs + len octets are in; cut splits into the payload [hs, hs + len) and the input from hs + len on; unmask XORs a mask key.

template next.got source · line 454 · raw

@-P:Type -> @-S:Type -> @-unmask:(@+key:U32 -> @_:P -> P) -> @+fin:Bool -> @+op:U32 -> @+m:Bool -> @+key:U32 -> @r:Pair(P, S) -> Decode<P, S>

template next.fits source · line 458 · raw

@-P:Type -> @-S:Type -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(P, S)) -> @-unmask:(@+key:U32 -> @_:P -> P) -> @+fin:Bool -> @+op:U32 -> @+m:Bool -> @+key:U32 -> @+hs:U32 -> @+len:U32 -> @r:Pair(S, Bool) -> Decode<P, S>

template next.size source · line 467 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, Bool)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(P, S)) -> @-unmask:(@+key:U32 -> @_:P -> P) -> @over:Bool -> @+fin:Bool -> @+op:U32 -> @+m:Bool -> @+key:U32 -> @+hs:U32 -> @+len:U32 -> @b:S -> Decode<P, S>

over: the payload is longer than max.

template next.head source · line 474 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, Bool)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(P, S)) -> @-unmask:(@+key:U32 -> @_:P -> P) -> @+max:U32 -> @h:Header -> @b:S -> Decode<P, S>

template next.start source · line 483 · raw

@-P:Type -> @-S:Type -> @-fits:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, Bool)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(P, S)) -> @-unmask:(@+key:U32 -> @_:P -> P) -> @+masked:Bool -> @+max:U32 -> @r:Pair(S, List<&2, U32>) -> Decode<P, S>

template next source · line 489 · raw

@-P:Type -> @-S:Type -> @-prefix:(@_:S -> Pair(S, List<&2, U32>)) -> @-fits:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(S, Bool)) -> @-cut:(@_:S -> @+hs:U32 -> @+len:U32 -> Pair(P, S)) -> @-unmask:(@+key:U32 -> @_:P -> P) -> @+masked:Bool -> @+max:U32 -> @b:S -> Decode<P, S>

One frame from the front of b, unmasked. masked: this peer's frames must be masked (true on a server, false on a client). max caps the payload length (1009 over it).