tokio-xmpp: Prevent XmppCodec from producing invalid stanza

Maxime “pep” Buquet created

This bug was introduced by 2e97f4de2e44c64524bbe9990553627e7a47f805, to
fix another bug where the parser would choke on whitespace.

The bug would manifest whenever a stanza was sent in different parts,
for example:
<< "<message "
<< "type='chat><body>foo</body></message>"

Would produce the following once parsed:
`<messagetype='chat'><body>foo</body></messagetype='chat'>`

This commit ensures this doesn't happen anymore (by not trimming
whitespaces before feeding the parser), and also ensures that
whitespaces are now handled at the correct layer.

The removal of xmpp_codec::test_lone_whitespace only happens because I'm
not sure if it's supposed to be here anymore. Maybe it should be at a
different layer? Or written differently?

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>

Change summary

tokio-xmpp/src/client/mod.rs |  1 +
tokio-xmpp/src/xmpp_codec.rs | 10 +++++-----
2 files changed, 6 insertions(+), 5 deletions(-)

Detailed changes

tokio-xmpp/src/client/mod.rs 🔗

@@ -177,6 +177,7 @@ impl Stream for Client {
                     }
                     Ok(Async::Ready(Some(Packet::Text(_)))) => {
                         // Ignore text between stanzas
+                        self.state = ClientState::Connected(stream);
                         Ok(Async::NotReady)
                     }
                     Ok(Async::Ready(Some(Packet::StreamStart(_)))) => {

tokio-xmpp/src/xmpp_codec.rs 🔗

@@ -229,9 +229,8 @@ impl Decoder for XMPPCodec {
         };
         let buf1 = buf1.as_ref().as_ref();
         match from_utf8(buf1) {
-            Ok(mut s) => {
+            Ok(s) => {
                 debug!("<< {:?}", s);
-                s = s.trim();
                 if !s.is_empty() {
                     let mut buffer_queue = BufferQueue::new();
                     let tendril = FromIterator::from_iter(s.chars());
@@ -503,7 +502,7 @@ mod tests {
     }
 
     #[test]
-    fn test_lone_whitespace() {
+    fn test_cut_out_stanza() {
         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'>");
@@ -514,10 +513,11 @@ mod tests {
         });
 
         b.clear();
-        b.put(r" ");
+        b.put(r"<message ");
+        b.put(r"type='chat'><body>Foo</body></message>");
         let r = c.decode(&mut b);
         assert!(match r {
-            Ok(None) => true,
+            Ok(Some(Packet::Stanza(_))) => true,
             _ => false,
         });
     }