tokio_xmpp: properly process <stream:error/> while waiting for features
Before this, a stream error would not be readable by user code, as the `recv_features()` function would not even attempt to parse it. This change allows application code to react to stream errors which are received before stream features are received. skip-changelog, because there's no release with the xmlstream module yet.
This commit is contained in:
parent
af7e47a791
commit
e438813fb2
6 changed files with 157 additions and 17 deletions
|
|
@ -4,6 +4,7 @@
|
|||
// 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 core::fmt;
|
||||
use core::pin::Pin;
|
||||
use std::borrow::Cow;
|
||||
use std::io;
|
||||
|
|
@ -12,7 +13,10 @@ use futures::SinkExt;
|
|||
|
||||
use tokio::io::{AsyncBufRead, AsyncWrite};
|
||||
|
||||
use xmpp_parsers::stream_features::StreamFeatures;
|
||||
use xmpp_parsers::{
|
||||
stream_error::{ReceivedStreamError, StreamError},
|
||||
stream_features::StreamFeatures,
|
||||
};
|
||||
|
||||
use xso::{AsXml, FromXml};
|
||||
|
||||
|
|
@ -42,6 +46,49 @@ impl<Io: AsyncBufRead + AsyncWrite + Unpin> InitiatingStream<Io> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(xso::FromXml)]
|
||||
#[xml()]
|
||||
enum StreamFeaturesPayload {
|
||||
#[xml(transparent)]
|
||||
Features(StreamFeatures),
|
||||
#[xml(transparent)]
|
||||
Error(StreamError),
|
||||
}
|
||||
|
||||
/// Error conditions when receiving stream features
|
||||
#[derive(Debug)]
|
||||
pub enum RecvFeaturesError {
|
||||
/// I/o error while receiving stream features
|
||||
Io(io::Error),
|
||||
|
||||
/// Received a stream error instead of stream features
|
||||
StreamError(ReceivedStreamError),
|
||||
}
|
||||
|
||||
impl fmt::Display for RecvFeaturesError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Io(e) => write!(f, "i/o error: {e}"),
|
||||
Self::StreamError(e) => fmt::Display::fmt(&e, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl core::error::Error for RecvFeaturesError {
|
||||
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
|
||||
match self {
|
||||
Self::Io(e) => Some(e),
|
||||
Self::StreamError(e) => Some(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for RecvFeaturesError {
|
||||
fn from(other: io::Error) -> Self {
|
||||
Self::Io(other)
|
||||
}
|
||||
}
|
||||
|
||||
/// Type state for an initiator stream which has sent and received the stream
|
||||
/// header.
|
||||
///
|
||||
|
|
@ -73,26 +120,39 @@ impl<Io: AsyncBufRead + AsyncWrite + Unpin> PendingFeaturesRecv<Io> {
|
|||
/// After the stream features have been received, the stream can be used
|
||||
/// for exchanging stream-level elements (stanzas or "nonzas"). The Rust
|
||||
/// type for these elements must be given as type parameter `T`.
|
||||
///
|
||||
/// If the peer sends a stream error instead of features, the error is
|
||||
/// returned as [`RecvFeaturesError::StreamError`].
|
||||
///
|
||||
/// If the peer sends any payload which is neither stream features nor
|
||||
/// a stream error, an [`io::Error`][`std::io::Error`] with
|
||||
/// [`InvalidData`][`io::ErrorKind::InvalidData`] kind is returned.
|
||||
pub async fn recv_features<T: FromXml + AsXml>(
|
||||
self,
|
||||
) -> io::Result<(StreamFeatures, XmlStream<Io, T>)> {
|
||||
) -> Result<(StreamFeatures, XmlStream<Io, T>), RecvFeaturesError> {
|
||||
let Self {
|
||||
mut stream,
|
||||
header: _,
|
||||
} = self;
|
||||
let features = loop {
|
||||
match ReadXso::read_from(Pin::new(&mut stream)).await {
|
||||
Ok(v) => break v,
|
||||
Ok(StreamFeaturesPayload::Features(v)) => break v,
|
||||
Ok(StreamFeaturesPayload::Error(v)) => {
|
||||
return Err(RecvFeaturesError::StreamError(ReceivedStreamError(v)))
|
||||
}
|
||||
Err(ReadXsoError::SoftTimeout) => (),
|
||||
Err(ReadXsoError::Hard(e)) => return Err(e),
|
||||
Err(ReadXsoError::Hard(e)) => return Err(RecvFeaturesError::Io(e)),
|
||||
Err(ReadXsoError::Parse(e)) => {
|
||||
return Err(io::Error::new(io::ErrorKind::InvalidData, e))
|
||||
return Err(RecvFeaturesError::Io(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
e,
|
||||
)))
|
||||
}
|
||||
Err(ReadXsoError::Footer) => {
|
||||
return Err(io::Error::new(
|
||||
return Err(RecvFeaturesError::Io(io::Error::new(
|
||||
io::ErrorKind::UnexpectedEof,
|
||||
"unexpected stream footer",
|
||||
))
|
||||
)))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue