-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SecWebsocketKey random constructor
- Loading branch information
Showing
3 changed files
with
36 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,38 @@ | ||
use http::HeaderValue; | ||
|
||
/// The `Sec-Websocket-Key` header. | ||
#[derive(Clone, Debug, PartialEq, Eq, Hash)] | ||
pub struct SecWebsocketKey(pub(super) http::HeaderValue); | ||
pub struct SecWebsocketKey(pub(super) HeaderValue); | ||
|
||
derive_header! { | ||
SecWebsocketKey(_), | ||
name: SEC_WEBSOCKET_KEY | ||
} | ||
|
||
impl SecWebsocketKey { | ||
/// Create a random `SecWebsocketKey`. | ||
#[cfg(feature = "rand")] | ||
#[cfg_attr(docsrs, cfg(feature = "rand"))] | ||
pub fn random() -> Self { | ||
use base64::{engine::general_purpose::STANDARD, Engine}; | ||
|
||
let bytes: [u8; 16] = rand::random(); | ||
let mut value = HeaderValue::from_str(&STANDARD.encode(bytes)).unwrap(); | ||
|
||
value.set_sensitive(true); | ||
|
||
Self(value) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[cfg(feature = "rand")] | ||
use super::SecWebsocketKey; | ||
|
||
#[cfg(feature = "rand")] | ||
#[test] | ||
fn random() { | ||
let _ = SecWebsocketKey::random(); | ||
} | ||
} |