From e8b218e316a2b349c818fa22263567a89cd6c2c8 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Mon, 20 Feb 2023 23:00:23 -0500 Subject: [PATCH] Add peek_bytes --- src/common/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/common/mod.rs b/src/common/mod.rs index 229812d..33e8e18 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -41,7 +41,7 @@ pub fn c2s(is_c2s: bool) -> &'static str { } } -pub async fn first_bytes_match(stream: &tokio::net::TcpStream, p: &mut [u8], matcher: fn(&[u8]) -> bool) -> anyhow::Result { +pub async fn peek_bytes<'a>(stream: &tokio::net::TcpStream, p: &'a mut [u8]) -> anyhow::Result<&'a [u8]> { // sooo... I don't think peek here can be used for > 1 byte without this timer craziness... can it? let len = p.len(); // wait up to 10 seconds until len bytes have been read @@ -61,7 +61,11 @@ pub async fn first_bytes_match(stream: &tokio::net::TcpStream, p: &mut [u8], mat } } - Ok(matcher(p)) + Ok(p) +} + +pub async fn first_bytes_match(stream: &tokio::net::TcpStream, p: &mut [u8], matcher: fn(&[u8]) -> bool) -> anyhow::Result { + Ok(matcher(peek_bytes(stream, p).await?)) } pub async fn stream_preamble(in_rd: &mut StanzaRead, in_wr: &mut StanzaWrite, client_addr: &'_ str, in_filter: &mut StanzaFilter) -> Result<(Vec, bool)> {