pub struct WebSocketFrame { /* private fields */ }
Expand description
Represents a decoded WebSocket frame.
Implementations§
Source§impl WebSocketFrame
impl WebSocketFrame
Sourcepub fn decode_ws_frame(data: &[u8]) -> WebsocketFrameWithLengthOption
pub fn decode_ws_frame(data: &[u8]) -> WebsocketFrameWithLengthOption
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
&[u8]
- The raw data slice from the WebSocket stream.
§Returns
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_response_frame_list(body: &ResponseBody) -> Vec<ResponseBody> ⓘ
pub fn create_response_frame_list(body: &ResponseBody) -> 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
&ResponseBody
- A reference to a response body (payload) as a byte slice.
§Returns
- A vector of
ResponseBody
(byte vectors), where each element represents a framed WebSocket message.
Sourcepub fn sha1(data: &[u8]) -> [u8; 20]
pub fn sha1(data: &[u8]) -> [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
&[u8]
- A byte slice containing the input data to be hashed.
§Returns
- A 20-byte array representing the SHA-1 hash of the input data.
Sourcepub fn generate_accept_key(key: &str) -> String
pub fn generate_accept_key(key: &str) -> 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
&str
- A string slice containing the client-provided key (typically from theSec-WebSocket-Key
header).
§Returns
- A string representing the generated WebSocket accept key (typically for the
Sec-WebSocket-Accept
header).
Sourcepub fn base64_encode(data: &[u8]) -> String
pub fn base64_encode(data: &[u8]) -> String
Encodes the input data as a base64 string.
This function implements the Base64 encoding scheme, converting binary data into an ASCII string format. It processes the input data in chunks of 3 bytes and encodes them into 4 base64 characters. Padding with ‘=’ characters is applied if necessary.
§Arguments
&[u8]
- A byte slice containing the data to encode in base64.
§Returns
- A string with the base64 encoded representation of the input data.
Sourcepub fn is_continuation_opcode(&self) -> bool
pub fn is_continuation_opcode(&self) -> bool
Checks if the opcode is a continuation frame.
§Returns
true
if the opcode is Continuation
, otherwise false
.
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
Sourcepub fn is_close_opcode(&self) -> bool
pub fn is_close_opcode(&self) -> bool
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
true
if the opcode is Reserved(_)
, otherwise false
.
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§fn clone(&self) -> WebSocketFrame
fn clone(&self) -> WebSocketFrame
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§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
.