transport: Adapt to quick_xml. Thanks eijebong.

This commit is contained in:
Maxime “pep” Buquet 2017-12-28 05:31:36 +01:00
parent 4b322cc62b
commit 37148b9097
2 changed files with 65 additions and 66 deletions

View file

@ -5,6 +5,7 @@ use std::fmt::Error as FormatError;
use std::io; use std::io;
use std::net::TcpStream; use std::net::TcpStream;
use std::str::Utf8Error;
use openssl::ssl::HandshakeError; use openssl::ssl::HandshakeError;
use openssl::error::ErrorStack; use openssl::error::ErrorStack;
@ -29,6 +30,7 @@ pub enum Error {
SaslError(Option<String>), SaslError(Option<String>),
XmppSaslError(SaslError), XmppSaslError(SaslError),
FormatError(FormatError), FormatError(FormatError),
Utf8Error(Utf8Error),
StreamError, StreamError,
EndOfDocument, EndOfDocument,
} }
@ -74,3 +76,9 @@ impl From<FormatError> for Error {
Error::FormatError(err) Error::FormatError(err)
} }
} }
impl From<Utf8Error> for Error {
fn from(err: Utf8Error) -> Error {
Error::Utf8Error(err)
}
}

View file

@ -1,11 +1,13 @@
//! Provides transports for the xml streams. //! Provides transports for the xml streams.
use std::io::BufReader;
use std::io::prelude::*; use std::io::prelude::*;
use std::str;
use std::net::{TcpStream, Shutdown}; use std::net::{TcpStream, Shutdown};
use xml::reader::{EventReader, XmlEvent as XmlReaderEvent}; use quick_xml::reader::{Reader as EventReader};
use xml::writer::{EventWriter, XmlEvent as XmlWriterEvent, EmitterConfig}; use quick_xml::events::Event;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -24,11 +26,11 @@ use sasl::common::ChannelBinding;
/// A trait which transports are required to implement. /// A trait which transports are required to implement.
pub trait Transport { pub trait Transport {
/// Writes an `xml::writer::XmlEvent` to the stream. /// Writes a `quick_xml::events::Event` to the stream.
fn write_event<'a, E: Into<XmlWriterEvent<'a>>>(&mut self, event: E) -> Result<(), Error>; fn write_event<'a, E: Into<Event<'a>>>(&mut self, event: E) -> Result<usize, Error>;
/// Reads an `xml::reader::XmlEvent` from the stream. /// Reads a `quick_xml::events::Event` from the stream.
fn read_event(&mut self) -> Result<XmlReaderEvent, Error>; fn read_event(&mut self) -> Result<Event, Error>;
/// Writes a `minidom::Element` to the stream. /// Writes a `minidom::Element` to the stream.
fn write_element(&mut self, element: &minidom::Element) -> Result<(), Error>; fn write_element(&mut self, element: &minidom::Element) -> Result<(), Error>;
@ -48,19 +50,20 @@ pub trait Transport {
/// A plain text transport, completely unencrypted. /// A plain text transport, completely unencrypted.
pub struct PlainTransport { pub struct PlainTransport {
inner: Arc<Mutex<TcpStream>>, // TODO: this feels rather ugly inner: Arc<Mutex<TcpStream>>, // TODO: this feels rather ugly
reader: EventReader<LockedIO<TcpStream>>, // TODO: especially feels ugly because // TODO: especially feels ugly because this read would keep the lock held very long
// this read would keep the lock // (potentially)
// held very long (potentially) reader: EventReader<BufReader<LockedIO<TcpStream>>>,
writer: EventWriter<LockedIO<TcpStream>>, writer: LockedIO<TcpStream>,
buf: Vec<u8>,
} }
impl Transport for PlainTransport { impl Transport for PlainTransport {
fn write_event<'a, E: Into<XmlWriterEvent<'a>>>(&mut self, event: E) -> Result<(), Error> { fn write_event<'a, E: Into<Event<'a>>>(&mut self, event: E) -> Result<usize, Error> {
Ok(self.writer.write(event)?) Ok(self.writer.write(&event.into())?)
} }
fn read_event(&mut self) -> Result<XmlReaderEvent, Error> { fn read_event(&mut self) -> Result<Event, Error> {
Ok(self.reader.next()?) Ok(self.reader.read_event(&mut self.buf)?)
} }
fn write_element(&mut self, element: &minidom::Element) -> Result<(), Error> { fn write_element(&mut self, element: &minidom::Element) -> Result<(), Error> {
@ -74,13 +77,8 @@ impl Transport for PlainTransport {
fn reset_stream(&mut self) { fn reset_stream(&mut self) {
let locked_io = LockedIO::from(self.inner.clone()); let locked_io = LockedIO::from(self.inner.clone());
self.reader = EventReader::new(locked_io.clone()); self.reader = EventReader::from_reader(BufReader::new(locked_io.clone()));
self.writer = EventWriter::new_with_config(locked_io, EmitterConfig { self.writer = locked_io;
line_separator: "".into(),
perform_indent: false,
normalize_empty_elements: false,
.. Default::default()
});
} }
fn channel_bind(&self) -> ChannelBinding { fn channel_bind(&self) -> ChannelBinding {
@ -93,21 +91,16 @@ impl PlainTransport {
/// Connects to a server without any encryption. /// Connects to a server without any encryption.
pub fn connect(host: &str, port: u16) -> Result<PlainTransport, Error> { pub fn connect(host: &str, port: u16) -> Result<PlainTransport, Error> {
let tcp_stream = TcpStream::connect((host, port))?; let tcp_stream = TcpStream::connect((host, port))?;
let parser = EventReader::new(tcp_stream); let stream = Arc::new(Mutex::new(tcp_stream));
let parser_stream = parser.into_inner();
let stream = Arc::new(Mutex::new(parser_stream));
let locked_io = LockedIO::from(stream.clone()); let locked_io = LockedIO::from(stream.clone());
let reader = EventReader::new(locked_io.clone()); let reader = EventReader::from_reader(BufReader::new(locked_io.clone()));
let writer = EventWriter::new_with_config(locked_io, EmitterConfig { let writer = locked_io;
line_separator: "".into(),
perform_indent: false,
normalize_empty_elements: false,
.. Default::default()
});
Ok(PlainTransport { Ok(PlainTransport {
inner: stream, inner: stream,
reader: reader, reader: reader,
writer: writer, writer: writer,
buf: Vec::new(),
}) })
} }
@ -123,19 +116,20 @@ impl PlainTransport {
/// A transport which uses STARTTLS. /// A transport which uses STARTTLS.
pub struct SslTransport { pub struct SslTransport {
inner: Arc<Mutex<SslStream<TcpStream>>>, // TODO: this feels rather ugly inner: Arc<Mutex<SslStream<TcpStream>>>, // TODO: this feels rather ugly
reader: EventReader<LockedIO<SslStream<TcpStream>>>, // TODO: especially feels ugly because // TODO: especially feels ugly because this read would keep the lock held very long
// this read would keep the lock // (potentially)
// held very long (potentially) reader: EventReader<BufReader<LockedIO<SslStream<TcpStream>>>>,
writer: EventWriter<LockedIO<SslStream<TcpStream>>>, writer: LockedIO<SslStream<TcpStream>>,
buf: Vec<u8>,
} }
impl Transport for SslTransport { impl Transport for SslTransport {
fn write_event<'a, E: Into<XmlWriterEvent<'a>>>(&mut self, event: E) -> Result<(), Error> { fn write_event<'a, E: Into<Event<'a>>>(&mut self, event: E) -> Result<usize, Error> {
Ok(self.writer.write(event)?) Ok(self.writer.write(&event.into())?)
} }
fn read_event(&mut self) -> Result<XmlReaderEvent, Error> { fn read_event(&mut self) -> Result<Event, Error> {
Ok(self.reader.next()?) Ok(self.reader.read_event(&mut self.buf)?)
} }
fn write_element(&mut self, element: &minidom::Element) -> Result<(), Error> { fn write_element(&mut self, element: &minidom::Element) -> Result<(), Error> {
@ -148,13 +142,8 @@ impl Transport for SslTransport {
fn reset_stream(&mut self) { fn reset_stream(&mut self) {
let locked_io = LockedIO::from(self.inner.clone()); let locked_io = LockedIO::from(self.inner.clone());
self.reader = EventReader::new(locked_io.clone()); self.reader = EventReader::from_reader(BufReader::new(locked_io.clone()));
self.writer = EventWriter::new_with_config(locked_io, EmitterConfig { self.writer = locked_io;
line_separator: "".into(),
perform_indent: false,
normalize_empty_elements: false,
.. Default::default()
});
} }
fn channel_bind(&self) -> ChannelBinding { fn channel_bind(&self) -> ChannelBinding {
@ -172,23 +161,28 @@ impl SslTransport {
, ns::CLIENT, ns::STREAM, host)?; , ns::CLIENT, ns::STREAM, host)?;
write!(stream, "<starttls xmlns='{}'/>" write!(stream, "<starttls xmlns='{}'/>"
, ns::TLS)?; , ns::TLS)?;
let mut parser = EventReader::new(stream); {
let mut parser = EventReader::from_reader(BufReader::new(&stream));
let mut buf = Vec::new();
let ns_buf = Vec::new();
loop { // TODO: possibly a timeout? loop { // TODO: possibly a timeout?
match parser.next()? { match parser.read_event(&mut buf)? {
XmlReaderEvent::StartElement { name, .. } => { Event::Start(ref e) => {
if let Some(ns) = name.namespace { let (namespace, local_name) = parser.resolve_namespace(e.name(), &ns_buf);
if ns == ns::TLS && name.local_name == "proceed" { let namespace = namespace.map(str::from_utf8);
let local_name = str::from_utf8(local_name)?;
if let Some(ns) = namespace {
if ns == Ok(ns::TLS) && local_name == "proceed" {
break; break;
} } else if ns == Ok(ns::STREAM) && local_name == "error" {
else if ns == ns::STREAM && name.local_name == "error" {
return Err(Error::StreamError); return Err(Error::StreamError);
} }
} }
},
_ => {},
} }
} }
let stream = parser.into_inner(); }
}
#[cfg(feature = "insecure")] #[cfg(feature = "insecure")]
let ssl_stream = { let ssl_stream = {
let mut ctx = SslContextBuilder::new(SslMethod::tls())?; let mut ctx = SslContextBuilder::new(SslMethod::tls())?;
@ -203,17 +197,14 @@ impl SslTransport {
}; };
let ssl_stream = Arc::new(Mutex::new(ssl_stream)); let ssl_stream = Arc::new(Mutex::new(ssl_stream));
let locked_io = LockedIO::from(ssl_stream.clone()); let locked_io = LockedIO::from(ssl_stream.clone());
let reader = EventReader::new(locked_io.clone()); let reader = EventReader::from_reader(BufReader::new(locked_io.clone()));
let writer = EventWriter::new_with_config(locked_io, EmitterConfig { let writer = locked_io;
line_separator: "".into(),
perform_indent: false,
normalize_empty_elements: false,
.. Default::default()
});
Ok(SslTransport { Ok(SslTransport {
inner: ssl_stream, inner: ssl_stream,
reader: reader, reader: reader,
writer: writer, writer: writer,
buf: Vec::new(),
}) })
} }