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
Inherent implementation of WebSocketFrame.
impl WebSocketFrame
Inherent implementation of 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
D- The raw data to decode into a WebSocket frame, which requires theAsRef<[u8]>bound.
§Returns
Option<(WebSocketFrame, usize)>-Some((frame, consumed))when the frame is successfully decoded, otherwiseNonewhen the frame is incomplete or malformed.
Sourcepub fn create_frame_list<D>(data: D) -> Vec<Vec<u8>>
pub fn create_frame_list<D>(data: D) -> Vec<Vec<u8>>
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
D- A reference to a response body (payload) as a byte slice, which requires theAsRef<[u8]>bound.
§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
D- The input data to be hashed, which requires theAsRef<[u8]>bound.
§Returns
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
K- The client-provided key (typically from theSec-WebSocket-Keyheader), which requires theAsRef<str>bound.
§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
K- The client-provided key (typically from theSec-WebSocket-Keyheader), which requires theAsRef<str>bound.
§Returns
String- 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
Encodes the input data as a base64 string.
§Arguments
D- The data to encode in base64, which requires theAsRef<[u8]>bound.
§Returns
String- A string with the base64 encoded representation of the input data.
§Panics
This function will panic if the input data cannot be converted to a UTF-8 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§fn clone(&self) -> WebSocketFrame
fn clone(&self) -> WebSocketFrame
1.0.0 (const: unstable) · 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.
Source§fn default() -> WebSocketFrame
fn default() -> WebSocketFrame
Source§impl Display for WebSocketFrame
impl Display for WebSocketFrame
impl Eq for WebSocketFrame
Source§impl PartialEq for WebSocketFrame
impl PartialEq for WebSocketFrame
impl StructuralPartialEq for WebSocketFrame
Auto Trait Implementations§
impl Freeze for WebSocketFrame
impl RefUnwindSafe for WebSocketFrame
impl Send for WebSocketFrame
impl Sync for WebSocketFrame
impl Unpin for WebSocketFrame
impl UnsafeUnpin for WebSocketFrame
impl UnwindSafe for WebSocketFrame
Blanket Implementations§
impl<T> AnySend for T
impl<T> AnySendClone for T
impl<T> AnySendSync for T
impl<T> AnySendSyncClone for T
impl<T> AnySync for T
impl<T> AnySyncClone for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.