From 2e97f4de2e44c64524bbe9990553627e7a47f805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 8 Sep 2019 15:05:57 +0200 Subject: [PATCH] Fixes #11: Trim whitespace before feeding parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/xmpp_codec.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/xmpp_codec.rs b/src/xmpp_codec.rs index 0002087..654cc21 100644 --- a/src/xmpp_codec.rs +++ b/src/xmpp_codec.rs @@ -229,7 +229,8 @@ impl Decoder for XMPPCodec { }; let buf1 = buf1.as_ref().as_ref(); match from_utf8(buf1) { - Ok(s) => { + Ok(mut s) => { + s = s.trim(); if !s.is_empty() { // println!("<< {}", s); let mut buffer_queue = BufferQueue::new(); @@ -508,4 +509,24 @@ mod tests { &("".to_owned() + &text + "").as_bytes() ); } + + #[test] + fn test_lone_whitespace() { + let mut c = XMPPCodec::new(); + let mut b = BytesMut::with_capacity(1024); + b.put(r""); + let r = c.decode(&mut b); + assert!(match r { + Ok(Some(Packet::StreamStart(_))) => true, + _ => false, + }); + + b.clear(); + b.put(r" "); + let r = c.decode(&mut b); + assert!(match r { + Ok(None) => true, + _ => false, + }); + } }