diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index 8007ebe5cb..918ac62cdc 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -265,17 +265,12 @@ where self.state.reading = Reading::Body(Decoder::new(msg.decode)); } - if msg + self.state.allow_trailer_fields = msg .head .headers .get(TE) .map(|te_header| te_header == "trailers") - .unwrap_or(false) - { - self.state.allow_trailer_fields = true; - } else { - self.state.allow_trailer_fields = false; - } + .unwrap_or(false); Poll::Ready(Some(Ok((msg.head, msg.decode, wants)))) } diff --git a/src/proto/h1/encode.rs b/src/proto/h1/encode.rs index cbea0c0b09..41538aa90d 100644 --- a/src/proto/h1/encode.rs +++ b/src/proto/h1/encode.rs @@ -165,7 +165,7 @@ impl Encoder { pub(crate) fn encode_trailers( &self, - mut trailers: HeaderMap, + trailers: HeaderMap, title_case_headers: bool, ) -> Option> { match &self.kind { @@ -175,7 +175,7 @@ impl Encoder { let mut cur_name = None; let mut allowed_trailers = HeaderMap::new(); - for (opt_name, value) in trailers.drain() { + for (opt_name, value) in trailers.into_iter() { if let Some(n) = opt_name { cur_name = Some(n); } @@ -530,14 +530,18 @@ mod tests { let trailers = vec![HeaderValue::from_static("chunky-trailer")]; let encoder = encoder.into_chunked_with_trailing_fields(trailers); - let mut headers = HeaderMap::new(); - headers.insert( - HeaderName::from_static("chunky-trailer"), - HeaderValue::from_static("header data"), - ); - headers.insert( - HeaderName::from_static("should-not-be-included"), - HeaderValue::from_static("oops"), + let headers = HeaderMap::from_iter( + vec![ + ( + HeaderName::from_static("chunky-trailer"), + HeaderValue::from_static("header data"), + ), + ( + HeaderName::from_static("should-not-be-included"), + HeaderValue::from_static("oops"), + ), + ] + .into_iter(), ); let buf1 = encoder.encode_trailers::<&[u8]>(headers, false).unwrap(); @@ -547,6 +551,39 @@ mod tests { assert_eq!(dst, b"0\r\nchunky-trailer: header data\r\n\r\n"); } + #[test] + fn chunked_with_multiple_trailer_headers() { + let encoder = Encoder::chunked(); + let trailers = vec![ + HeaderValue::from_static("chunky-trailer"), + HeaderValue::from_static("chunky-trailer-2"), + ]; + let encoder = encoder.into_chunked_with_trailing_fields(trailers); + + let headers = HeaderMap::from_iter( + vec![ + ( + HeaderName::from_static("chunky-trailer"), + HeaderValue::from_static("header data"), + ), + ( + HeaderName::from_static("chunky-trailer-2"), + HeaderValue::from_static("more header data"), + ), + ] + .into_iter(), + ); + + let buf1 = encoder.encode_trailers::<&[u8]>(headers, false).unwrap(); + + let mut dst = Vec::new(); + dst.put(buf1); + assert_eq!( + dst, + b"0\r\nchunky-trailer: header data\r\nchunky-trailer-2: more header data\r\n\r\n" + ); + } + #[test] fn chunked_with_no_trailer_header() { let encoder = Encoder::chunked(); diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index a5a08d8cab..6828db75a7 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -1350,7 +1350,7 @@ impl Client { let allowed_trailer_fields: Vec = headers.get_all(header::TRAILER).iter().cloned().collect(); - if allowed_trailer_fields.len() > 0 { + if !allowed_trailer_fields.is_empty() { return enc.into_chunked_with_trailing_fields(allowed_trailer_fields); } }