Home
HTTP module

ws::WebSocketFramer

WebSocket frame encoder/decoder and handshake validator for RFC 6455.

WebSocketFramer

#include <icy/http/websocket.h>
class WebSocketFramer

Defined in src/http/include/icy/http/websocket.h:156

WebSocket frame encoder/decoder and handshake validator for RFC 6455.

List of all members

NameKindOwner
WebSocketAdapterfriendDeclared here
wsFramerTestAccessfriendDeclared here
wsFramerGetFlagsfriendDeclared here
WebSocketFramerfunctionDeclared here
~WebSocketFramerfunctionDeclared here
writeFramefunctionDeclared here
readFramefunctionDeclared here
handshakeCompletefunctionDeclared here
acceptServerRequestfunctionDeclared here
createClientHandshakeRequestfunctionDeclared here
checkClientHandshakeResponsefunctionDeclared here
completeClientHandshakefunctionDeclared here
frameFlagsfunctionDeclared here
mustMaskPayloadfunctionDeclared here
modefunctionDeclared here
_modevariableDeclared here
_frameFlagsvariableDeclared here
_headerStatevariableDeclared here
_maskPayloadvariableDeclared here
_rndvariableDeclared here
_keyvariableDeclared here
_fragmentedvariableDeclared here
_fragmentOpcodevariableDeclared here
_fragmentBuffervariableDeclared here
_incompleteFramevariableDeclared here

Friends


WebSocketAdapter

friend class WebSocketAdapter

Defined in src/http/include/icy/http/websocket.h:253


wsFramerTestAccess

friend void wsFramerTestAccess(WebSocketFramer & f, int state)

Defined in src/http/include/icy/http/websocket.h:256


wsFramerGetFlags

friend int wsFramerGetFlags(const WebSocketFramer & f)

Defined in src/http/include/icy/http/websocket.h:257

Public Methods

ReturnNameDescription
WebSocketFramerCreates a WebSocketFramer operating in the given endpoint mode. Client-side framers mask outgoing payloads; server-side framers do not.
size_twriteFrame virtualEncodes data into a WebSocket frame and writes it to frame.
uint64_treadFrame virtualDecodes a single WebSocket frame from frame.
boolhandshakeComplete constReturns true if the WebSocket handshake has completed successfully.
voidacceptServerRequestServer side.
voidcreateClientHandshakeRequestClient side.
boolcheckClientHandshakeResponseValidates the server's 101 Switching Protocols response.
voidcompleteClientHandshakeCompletes the client-side handshake by verifying Connection, Upgrade and Sec-WebSocket-Accept headers. Advances internal state to "complete".

WebSocketFramer

WebSocketFramer(ws::Mode mode)

Defined in src/http/include/icy/http/websocket.h:162

Creates a WebSocketFramer operating in the given endpoint mode. Client-side framers mask outgoing payloads; server-side framers do not.

Parameters

  • mode ServerSide or ClientSide.

writeFrame

virtual

virtual size_t writeFrame(const char * data, size_t len, int flags, BitWriter & frame)

Defined in src/http/include/icy/http/websocket.h:173

Encodes data into a WebSocket frame and writes it to frame.

Parameters

Returns

Total number of bytes written to the frame buffer (header + payload).


readFrame

virtual

virtual uint64_t readFrame(BitReader & frame, char *& payload)

Defined in src/http/include/icy/http/websocket.h:184

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.

Parameters

  • frame BitReader positioned at the start of a frame.

  • payload Set to point at the start of the decoded payload. Not null-terminated.

Returns

Payload length in bytes.

Exceptions

  • std::runtime_error on protocol violations or if the buffer is too small.

handshakeComplete

const

bool handshakeComplete() const

Defined in src/http/include/icy/http/websocket.h:187

Returns true if the WebSocket handshake has completed successfully.


acceptServerRequest

void acceptServerRequest(http::Request & request, http::Response & response)

Defined in src/http/include/icy/http/websocket.h:197

Server side.

Validates the client upgrade request and writes a 101 Switching Protocols response. Sets the internal state to mark the handshake as complete.

Parameters

  • request Incoming HTTP upgrade request.

  • response HTTP response to populate with the handshake reply.

Exceptions

  • std::runtime_error if the request is not a valid WebSocket upgrade.

createClientHandshakeRequest

void createClientHandshakeRequest(http::Request & request)

Defined in src/http/include/icy/http/websocket.h:205

Client side.

Populates request with the WebSocket upgrade headers (Connection, Upgrade, Sec-WebSocket-Key, Sec-WebSocket-Version) to initiate the handshake.

Parameters

  • request HTTP request to add upgrade headers to.

checkClientHandshakeResponse

bool checkClientHandshakeResponse(http::Response & response)

Defined in src/http/include/icy/http/websocket.h:211

Validates the server's 101 Switching Protocols response.

Parameters

  • response The HTTP response received from the server.

Returns

true if the handshake succeeded and data can flow.

Exceptions

  • std::runtime_error if the server rejected or mishandled the upgrade.

completeClientHandshake

void completeClientHandshake(http::Response & response)

Defined in src/http/include/icy/http/websocket.h:217

Completes the client-side handshake by verifying Connection, Upgrade and Sec-WebSocket-Accept headers. Advances internal state to "complete".

Parameters

  • response The 101 Switching Protocols response from the server.

Exceptions

  • std::runtime_error if any required header is missing or incorrect.

Protected Methods

ReturnNameDescription
intframeFlags constReturns the frame flags of the most recently received frame. Set by readFrame()
boolmustMaskPayload constReturns true if the payload must be masked. Used by writeFrame()
ws::Modemode const

frameFlags

const

int frameFlags() const

Defined in src/http/include/icy/http/websocket.h:222

Returns the frame flags of the most recently received frame. Set by readFrame()


mustMaskPayload

const

bool mustMaskPayload() const

Defined in src/http/include/icy/http/websocket.h:226

Returns true if the payload must be masked. Used by writeFrame()


mode

const

ws::Mode mode() const

Defined in src/http/include/icy/http/websocket.h:228

Private Attributes

ReturnNameDescription
ws::Mode_mode
int_frameFlags
int_headerState
bool_maskPayload
Random_rnd
std::string_key
bool_fragmentedCurrently receiving a fragmented message.
int_fragmentOpcodeOpcode of the first frame in the fragment sequence.
Buffer_fragmentBufferAccumulated payload from continuation frames.
Buffer_incompleteFrameBuffer for incomplete frame data across TCP segments.

_mode

ws::Mode _mode

Defined in src/http/include/icy/http/websocket.h:238


_frameFlags

int _frameFlags

Defined in src/http/include/icy/http/websocket.h:239


_headerState

int _headerState

Defined in src/http/include/icy/http/websocket.h:240


_maskPayload

bool _maskPayload

Defined in src/http/include/icy/http/websocket.h:241


_rnd

Random _rnd

Defined in src/http/include/icy/http/websocket.h:242


_key

std::string _key

Defined in src/http/include/icy/http/websocket.h:243


_fragmented

bool _fragmented {false}

Defined in src/http/include/icy/http/websocket.h:246

Currently receiving a fragmented message.


_fragmentOpcode

int _fragmentOpcode {0}

Defined in src/http/include/icy/http/websocket.h:247

Opcode of the first frame in the fragment sequence.


_fragmentBuffer

Buffer _fragmentBuffer

Defined in src/http/include/icy/http/websocket.h:248

Accumulated payload from continuation frames.


_incompleteFrame

Buffer _incompleteFrame

Defined in src/http/include/icy/http/websocket.h:251

Buffer for incomplete frame data across TCP segments.