sig_ws.h

include/sig/http/sig_ws.h

WebSocket (RFC 6455) handshake and framing primitives. Pure functions: no sockets, no I/O. Only unfragmented text and control frames are supported.

sig_ws_opcode

typedef enum { SIG_WS_TEXT = 1, SIG_WS_CLOSE = 8, SIG_WS_PING = 9, SIG_WS_PONG = 10 } sig_ws_opcode;

sig_ws_accept_key

sig_status_t sig_ws_accept_key(const char * client_key, char [29] out)
ParameterType
client_keyconst char *
outchar [29]

Computes the Sec-WebSocket-Accept value for `client_key` (the client's Sec-WebSocket-Key, NUL-terminated) into `out` as a NUL-terminated string. SIG_ERR_INVALID_ARG on a NULL argument.

sig_ws_encode

sig_status_t sig_ws_encode(sig_ws_opcode op, const void * payload, size_t len, char ** out, size_t * out_len)
ParameterType
opsig_ws_opcode
payloadconst void *
lensize_t
outchar **
out_lensize_t *

Encodes one unmasked, unfragmented server frame. *out is sig_alloc'd (free with sig_free) and holds *out_len bytes. `payload` may be NULL when len is 0.

sig_ws_decode_result

typedef enum {
    SIG_WS_FRAME,          /* one complete frame decoded into *out */
    SIG_WS_INCOMPLETE,     /* need more bytes */
    SIG_WS_PROTOCOL_ERROR, /* unmasked client frame, reserved bits, unknown opcode,
                              fragmented (FIN clear or opcode 0), or control frame > 125 bytes */
    SIG_WS_TOO_LARGE       /* payload exceeds max_payload */
} sig_ws_decode_result;

sig_ws_frame

typedef struct {
    sig_ws_opcode op;
    unsigned char *payload; /* points into the caller's buffer, unmasked in place */
    size_t len;
    size_t consumed; /* bytes of the buffer this frame occupied; advance by this */
} sig_ws_frame;

sig_ws_decode

sig_ws_decode_result sig_ws_decode(unsigned char * buf, size_t len, size_t max_payload, sig_ws_frame * out)
ParameterType
bufunsigned char *
lensize_t
max_payloadsize_t
outsig_ws_frame *

Decodes one masked client frame from the start of buf[0..len). Unmasks the payload in place. *out is only valid for SIG_WS_FRAME.