pub struct WebSocketFrame {
pub fin: bool,
pub opcode: WebSocketOpcode,
pub mask: bool,
pub payload_data: Vec<u8>,
}Expand description
Represents a decoded WebSocket frame.
Fields§
§fin: boolFIN flag indicating if this is the final frame.
opcode: WebSocketOpcodeOpcode indicating the frame type (text, binary, etc.).
mask: boolMask flag indicating if the payload is masked.
payload_data: Vec<u8>The payload data of the frame.
Implementations§
Source§impl WebSocketFrame
impl WebSocketFrame
Sourcepub fn decode_ws_frame<D>(data: D) -> Option<(WebSocketFrame, usize)>
pub fn decode_ws_frame<D>(data: D) -> Option<(WebSocketFrame, usize)>
Decodes a WebSocket frame from the provided data slice.
This function parses the raw bytes from a WebSocket stream according to the WebSocket protocol
specification to reconstruct a WebSocketFrame. It handles FIN bit, opcode, mask bit,
payload length (including extended lengths), mask key, and the payload data itself.
§Arguments
AsRef<[u8]>- The raw data to decode into a WebSocket frame.
§Returns
Option<(WebSocketFrame, usize)>Some((WebSocketFrame, usize)): If the frame is successfully decoded, returns the decoded frame and the number of bytes consumed from the input slice.None: If the frame is incomplete or malformed.
Sourcepub fn create_frame_list<D>(data: D) -> Vec<ResponseBody> ⓘ
pub fn create_frame_list<D>(data: D) -> Vec<ResponseBody> ⓘ
Creates a list of response frames from the provided body.
This method segments the response body into WebSocket frames, respecting the maximum frame size and handling UTF-8 character boundaries for text frames. It determines the appropriate opcode (Text or Binary) based on the body’s content.
§Arguments
AsRef<[u8]>- A reference to a response body (payload) as a byte slice.
§Returns
Vec<ResponseBody>- A vector ofResponseBody(byte vectors), where each element represents a framed WebSocket message.
Sourcepub fn sha1<D>(data: D) -> [u8; 20]
pub fn sha1<D>(data: D) -> [u8; 20]
Calculates the SHA-1 hash of the input data.
This function implements the SHA-1 cryptographic hash algorithm according to RFC 3174. It processes the input data in 512-bit (64-byte) blocks and produces a 160-bit (20-byte) hash.
§Arguments
AsRef<[u8]>- The input data to be hashed.
§Returns
[u8; 20]- A 20-byte array representing the SHA-1 hash of the input data.
Sourcepub fn try_generate_accept_key<K>(key: K) -> Option<String>
pub fn try_generate_accept_key<K>(key: K) -> Option<String>
Generates a WebSocket accept key from the client-provided key, returning an Option<String>.
§Arguments
AsRef<str>- The client-provided key (typically from theSec-WebSocket-Keyheader).
§Returns
Option<String>- An optional string representing the generated WebSocket accept key (typically for theSec-WebSocket-Acceptheader).
Sourcepub fn generate_accept_key<K>(key: K) -> String
pub fn generate_accept_key<K>(key: K) -> String
Generates a WebSocket accept key from the client-provided key.
This function is used during the WebSocket handshake to validate the client’s request. It concatenates the client’s key with a specific GUID, calculates the SHA-1 hash of the result, and then encodes the hash in base64.
§Arguments
AsRef<str>- The client-provided key (typically from theSec-WebSocket-Keyheader).
§Returns
Option<String>- An optional string representing the generated WebSocket accept key (typically for theSec-WebSocket-Acceptheader).
§Panics
This function will panic if the input key cannot be converted to a UTF-8 string.
Sourcepub fn try_base64_encode<D>(data: D) -> Option<String>
pub fn try_base64_encode<D>(data: D) -> Option<String>
Sourcepub fn base64_encode<D>(data: D) -> String
pub fn base64_encode<D>(data: D) -> String
Sourcepub fn is_continuation_opcode(&self) -> bool
pub fn is_continuation_opcode(&self) -> bool
Checks if the opcode is a continuation frame.
§Returns
bool-trueif the opcode isContinuation, otherwisefalse.
Sourcepub fn is_text_opcode(&self) -> bool
pub fn is_text_opcode(&self) -> bool
Sourcepub fn is_binary_opcode(&self) -> bool
pub fn is_binary_opcode(&self) -> bool
Checks if the opcode is a binary frame.
§Returns
bool-trueif the opcode isBinary, otherwisefalse.
Sourcepub fn is_close_opcode(&self) -> bool
pub fn is_close_opcode(&self) -> bool
Checks if the opcode is a close frame.
§Returns
bool-trueif the opcode isClose, otherwisefalse.
Sourcepub fn is_ping_opcode(&self) -> bool
pub fn is_ping_opcode(&self) -> bool
Sourcepub fn is_pong_opcode(&self) -> bool
pub fn is_pong_opcode(&self) -> bool
Sourcepub fn is_reserved_opcode(&self) -> bool
pub fn is_reserved_opcode(&self) -> bool
Checks if the opcode is a reserved frame.
§Returns
bool-trueif the opcode isReserved(_), otherwisefalse.
Source§impl WebSocketFrame
impl WebSocketFrame
pub fn get_fin(&self) -> &bool
pub fn get_opcode(&self) -> &WebSocketOpcode
pub fn get_mask(&self) -> &bool
pub fn get_payload_data(&self) -> &Vec<u8> ⓘ
Trait Implementations§
Source§impl Clone for WebSocketFrame
impl Clone for WebSocketFrame
Source§impl Debug for WebSocketFrame
impl Debug for WebSocketFrame
Source§impl Default for WebSocketFrame
Implements the Default trait for WebSocketFrame.
impl Default for WebSocketFrame
Implements the Default trait for WebSocketFrame.
Provides a default WebSocketFrame with fin: false, opcode: WebSocketOpcode::Text,
mask: false, and an empty payload_data.