From fb8329a187a682e9ed5aa71243411e75104b97a3 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Wed, 5 Apr 2023 14:31:44 +0200 Subject: [PATCH] feat(xmpp): add stream id --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/projections/xmpp/mod.rs | 3 +++ src/protos/xmpp/stream.rs | 14 ++++++++++---- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1169f6a..73d098f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -462,6 +462,7 @@ dependencies = [ "tokio-tungstenite", "tracing", "tracing-subscriber", + "uuid", ] [[package]] @@ -1249,6 +1250,15 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "uuid" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1674845326ee10d37ca60470760d4288a6f80f304007d92e5c53bab78c9cfd79" +dependencies = [ + "getrandom", +] + [[package]] name = "valuable" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 383b56a..1e9b641 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ tokio-rustls = "0.23.4" rustls-pemfile = "1.0.2" quick-xml = { version = "0.28.1", features = ["async-tokio"] } derive_more = "0.99.17" +uuid = { version = "1.3.0", features = ["v4"] } [dev-dependencies] assert_matches = "1.5.0" diff --git a/src/projections/xmpp/mod.rs b/src/projections/xmpp/mod.rs index e92a816..6dde107 100644 --- a/src/projections/xmpp/mod.rs +++ b/src/projections/xmpp/mod.rs @@ -194,6 +194,7 @@ async fn socket_force_tls( let msg = ServerStreamStart { from: "localhost".into(), lang: "en".into(), + id: uuid::Uuid::new_v4().to_string(), version: "1.0".into(), }; msg.write_xml(xml_writer).await?; @@ -225,6 +226,7 @@ async fn socket_auth( ServerStreamStart { from: "localhost".into(), lang: "en".into(), + id: uuid::Uuid::new_v4().to_string(), version: "1.0".into(), } .write_xml(xml_writer) @@ -257,6 +259,7 @@ async fn socket_final( ServerStreamStart { from: "localhost".into(), lang: "en".into(), + id: uuid::Uuid::new_v4().to_string(), version: "1.0".into(), } .write_xml(xml_writer) diff --git a/src/protos/xmpp/stream.rs b/src/protos/xmpp/stream.rs index 7da2a3a..a96bad2 100644 --- a/src/protos/xmpp/stream.rs +++ b/src/protos/xmpp/stream.rs @@ -14,7 +14,7 @@ pub static XMLNS_XML: &'static str = "http://www.w3.org/XML/1998/namespace"; #[derive(Debug, PartialEq, Eq)] pub struct ClientStreamStart { pub to: String, - pub lang: String, + pub lang: Option, pub version: String, } impl ClientStreamStart { @@ -58,7 +58,7 @@ impl ClientStreamStart { } Ok(ClientStreamStart { to: to.unwrap(), - lang: lang.unwrap(), + lang: lang, version: version.unwrap(), }) } else { @@ -71,6 +71,7 @@ impl ClientStreamStart { pub struct ServerStreamStart { pub from: String, pub lang: String, + pub id: String, pub version: String, } impl ServerStreamStart { @@ -97,6 +98,10 @@ impl ServerStreamStart { key: QName(b"xml:lang"), value: self.lang.as_bytes().into(), }, + Attribute { + key: QName(b"id"), + value: self.id.as_bytes().into(), + }, ]; event.extend_attributes(attributes.into_iter()); writer.write_event_async(Event::Start(event)).await?; @@ -176,7 +181,7 @@ mod test { res, ClientStreamStart { to: "vlnv.dev".to_owned(), - lang: "en".to_owned(), + lang: Some("en".to_owned()), version: "1.0".to_owned() } ) @@ -187,9 +192,10 @@ mod test { let input = ServerStreamStart { from: "vlnv.dev".to_owned(), lang: "en".to_owned(), + id: "stream_id".to_owned(), version: "1.0".to_owned(), }; - let expected = r###""###; + let expected = r###""###; let mut output: Vec = vec![]; let mut writer = Writer::new(&mut output); input.write_xml(&mut writer).await.unwrap();