From 134d82ecf4f8905f4ec84080adb1839f2de115ea Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sun, 10 Jul 2022 11:53:22 +0200 Subject: [PATCH] debug! log method of body response --- src/response.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/response.rs b/src/response.rs index 15ccda0..afb51cd 100644 --- a/src/response.rs +++ b/src/response.rs @@ -3,6 +3,7 @@ use std::str::FromStr; use std::{fmt, io::BufRead}; use chunked_transfer::Decoder as ChunkDecoder; +use log::debug; use url::Url; use crate::body::SizedReader; @@ -302,25 +303,40 @@ impl Response { let buffer_len = inner.buffer().len(); let body_reader: Box = match (use_chunked, limit_bytes) { - (true, _) => Box::new(PoolReturnRead::new( - &unit.agent, - &unit.url, - ChunkDecoder::new(stream), - )), + // Chunked responses have an unknown length, but do have an end of body + // marker. When we encounter the marker, we can return the underlying stream + // to the connection pool. + (true, _) => { + debug!("Chunked body in response"); + Box::new(PoolReturnRead::new( + &unit.agent, + &unit.url, + ChunkDecoder::new(stream), + )) + } + // Responses with a content-length header means we should limit the reading + // of the body to the number of bytes in the header. Once done, we can + // return the underlying stream to the connection pool. (false, Some(len)) => { let mut pooler = PoolReturnRead::new(&unit.agent, &unit.url, LimitedRead::new(stream, len)); + if len <= buffer_len { + debug!("Body entirely buffered (length: {})", len); let mut buf = vec![0; len]; pooler .read_exact(&mut buf) .expect("failed to read exact buffer length from stream"); Box::new(Cursor::new(buf)) } else { + debug!("Streaming body until content-length: {}", len); Box::new(pooler) } } - (false, None) => Box::new(stream), + (false, None) => { + debug!("Body of unknown size - read until socket close"); + Box::new(stream) + } }; match self.compression {