chiark / gitweb /
read_limited_bytes: take an `initial` argument
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 Aug 2021 16:04:16 +0000 (17:04 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 15 Aug 2021 16:04:16 +0000 (17:04 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/client.rs
src/bin/server.rs
src/utils.rs

index 2497780c52a1e54c5707b7137426247535b37dd3..c3beab3d98f98aed67c31d8536446b4ec7230dd2 100644 (file)
@@ -140,7 +140,7 @@ fn submit_request<'r, 'c:'r, C:HCC>(
       let status = resp.status();
       let mut resp = resp.into_body();
       let max_body = c.ic.max_batch_down.sat() + MAX_OVERHEAD;
-      let resp = read_limited_bytes(max_body, &mut resp).await
+      let resp = read_limited_bytes(max_body, default(), &mut resp).await
         .discard_data().context("fetching response body")?;
 
       if ! status.is_success() {
index 8c0d996ef0fca412a546cbfb6dba30792ce874ec..8c3c23efdec1222eadcd465a02ef4c67b575bab0 100644 (file)
@@ -85,7 +85,9 @@ async fn handle(
     };
 
     let mut body = req.into_body();
-    let initial = match read_limited_bytes(METADATA_MAX_LEN, &mut body).await {
+    let initial = match read_limited_bytes(
+      METADATA_MAX_LEN, default(), &mut body
+    ).await {
       Ok(all) => all,
       Err(ReadLimitedError::Truncated { sofar,.. }) => sofar,
       Err(ReadLimitedError::Hyper(e)) => throw!(e),
index 5c1075205bb2eef874e52ade33c3fcf4f9aaf661..4b345d0c4cf8cbd8993a5fd4ce778477be5d0662 100644 (file)
@@ -39,12 +39,13 @@ impl<T> Result<T,ReadLimitedError> {
 }
 
 #[throws(ReadLimitedError)]
-pub async fn read_limited_bytes<S>(limit: usize, stream: &mut S) -> Box<[u8]>
+pub async fn read_limited_bytes<S>(limit: usize, initial: Box<[u8]>,
+                                   stream: &mut S) -> Box<[u8]>
 where S: futures::Stream<Item=Result<hyper::body::Bytes,hyper::Error>>
          + Debug + Unpin,
       // we also require that the Stream is cancellation-safe
 {
-  let mut accum = vec![];
+  let mut accum = initial.into_vec();
   while let Some(item) = stream.next().await {
     let b = item?;
     accum.extend(b);