From 8acbcaea62d09bf1e4f70701d6369c594f417770 Mon Sep 17 00:00:00 2001 From: Aode Date: Mon, 17 Feb 2020 16:38:13 -0600 Subject: [PATCH 1/2] Make use of body inspection rather than body mapping --- .../src/digest/mod.rs | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/http-signature-normalization-warp/src/digest/mod.rs b/http-signature-normalization-warp/src/digest/mod.rs index 78bd678..30c889d 100644 --- a/http-signature-normalization-warp/src/digest/mod.rs +++ b/http-signature-normalization-warp/src/digest/mod.rs @@ -1,4 +1,4 @@ -use bytes::Buf; +use bytes::BytesMut; use std::str::FromStr; use warp::{header, Filter, Rejection}; @@ -36,31 +36,30 @@ pub enum ParseDigestError { #[error("Could not parse request body")] pub struct ParseBodyError; -pub fn verify( +pub fn verify( verifier: impl DigestVerify + Clone + Send + Sync + 'static, - filter: F, -) -> impl Filter + Clone -where - F: Filter + Clone + Send + Sync + 'static, - F::Extract: warp::Reply, - F::Error: Into, -{ - filter.with( - warp::body::map_request_body(parse_digest_header(), move |body: hyper::body::Body, parts: (Vec,)| { +) -> impl Filter + Clone { + parse_digest_header() + .and(warp::body::inspect_request_body( + BytesMut::new(), + move |mut acc, bytes| { + let bytes = bytes.clone(); + async move { + acc.extend_from_slice(&bytes); + acc + } + }, + )) + .and_then(move |parts: Vec, bytes_mut: BytesMut| { let mut verifier = verifier.clone(); async move { - let parts = parts.clone(); - let buf = hyper::body::aggregate(body).await.ok()?; - let bytes: Vec = buf.bytes().to_owned(); - - if verifier.verify(&parts.0, &bytes) { - Some(bytes.into()) + if verifier.verify(&parts, &bytes_mut.freeze()) { + Ok(()) } else { - None + Err(warp::reject::custom(VerifyError)) } } }) - ) } fn parse_digest_header() -> impl Filter,), Error = Rejection> + Clone { From 3cc6a712780edab6652a2d7d83f85d6521851214 Mon Sep 17 00:00:00 2001 From: Aode Date: Mon, 17 Feb 2020 16:57:07 -0600 Subject: [PATCH 2/2] Remove unneeded clone --- http-signature-normalization-warp/src/digest/mod.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/http-signature-normalization-warp/src/digest/mod.rs b/http-signature-normalization-warp/src/digest/mod.rs index 30c889d..eeb07a5 100644 --- a/http-signature-normalization-warp/src/digest/mod.rs +++ b/http-signature-normalization-warp/src/digest/mod.rs @@ -43,11 +43,8 @@ pub fn verify( .and(warp::body::inspect_request_body( BytesMut::new(), move |mut acc, bytes| { - let bytes = bytes.clone(); - async move { - acc.extend_from_slice(&bytes); - acc - } + acc.extend_from_slice(bytes); + async move { acc } }, )) .and_then(move |parts: Vec, bytes_mut: BytesMut| {