nx_websocket.nx
buildroot/runtime/nx_websocket.nx
about
nx_websocket.nx -- RFC 6455 server-side WebSocket primitive.
Pure NishiLang. No external library. Composes nx_sha1 and
nx_base64 (both already shipped) for the handshake; implements
the binary framing (opcodes, masking, payload-length variants)
from the RFC text.
Server-side flow:
1. Client sends:
GET /ws HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: <base64 16-byte nonce>
Sec-WebSocket-Version: 13
We compute Sec-WebSocket-Accept = base64(sha1(key + GUID))
where GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (RFC 6455
section 1.3).
2. Reply:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: <accept>
3. Subsequent traffic is binary frames.
Frame layout (most-common case; mask MUST be present for client->server):
[byte 0] FIN (1) | RSV (3) | OPCODE (4)
[byte 1] MASK (1) | LEN7 (7)
LEN7 in 0..125: payload length is LEN7
LEN7 == 126: next 2 bytes (big-endian) are payload length
LEN7 == 127: next 8 bytes (big-endian) are payload length
[4 bytes] masking-key (when MASK=1)
[N bytes] payload (XORed with masking-key when MASK=1)
Common opcodes:
0x0 continuation
0x1 text
0x2 binary
0x8 close
0x9 ping
0xA pong
dependencies 2 imports · 0 importers
imports: nx_syscalls_x86_64.nxnx_ws_crypto.nx
imported by: nobody (leaf or entry point)
structs
| none |
consts
| 56 | const NX_WS_VERDICT_UNKNOWN: i64 = 0 |
| 57 | const NX_WS_VERDICT_OK: i64 = 1 |
| 58 | const NX_WS_VERDICT_BAD_HANDSHAKE: i64 = 2 |
| 59 | const NX_WS_VERDICT_BUF_TOO_SMALL: i64 = 3 |
| 60 | const NX_WS_VERDICT_TRUNCATED: i64 = 4 |
| 61 | const NX_WS_VERDICT_UNSUPPORTED_FRAME: i64 = 5 |
| 62 | const NX_WS_VERDICT_BAD_PARAMS: i64 = 6 |
| 63 | const NX_WS_VERDICT_N: i64 = 7 |
| 65 | const NX_WS_OPCODE_CONT: i64 = 0 |
| 66 | const NX_WS_OPCODE_TEXT: i64 = 1 |
| 67 | const NX_WS_OPCODE_BINARY: i64 = 2 |
| 68 | const NX_WS_OPCODE_CLOSE: i64 = 8 |
| 69 | const NX_WS_OPCODE_PING: i64 = 9 |
| 70 | const NX_WS_OPCODE_PONG: i64 = 10 |
| 72 | const NX_WS_FIN_BIT: i64 = 128 |
| 73 | const NX_WS_MASK_BIT: i64 = 128 |
functions
| 76 | func _ws_guid(buf: *u8) -> i64 |
| 96 | func _ws_write_guid_at(combined: *u8, k: i64) -> i64 called by 1: nx_ws_accept_key |
| 115 | func _ws_copy(dst: *u8, src: *u8, n: i64) -> i64 called by 1: nx_ws_accept_key |
| 127 | func nx_ws_accept_key(client_key: *u8, key_len: i64, |
| 147 | func nx_ws_build_handshake_response(out_buf: *u8, out_cap: i64, |
| 203 | func nx_ws_build_frame(out_buf: *u8, out_cap: i64, |
| 248 | func nx_ws_parse_frame_inplace( |
| 299 | func nx_ws_verdict_is_valid(v: i64) -> i64 |