From 5ad8cef6ffb7edc372343717b6f86be16805e08c Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Tue, 19 Oct 2021 22:40:21 +0200 Subject: [PATCH] Echo the `CloseFrame` when close is initiated --- src/protocol/mod.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 215b0618..0586dcdb 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -555,23 +555,20 @@ impl WebSocketContext { debug!("Received close frame: {:?}", close); match self.state { WebSocketState::Active => { - let close_code = close.as_ref().map(|f| f.code); self.state = WebSocketState::ClosedByPeer; - let reply = if let Some(code) = close_code { - if code.is_allowed() { - Frame::close(Some(CloseFrame { - code: CloseCode::Normal, - reason: "".into(), - })) - } else { - Frame::close(Some(CloseFrame { + + let close = close.map(|frame| { + if !frame.code.is_allowed() { + CloseFrame { code: CloseCode::Protocol, reason: "Protocol violation".into(), - })) + } + } else { + frame } - } else { - Frame::close(None) - }; + }); + + let reply = Frame::close(close.clone()); debug!("Replying to close with {:?}", reply); self.send_queue.push_back(reply);