xmpp-parsers: Reduce component handshake allocations
This function always appends to an existing string, so by passing it a String directly we can avoid superfluous allocations. Also tokio-xmpp was doing a bunch of &str to String for no reason around there, let’s remove that too.
This commit is contained in:
parent
b8f64037a9
commit
c497967b1c
4 changed files with 14 additions and 10 deletions
|
|
@ -54,6 +54,10 @@ XXXX-YY-ZZ RELEASER <admin@example.com>
|
||||||
- pubsub::Event is now the wrapper for the pubsub::event::Payload enum,
|
- pubsub::Event is now the wrapper for the pubsub::event::Payload enum,
|
||||||
and the PublishedItems and RetractedItems have been merged into the
|
and the PublishedItems and RetractedItems have been merged into the
|
||||||
Items sub-struct. These replace the previous PubSubEvent enum (!531)
|
Items sub-struct. These replace the previous PubSubEvent enum (!531)
|
||||||
|
- Handshake::from_password_and_stream_id() became
|
||||||
|
Handshake::from_stream_id_and_password(), with the two parameters
|
||||||
|
having been exchanged, and the stream_id is now a String instead of
|
||||||
|
&str.
|
||||||
* New parsers/serialisers:
|
* New parsers/serialisers:
|
||||||
- Stream Features (RFC 6120) (!400)
|
- Stream Features (RFC 6120) (!400)
|
||||||
- Spam Reporting (XEP-0377) (!506)
|
- Spam Reporting (XEP-0377) (!506)
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,8 @@ impl Handshake {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an authentication request from the component.
|
/// Creates an authentication request from the component.
|
||||||
pub fn from_password_and_stream_id(password: &str, stream_id: &str) -> Handshake {
|
pub fn from_stream_id_and_password(stream_id: String, password: &str) -> Handshake {
|
||||||
let input = String::from(stream_id) + password;
|
let input = stream_id + password;
|
||||||
let hash = Sha1::digest(input.as_bytes());
|
let hash = Sha1::digest(input.as_bytes());
|
||||||
Handshake {
|
Handshake {
|
||||||
data: Some(hash.into()),
|
data: Some(hash.into()),
|
||||||
|
|
@ -76,7 +76,9 @@ mod tests {
|
||||||
let handshake = Handshake::new();
|
let handshake = Handshake::new();
|
||||||
assert_eq!(handshake.data, None);
|
assert_eq!(handshake.data, None);
|
||||||
|
|
||||||
let handshake = Handshake::from_password_and_stream_id("123456", "sid");
|
let stream_id = String::from("sid");
|
||||||
|
let password = "123456";
|
||||||
|
let handshake = Handshake::from_stream_id_and_password(stream_id, password);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
handshake.data,
|
handshake.data,
|
||||||
Some([
|
Some([
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,14 @@ use crate::xmlstream::{ReadError, Timeouts, XmppStream, XmppStreamElement};
|
||||||
pub async fn component_login<C: ServerConnector>(
|
pub async fn component_login<C: ServerConnector>(
|
||||||
connector: C,
|
connector: C,
|
||||||
jid: Jid,
|
jid: Jid,
|
||||||
password: String,
|
password: &str,
|
||||||
timeouts: Timeouts,
|
timeouts: Timeouts,
|
||||||
) -> Result<XmppStream<C::Stream>, Error> {
|
) -> Result<XmppStream<C::Stream>, Error> {
|
||||||
let password = password;
|
|
||||||
let (mut stream, _) = connector.connect(&jid, ns::COMPONENT, timeouts).await?;
|
let (mut stream, _) = connector.connect(&jid, ns::COMPONENT, timeouts).await?;
|
||||||
let header = stream.take_header();
|
let header = stream.take_header();
|
||||||
let mut stream = stream.skip_features();
|
let mut stream = stream.skip_features();
|
||||||
let stream_id = match header.id {
|
let stream_id = match header.id {
|
||||||
Some(ref v) => &**v,
|
Some(id) => id.into_owned(),
|
||||||
None => {
|
None => {
|
||||||
return Err(io::Error::new(
|
return Err(io::Error::new(
|
||||||
io::ErrorKind::InvalidData,
|
io::ErrorKind::InvalidData,
|
||||||
|
|
@ -29,16 +28,16 @@ pub async fn component_login<C: ServerConnector>(
|
||||||
.into())
|
.into())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
auth(&mut stream, stream_id, &password).await?;
|
auth(&mut stream, stream_id, password).await?;
|
||||||
Ok(stream)
|
Ok(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn auth<S: AsyncBufRead + AsyncWrite + Unpin>(
|
pub async fn auth<S: AsyncBufRead + AsyncWrite + Unpin>(
|
||||||
stream: &mut XmppStream<S>,
|
stream: &mut XmppStream<S>,
|
||||||
stream_id: &str,
|
stream_id: String,
|
||||||
password: &str,
|
password: &str,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let nonza = Handshake::from_password_and_stream_id(password, stream_id);
|
let nonza = Handshake::from_stream_id_and_password(stream_id, password);
|
||||||
stream
|
stream
|
||||||
.send(&XmppStreamElement::ComponentHandshake(nonza))
|
.send(&XmppStreamElement::ComponentHandshake(nonza))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,6 @@ impl<C: ServerConnector> Component<C> {
|
||||||
timeouts: Timeouts,
|
timeouts: Timeouts,
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self, Error> {
|
||||||
let jid = Jid::from_str(jid)?;
|
let jid = Jid::from_str(jid)?;
|
||||||
let password = password.to_owned();
|
|
||||||
let stream = component_login(connector, jid.clone(), password, timeouts).await?;
|
let stream = component_login(connector, jid.clone(), password, timeouts).await?;
|
||||||
Ok(Component { jid, stream })
|
Ok(Component { jid, stream })
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue