#include <icy/http/websocket.h>WebSocket frame encoder/decoder and handshake validator for RFC 6455.
| Return | Name | Description |
|---|---|---|
WebSocketFramer | Creates a WebSocketFramer operating in the given endpoint mode. Client-side framers mask outgoing payloads; server-side framers do not. | |
size_t | writeFrame virtual | Encodes data into a WebSocket frame and writes it to frame. |
uint64_t | readFrame virtual | Decodes a single WebSocket frame from frame. |
bool | handshakeComplete const | Returns true if the WebSocket handshake has completed successfully. |
void | acceptServerRequest | Server side. |
void | createClientHandshakeRequest | Client side. |
bool | checkClientHandshakeResponse | Validates the server's 101 Switching Protocols response. |
void | completeClientHandshake | Completes the client-side handshake by verifying Connection, Upgrade and Sec-WebSocket-Accept headers. Advances internal state to "complete". |
WebSocketFramer(ws::Mode mode)Creates a WebSocketFramer operating in the given endpoint mode. Client-side framers mask outgoing payloads; server-side framers do not.
mode ServerSide or ClientSide.virtual
virtual size_t writeFrame(const char * data, size_t len, int flags, BitWriter & frame)Encodes data into a WebSocket frame and writes it to frame.
data Pointer to the payload data.
len Payload length in bytes.
flags Frame flags: ws::SendFlags::Text, ws::SendFlags::Binary, or a control frame opcode combined with FrameFlags::Fin.
frame BitWriter to write the encoded frame into.
Total number of bytes written to the frame buffer (header + payload).
virtual
virtual uint64_t readFrame(BitReader & frame, char *& payload)Decodes a single WebSocket frame from frame.
The payload is unmasked in-place in the source buffer; no copy is made. payload is set to point at the start of the payload within frame's buffer.
frame BitReader positioned at the start of a frame.
payload Set to point at the start of the decoded payload. Not null-terminated.
Payload length in bytes.
std::runtime_error on protocol violations or if the buffer is too small.const
bool handshakeComplete() constReturns true if the WebSocket handshake has completed successfully.
void acceptServerRequest(http::Request & request, http::Response & response)Server side.
Validates the client upgrade request and writes a 101 Switching Protocols response. Sets the internal state to mark the handshake as complete.
request Incoming HTTP upgrade request.
response HTTP response to populate with the handshake reply.
std::runtime_error if the request is not a valid WebSocket upgrade.void createClientHandshakeRequest(http::Request & request)Client side.
Populates request with the WebSocket upgrade headers (Connection, Upgrade, Sec-WebSocket-Key, Sec-WebSocket-Version) to initiate the handshake.
request HTTP request to add upgrade headers to.bool checkClientHandshakeResponse(http::Response & response)Validates the server's 101 Switching Protocols response.
response The HTTP response received from the server.true if the handshake succeeded and data can flow.
std::runtime_error if the server rejected or mishandled the upgrade.void completeClientHandshake(http::Response & response)Completes the client-side handshake by verifying Connection, Upgrade and Sec-WebSocket-Accept headers. Advances internal state to "complete".
response The 101 Switching Protocols response from the server.std::runtime_error if any required header is missing or incorrect.| Return | Name | Description |
|---|---|---|
int | frameFlags const | Returns the frame flags of the most recently received frame. Set by readFrame() |
bool | mustMaskPayload const | Returns true if the payload must be masked. Used by writeFrame() |
ws::Mode | mode const |
const
int frameFlags() constReturns the frame flags of the most recently received frame. Set by readFrame()
const
bool mustMaskPayload() constReturns true if the payload must be masked. Used by writeFrame()
const
ws::Mode mode() const| Return | Name | Description |
|---|---|---|
ws::Mode | _mode | |
int | _frameFlags | |
int | _headerState | |
bool | _maskPayload | |
Random | _rnd | |
std::string | _key | |
bool | _fragmented | Currently receiving a fragmented message. |
int | _fragmentOpcode | Opcode of the first frame in the fragment sequence. |
Buffer | _fragmentBuffer | Accumulated payload from continuation frames. |
Buffer | _incompleteFrame | Buffer for incomplete frame data across TCP segments. |
ws::Mode _modeint _frameFlagsint _headerStatebool _maskPayloadRandom _rndstd::string _keybool _fragmented {false}Currently receiving a fragmented message.
int _fragmentOpcode {0}Opcode of the first frame in the fragment sequence.
Buffer _fragmentBufferAccumulated payload from continuation frames.
Buffer _incompleteFrameBuffer for incomplete frame data across TCP segments.