Skip to content

Commit

Permalink
fix(client): drop connection if body not consumed
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Jan 11, 2025
1 parent afd9053 commit ad51cce
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions client/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ impl ResponseBody {
}
}

impl Drop for ResponseBody {
fn drop(&mut self) {
#[cfg(feature = "http1")]
if let Self::H1(ref mut body) = *self {
if !body.is_consumed() {
body.conn_mut().destroy_on_drop()
}
}
}
}

impl Stream for ResponseBody {
type Item = Result<Bytes, BodyError>;

Expand Down
4 changes: 4 additions & 0 deletions client/src/h1/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl<C> ResponseBody<C> {
pub(crate) fn conn_mut(&mut self) -> &mut C {
&mut self.conn
}

pub(crate) fn is_consumed(&self) -> bool {
self.decoder.is_consumed()
}
}

impl<C> Stream for ResponseBody<C>
Expand Down
8 changes: 8 additions & 0 deletions http/src/h1/proto/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ impl TransferCoding {
}
}

/// Determine if the body was consumed given the current state of the codec
pub fn is_consumed(&self) -> bool {
match self {
Self::Eof | Self::Length(0) | Self::DecodeChunked(ChunkedState::End, _) => true,
_ => false,
}
}

/// decode body. See [ChunkResult] for detailed outcome.
pub fn decode(&mut self, src: &mut BytesMut) -> ChunkResult {
match *self {
Expand Down

0 comments on commit ad51cce

Please sign in to comment.