From 19dd627a7c142b04586ba559b8dfc8ff716861af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E5=A5=95GaoYi?= Date: Mon, 18 Mar 2024 15:53:28 +0800 Subject: [PATCH] Add 'WebSocket::accept_key()'. --- contrib/ws/src/websocket.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/contrib/ws/src/websocket.rs b/contrib/ws/src/websocket.rs index 78ec77d6..6557a293 100644 --- a/contrib/ws/src/websocket.rs +++ b/contrib/ws/src/websocket.rs @@ -155,6 +155,38 @@ impl WebSocket { { MessageStream { ws: self, handler: Box::new(stream), } } + + /// Returns the server's fully computed and encoded WebSocket handshake + /// accept key. + /// + /// > The server takes the value of the `Sec-WebSocket-Key` sent in the + /// > handshake request, appends `258EAFA5-E914-47DA-95CA-C5AB0DC85B11`, + /// > SHA-1 of the new value, and is then base64 encoded. + /// > + /// > -- [`Sec-WebSocket-Accept`] + /// + /// This is the value returned via the [`Sec-WebSocket-Accept`] header + /// during the acceptance response. + /// + /// [`Sec-WebSocket-Accept`]: + /// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-WebSocket-Accept + /// + /// # Example + /// + /// ```rust + /// # use rocket::get; + /// # use rocket_ws as ws; + /// # + /// #[get("/echo")] + /// fn echo_stream(ws: ws::WebSocket) -> ws::Stream!['static] { + /// let accept_key = ws.accept_key(); + /// ws.stream(|io| io) + /// } + /// ``` + pub fn accept_key(&self) -> &str { + &self.key + } + } /// A streaming channel, returned by [`WebSocket::channel()`].