forked from lavina/lavina
1
0
Fork 0

xmpp: make xml-headers optional in the c2s stream

This commit is contained in:
Nikita Vilunov 2024-04-04 17:49:03 +00:00
parent d436631450
commit 1e2a6d5656
2 changed files with 14 additions and 29 deletions

View File

@ -229,7 +229,6 @@ async fn socket_force_tls(
use proto_xmpp::tls::*;
let xml_reader = &mut NsReader::from_reader(reader);
let xml_writer = &mut Writer::new(writer);
read_xml_header(xml_reader, reader_buf).await?;
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
let event = Event::Decl(BytesDecl::new("1.0", None, None));
@ -261,7 +260,6 @@ async fn socket_auth(
reader_buf: &mut Vec<u8>,
storage: &mut Storage,
) -> Result<Authenticated> {
read_xml_header(xml_reader, reader_buf).await?;
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
xml_writer.write_event_async(Event::Decl(BytesDecl::new("1.0", None, None))).await?;
@ -327,7 +325,6 @@ async fn socket_final(
user_handle: &mut PlayerConnection,
rooms: &RoomRegistry,
) -> Result<()> {
read_xml_header(xml_reader, reader_buf).await?;
let _ = ClientStreamStart::parse(xml_reader, reader_buf).await?;
xml_writer.write_event_async(Event::Decl(BytesDecl::new("1.0", None, None))).await?;
@ -419,7 +416,7 @@ struct XmppConnection<'a> {
impl<'a> XmppConnection<'a> {
async fn handle_packet(&mut self, output: &mut Vec<Event<'static>>, packet: ClientPacket) -> Result<bool> {
let res = match packet {
proto::ClientPacket::Iq(iq) => {
ClientPacket::Iq(iq) => {
self.handle_iq(output, iq).await;
false
}
@ -427,11 +424,11 @@ impl<'a> XmppConnection<'a> {
self.handle_message(output, m).await?;
false
}
proto::ClientPacket::Presence(p) => {
ClientPacket::Presence(p) => {
self.handle_presence(output, p).await?;
false
}
proto::ClientPacket::StreamEnd => {
ClientPacket::StreamEnd => {
ServerStreamEnd.serialize(output);
true
}
@ -439,25 +436,3 @@ impl<'a> XmppConnection<'a> {
Ok(res)
}
}
async fn read_xml_header(
xml_reader: &mut NsReader<(impl AsyncBufRead + Unpin)>,
reader_buf: &mut Vec<u8>,
) -> Result<()> {
if let Event::Decl(bytes) = xml_reader.read_event_into_async(reader_buf).await? {
// this is <?xml ...> header
if let Some(encoding) = bytes.encoding() {
let encoding = encoding?;
if &*encoding == b"UTF-8" {
Ok(())
} else {
Err(anyhow!("Unsupported encoding: {encoding:?}"))
}
} else {
// Err(fail("No XML encoding provided"))
Ok(())
}
} else {
Err(anyhow!("Expected XML header"))
}
}

View File

@ -24,7 +24,17 @@ impl ClientStreamStart {
reader: &mut NsReader<impl AsyncBufRead + Unpin>,
buf: &mut Vec<u8>,
) -> Result<ClientStreamStart> {
let incoming = skip_text!(reader, buf);
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);
}
if let Event::Start(e) = incoming {
let (ns, local) = reader.resolve_element(e.name());
if ns != ResolveResult::Bound(Namespace(XMLNS.as_bytes())) {