From 208e280067f075daf1d061b78992fa8611342386 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 20 Feb 2018 16:20:45 +0100 Subject: [PATCH] Add a WebSocket parser. --- src/lib.rs | 3 +++ src/ns.rs | 3 +++ src/websocket.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/websocket.rs diff --git a/src/lib.rs b/src/lib.rs index 415572c0f4092d811e57ae196363a9714a532da1..36b4b3e73fe1d91036a5a4926a9e77c7a8934ad8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,6 +59,9 @@ pub mod stanza_error; /// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence pub mod roster; +/// RFC 7395: An Extensible Messaging and Presence Protocol (XMPP) Subprotocol for WebSocket +pub mod websocket; + /// XEP-0004: Data Forms pub mod data_forms; diff --git a/src/ns.rs b/src/ns.rs index b8a10bca58e0eca69cc60c3445c392dace9b7662..dcc3946c95b276ca17d3aba8c55061a48e7ac144 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -13,6 +13,9 @@ pub const XMPP_STANZAS: &str = "urn:ietf:params:xml:ns:xmpp-stanzas"; /// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence pub const ROSTER: &str = "jabber:iq:roster"; +/// RFC 7395: An Extensible Messaging and Presence Protocol (XMPP) Subprotocol for WebSocket +pub const WEBSOCKET: &str = "urn:ietf:params:xml:ns:xmpp-framing"; + /// XEP-0004: Data Forms pub const DATA_FORMS: &str = "jabber:x:data"; diff --git a/src/websocket.rs b/src/websocket.rs new file mode 100644 index 0000000000000000000000000000000000000000..163ce3ef4ae91b2699a90b60c1d069f7df470584 --- /dev/null +++ b/src/websocket.rs @@ -0,0 +1,70 @@ +// Copyright (c) 2018 Emmanuel Gil Peyrot +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +use try_from::TryFrom; + +use minidom::Element; +use jid::Jid; +use error::Error; +use ns; + +generate_element_with_only_attributes!(Open, "open", ns::WEBSOCKET, [ + from: Option = "from" => optional, + to: Option = "to" => optional, + id: Option = "id" => optional, + version: Option = "version" => optional, + xml_lang: Option = "xml:lang" => optional, +]); + +impl Open { + pub fn new(to: Jid) -> Open { + Open { + from: None, + to: Some(to), + id: None, + version: Some(String::from("1.0")), + xml_lang: None, + } + } + + pub fn with_from(mut self, from: Jid) -> Open { + self.from = Some(from); + self + } + + pub fn with_id(mut self, id: String) -> Open { + self.id = Some(id); + self + } + + pub fn with_lang(mut self, xml_lang: String) -> Open { + self.xml_lang = Some(xml_lang); + self + } + + pub fn is_version(&self, version: &str) -> bool { + match self.version { + None => false, + Some(ref self_version) => self_version == &String::from(version), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_simple() { + let elem: Element = "".parse().unwrap(); + let open = Open::try_from(elem).unwrap(); + assert_eq!(open.from, None); + assert_eq!(open.to, None); + assert_eq!(open.id, None); + assert_eq!(open.version, None); + assert_eq!(open.xml_lang, None); + } +}