From 1b4efe2e9faac3d96ddfb9347c2028477663f01d Mon Sep 17 00:00:00 2001 From: tottoto Date: Wed, 3 Apr 2024 07:13:09 +0900 Subject: [PATCH] feat: implement From bytes for SecWebsocketKey --- src/common/sec_websocket_key.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/common/sec_websocket_key.rs b/src/common/sec_websocket_key.rs index 8f6f7c01..0ef63635 100644 --- a/src/common/sec_websocket_key.rs +++ b/src/common/sec_websocket_key.rs @@ -1,8 +1,30 @@ +use base64::{engine::general_purpose::STANDARD, Engine}; +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 From<[u8; 16]> for SecWebsocketKey { + fn from(bytes: [u8; 16]) -> Self { + let mut value = HeaderValue::from_str(&STANDARD.encode(bytes)).unwrap(); + value.set_sensitive(true); + Self(value) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn from_bytes() { + let bytes: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; + let _ = SecWebsocketKey::from(bytes); + } +}