forked from lavina/lavina
1
0
Fork 0
lavina/crates/proto-xmpp/src/tls.rs

39 lines
1.2 KiB
Rust

use quick_xml::events::attributes::Attribute;
use quick_xml::events::{BytesStart, Event};
use quick_xml::name::QName;
use quick_xml::{NsReader, Writer};
use tokio::io::{AsyncBufRead, AsyncWrite};
use super::skip_text;
use anyhow::{anyhow, Result};
pub static XMLNS: &'static str = "urn:ietf:params:xml:ns:xmpp-tls";
pub struct StartTLS;
impl StartTLS {
pub async fn parse(reader: &mut NsReader<impl AsyncBufRead + Unpin>, buf: &mut Vec<u8>) -> Result<StartTLS> {
let incoming = skip_text!(reader, buf);
if let Event::Empty(ref e) = incoming {
if e.name().0 == b"starttls" {
return Ok(StartTLS);
}
}
Err(anyhow!("XML tag starttls expected, received: {incoming:?}"))
}
}
pub struct ProceedTLS;
impl ProceedTLS {
pub async fn write_xml(&self, writer: &mut Writer<impl AsyncWrite + Unpin>) -> Result<()> {
let mut event = BytesStart::new("proceed");
let attributes = [Attribute {
key: QName(b"xmlns"),
value: XMLNS.as_bytes().into(),
}];
event.extend_attributes(attributes.into_iter());
writer.write_event_async(Event::Empty(event)).await?;
Ok(())
}
}