2023-02-28 11:12:03 +00:00
|
|
|
use quick_xml::events::attributes::Attribute;
|
2023-03-05 21:04:28 +00:00
|
|
|
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
|
2023-02-28 11:12:03 +00:00
|
|
|
use quick_xml::name::{Namespace, QName, ResolveResult};
|
2023-03-05 21:04:28 +00:00
|
|
|
use quick_xml::{NsReader, Writer};
|
|
|
|
use tokio::io::{AsyncBufRead, AsyncWrite};
|
|
|
|
|
|
|
|
use super::skip_text;
|
2023-09-30 15:43:46 +00:00
|
|
|
|
|
|
|
use crate::xml::ToXml;
|
2024-03-20 18:59:15 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2023-02-17 21:27:58 +00:00
|
|
|
|
|
|
|
pub static XMLNS: &'static str = "http://etherx.jabber.org/streams";
|
2023-02-28 11:12:03 +00:00
|
|
|
pub static PREFIX: &'static str = "stream";
|
|
|
|
pub static XMLNS_XML: &'static str = "http://www.w3.org/XML/1998/namespace";
|
2023-02-17 21:27:58 +00:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub struct ClientStreamStart {
|
|
|
|
pub to: String,
|
2023-04-05 12:31:44 +00:00
|
|
|
pub lang: Option<String>,
|
2023-02-17 21:27:58 +00:00
|
|
|
pub version: String,
|
|
|
|
}
|
|
|
|
impl ClientStreamStart {
|
2023-02-28 11:12:03 +00:00
|
|
|
pub async fn parse(
|
|
|
|
reader: &mut NsReader<impl AsyncBufRead + Unpin>,
|
|
|
|
buf: &mut Vec<u8>,
|
|
|
|
) -> Result<ClientStreamStart> {
|
2024-04-04 17:49:03 +00:00
|
|
|
let mut incoming = skip_text!(reader, buf);
|
|
|
|
if let Event::Decl(bytes) = incoming {
|
|
|
|
// this is <?xml ...> header
|
|
|
|
if let Some(encoding) = bytes.encoding() {
|
|
|
|
let encoding = encoding?;
|
|
|
|
if &*encoding != b"UTF-8" {
|
|
|
|
return Err(anyhow!("Unsupported encoding: {encoding:?}"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
incoming = skip_text!(reader, buf);
|
|
|
|
}
|
2023-03-05 21:04:28 +00:00
|
|
|
if let Event::Start(e) = incoming {
|
2023-02-17 21:27:58 +00:00
|
|
|
let (ns, local) = reader.resolve_element(e.name());
|
2023-02-28 11:12:03 +00:00
|
|
|
if ns != ResolveResult::Bound(Namespace(XMLNS.as_bytes())) {
|
2024-01-22 15:13:19 +00:00
|
|
|
return Err(anyhow!("Invalid namespace for stream element"));
|
2023-02-17 21:27:58 +00:00
|
|
|
}
|
|
|
|
if local.into_inner() != b"stream" {
|
2024-01-22 15:13:19 +00:00
|
|
|
return Err(anyhow!("Invalid local name for stream element"));
|
2023-02-17 21:27:58 +00:00
|
|
|
}
|
|
|
|
let mut to = None;
|
|
|
|
let mut lang = None;
|
|
|
|
let mut version = None;
|
|
|
|
for attr in e.attributes() {
|
|
|
|
let attr = attr?;
|
|
|
|
let (ns, name) = reader.resolve_attribute(attr.key);
|
|
|
|
match (ns, name.into_inner()) {
|
|
|
|
(ResolveResult::Unbound, b"to") => {
|
|
|
|
let value = attr.unescape_value()?;
|
|
|
|
to = Some(value.to_string());
|
2023-02-28 11:12:03 +00:00
|
|
|
}
|
2024-03-20 18:59:15 +00:00
|
|
|
(ResolveResult::Bound(Namespace(b"http://www.w3.org/XML/1998/namespace")), b"lang") => {
|
2023-02-17 21:27:58 +00:00
|
|
|
let value = attr.unescape_value()?;
|
|
|
|
lang = Some(value.to_string());
|
2023-02-28 11:12:03 +00:00
|
|
|
}
|
2023-02-17 21:27:58 +00:00
|
|
|
(ResolveResult::Unbound, b"version") => {
|
|
|
|
let value = attr.unescape_value()?;
|
|
|
|
version = Some(value.to_string());
|
2023-02-28 11:12:03 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
2023-02-17 21:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-28 11:12:03 +00:00
|
|
|
Ok(ClientStreamStart {
|
|
|
|
to: to.unwrap(),
|
2023-04-05 12:31:44 +00:00
|
|
|
lang: lang,
|
2023-02-28 11:12:03 +00:00
|
|
|
version: version.unwrap(),
|
|
|
|
})
|
2023-02-17 21:27:58 +00:00
|
|
|
} else {
|
2024-01-22 15:13:19 +00:00
|
|
|
Err(anyhow!("Incoming message does not belong XML Start Event"))
|
2023-02-17 21:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 11:12:03 +00:00
|
|
|
pub struct ServerStreamStart {
|
|
|
|
pub from: String,
|
|
|
|
pub lang: String,
|
2023-04-05 12:31:44 +00:00
|
|
|
pub id: String,
|
2023-02-28 11:12:03 +00:00
|
|
|
pub version: String,
|
|
|
|
}
|
|
|
|
impl ServerStreamStart {
|
2023-03-05 21:04:28 +00:00
|
|
|
pub async fn write_xml(&self, writer: &mut Writer<impl AsyncWrite + Unpin>) -> Result<()> {
|
2023-02-28 11:12:03 +00:00
|
|
|
let mut event = BytesStart::new("stream:stream");
|
|
|
|
let attributes = [
|
|
|
|
Attribute {
|
|
|
|
key: QName(b"from"),
|
|
|
|
value: self.from.as_bytes().into(),
|
|
|
|
},
|
|
|
|
Attribute {
|
|
|
|
key: QName(b"version"),
|
|
|
|
value: self.version.as_bytes().into(),
|
|
|
|
},
|
|
|
|
Attribute {
|
|
|
|
key: QName(b"xmlns"),
|
|
|
|
value: super::client::XMLNS.as_bytes().into(),
|
|
|
|
},
|
|
|
|
Attribute {
|
|
|
|
key: QName(b"xmlns:stream"),
|
|
|
|
value: XMLNS.as_bytes().into(),
|
|
|
|
},
|
|
|
|
Attribute {
|
|
|
|
key: QName(b"xml:lang"),
|
|
|
|
value: self.lang.as_bytes().into(),
|
|
|
|
},
|
2023-04-05 12:31:44 +00:00
|
|
|
Attribute {
|
|
|
|
key: QName(b"id"),
|
|
|
|
value: self.id.as_bytes().into(),
|
|
|
|
},
|
2023-02-28 11:12:03 +00:00
|
|
|
];
|
|
|
|
event.extend_attributes(attributes.into_iter());
|
2023-03-05 21:04:28 +00:00
|
|
|
writer.write_event_async(Event::Start(event)).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:57:35 +00:00
|
|
|
pub struct ServerStreamEnd;
|
|
|
|
impl ToXml for ServerStreamEnd {
|
|
|
|
fn serialize(&self, events: &mut Vec<Event<'static>>) {
|
|
|
|
events.push(Event::End(BytesEnd::new("stream:stream")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 21:04:28 +00:00
|
|
|
pub struct Features {
|
|
|
|
pub start_tls: bool,
|
|
|
|
pub mechanisms: bool,
|
|
|
|
pub bind: bool,
|
|
|
|
}
|
|
|
|
impl Features {
|
|
|
|
pub async fn write_xml(&self, writer: &mut Writer<impl AsyncWrite + Unpin>) -> Result<()> {
|
2024-03-20 18:59:15 +00:00
|
|
|
writer.write_event_async(Event::Start(BytesStart::new("stream:features"))).await?;
|
2023-03-05 21:04:28 +00:00
|
|
|
if self.start_tls {
|
|
|
|
writer
|
|
|
|
.write_event_async(Event::Start(BytesStart::new(
|
|
|
|
r#"starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls""#,
|
|
|
|
)))
|
|
|
|
.await?;
|
2024-03-20 18:59:15 +00:00
|
|
|
writer.write_event_async(Event::Empty(BytesStart::new("required"))).await?;
|
|
|
|
writer.write_event_async(Event::End(BytesEnd::new("starttls"))).await?;
|
2023-03-05 21:04:28 +00:00
|
|
|
}
|
|
|
|
if self.mechanisms {
|
|
|
|
writer
|
|
|
|
.write_event_async(Event::Start(BytesStart::new(
|
|
|
|
r#"mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl""#,
|
|
|
|
)))
|
|
|
|
.await?;
|
2024-03-20 18:59:15 +00:00
|
|
|
writer.write_event_async(Event::Start(BytesStart::new(r#"mechanism"#))).await?;
|
|
|
|
writer.write_event_async(Event::Text(BytesText::new("PLAIN"))).await?;
|
|
|
|
writer.write_event_async(Event::End(BytesEnd::new("mechanism"))).await?;
|
|
|
|
writer.write_event_async(Event::End(BytesEnd::new("mechanisms"))).await?;
|
2023-03-05 21:04:28 +00:00
|
|
|
}
|
|
|
|
if self.bind {
|
|
|
|
writer
|
|
|
|
.write_event_async(Event::Empty(BytesStart::new(
|
|
|
|
r#"bind xmlns="urn:ietf:params:xml:ns:xmpp-bind""#,
|
|
|
|
)))
|
|
|
|
.await?;
|
|
|
|
}
|
2024-03-20 18:59:15 +00:00
|
|
|
writer.write_event_async(Event::End(BytesEnd::new("stream:features"))).await?;
|
2023-03-05 21:04:28 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 21:27:58 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[tokio::test]
|
2023-02-28 11:12:03 +00:00
|
|
|
async fn client_stream_start_correct_parse() {
|
2024-05-05 15:12:58 +00:00
|
|
|
let input = r###"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="xmpp.ru" version="1.0" xmlns="jabber:client" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">"###;
|
2023-02-17 21:27:58 +00:00
|
|
|
let mut reader = NsReader::from_reader(input.as_bytes());
|
|
|
|
let mut buf = vec![];
|
2024-03-20 18:59:15 +00:00
|
|
|
let res = ClientStreamStart::parse(&mut reader, &mut buf).await.unwrap();
|
2023-02-28 11:12:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
res,
|
|
|
|
ClientStreamStart {
|
2024-05-05 15:12:58 +00:00
|
|
|
to: "xmpp.ru".to_owned(),
|
2023-04-05 12:31:44 +00:00
|
|
|
lang: Some("en".to_owned()),
|
2023-02-28 11:12:03 +00:00
|
|
|
version: "1.0".to_owned()
|
|
|
|
}
|
|
|
|
)
|
2023-02-17 21:27:58 +00:00
|
|
|
}
|
2023-02-28 11:12:03 +00:00
|
|
|
|
2023-03-05 21:04:28 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn server_stream_start_write() {
|
2023-02-28 11:12:03 +00:00
|
|
|
let input = ServerStreamStart {
|
2024-05-05 15:12:58 +00:00
|
|
|
from: "xmpp.ru".to_owned(),
|
2023-02-28 11:12:03 +00:00
|
|
|
lang: "en".to_owned(),
|
2023-04-05 12:31:44 +00:00
|
|
|
id: "stream_id".to_owned(),
|
2023-02-28 11:12:03 +00:00
|
|
|
version: "1.0".to_owned(),
|
|
|
|
};
|
2024-05-05 15:12:58 +00:00
|
|
|
let expected = r###"<stream:stream from="xmpp.ru" version="1.0" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" xml:lang="en" id="stream_id">"###;
|
2023-02-28 11:12:03 +00:00
|
|
|
let mut output: Vec<u8> = vec![];
|
|
|
|
let mut writer = Writer::new(&mut output);
|
2023-03-05 21:04:28 +00:00
|
|
|
input.write_xml(&mut writer).await.unwrap();
|
2023-02-28 11:12:03 +00:00
|
|
|
assert_eq!(std::str::from_utf8(&output).unwrap(), expected);
|
|
|
|
}
|
|
|
|
}
|