From 7869f11b4155d32bc9aa4872baf62373cb19f342 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Sat, 17 Jun 2023 23:36:46 +0100 Subject: [PATCH 1/3] Add assert panics for WebSocketConfig --- src/protocol/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index db32586..78112fa 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -81,6 +81,17 @@ impl Default for WebSocketConfig { } } +impl WebSocketConfig { + /// Panic if values are invalid. + pub(crate) fn assert_valid(&self) { + assert!( + self.max_write_buffer_size > self.write_buffer_size, + "WebSocketConfig::max_write_buffer_size must be greater than write_buffer_size, \ + see WebSocketConfig docs`" + ); + } +} + /// WebSocket input-output stream. /// /// This is THE structure you want to create to be able to speak the WebSocket protocol. @@ -301,6 +312,7 @@ impl WebSocketContext { } fn _new(role: Role, mut frame: FrameCodec, config: WebSocketConfig) -> Self { + config.assert_valid(); frame.set_max_out_buffer_len(config.max_write_buffer_size); frame.set_out_buffer_write_len(config.write_buffer_size); Self { @@ -316,6 +328,7 @@ impl WebSocketContext { /// Change the configuration. pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig)) { set_func(&mut self.config); + self.config.assert_valid(); self.frame.set_max_out_buffer_len(self.config.max_write_buffer_size); self.frame.set_out_buffer_write_len(self.config.write_buffer_size); } From 9567cc73f3e16dbe17404b88d68ba9d22836f116 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Sat, 17 Jun 2023 23:46:04 +0100 Subject: [PATCH 2/3] Add panics docs --- src/protocol/mod.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 78112fa..5980de1 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -112,6 +112,9 @@ impl WebSocket { /// Call this function if you're using Tungstenite as a part of a web framework /// or together with an existing one. If you need an initial handshake, use /// `connect()` or `accept()` functions of the crate to construct a websocket. + /// + /// # Panics + /// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`. pub fn from_raw_socket(stream: Stream, role: Role, config: Option) -> Self { WebSocket { socket: stream, context: WebSocketContext::new(role, config) } } @@ -121,6 +124,9 @@ impl WebSocket { /// Call this function if you're using Tungstenite as a part of a web framework /// or together with an existing one. If you need an initial handshake, use /// `connect()` or `accept()` functions of the crate to construct a websocket. + /// + /// # Panics + /// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`. pub fn from_partially_read( stream: Stream, part: Vec, @@ -143,6 +149,9 @@ impl WebSocket { } /// Change the configuration. + /// + /// # Panics + /// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`. pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig)) { self.context.set_config(set_func) } @@ -302,11 +311,17 @@ pub struct WebSocketContext { impl WebSocketContext { /// Create a WebSocket context that manages a post-handshake stream. + /// + /// # Panics + /// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`. pub fn new(role: Role, config: Option) -> Self { Self::_new(role, FrameCodec::new(), config.unwrap_or_default()) } /// Create a WebSocket context that manages an post-handshake stream. + /// + /// # Panics + /// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`. pub fn from_partially_read(part: Vec, role: Role, config: Option) -> Self { Self::_new(role, FrameCodec::from_partially_read(part), config.unwrap_or_default()) } @@ -326,6 +341,9 @@ impl WebSocketContext { } /// Change the configuration. + /// + /// # Panics + /// Panics if config is invalid e.g. `max_write_buffer_size <= write_buffer_size`. pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig)) { set_func(&mut self.config); self.config.assert_valid(); From 8f73cf03ab56f9a36d7a89154359d8fbc565d841 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Sat, 17 Jun 2023 23:50:18 +0100 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36a1893..c010e23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Note: `WriteBufferFull` returns the message that could not be written as a `Message::Frame`. - Add ability to buffer multiple writes before writing to the underlying stream, controlled by `WebSocketConfig::write_buffer_size` (default 128 KiB). Improves batch message write performance. +- Panic on receiving invalid `WebSocketConfig`. # 0.19.0