Fixes #11: Trim whitespace before feeding parser

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2019-09-08 15:05:57 +02:00
parent d173254483
commit 2e97f4de2e
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -229,7 +229,8 @@ impl Decoder for XMPPCodec {
}; };
let buf1 = buf1.as_ref().as_ref(); let buf1 = buf1.as_ref().as_ref();
match from_utf8(buf1) { match from_utf8(buf1) {
Ok(s) => { Ok(mut s) => {
s = s.trim();
if !s.is_empty() { if !s.is_empty() {
// println!("<< {}", s); // println!("<< {}", s);
let mut buffer_queue = BufferQueue::new(); let mut buffer_queue = BufferQueue::new();
@ -508,4 +509,24 @@ mod tests {
&("<message><body>".to_owned() + &text + "</body></message>").as_bytes() &("<message><body>".to_owned() + &text + "</body></message>").as_bytes()
); );
} }
#[test]
fn test_lone_whitespace() {
let mut c = XMPPCodec::new();
let mut b = BytesMut::with_capacity(1024);
b.put(r"<?xml version='1.0'?><stream:stream xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xmlns='jabber:client'>");
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,
});
}
} }