From a7f2019adbb7fa438c680acdc325f1ad5479752a Mon Sep 17 00:00:00 2001 From: "Herman J. Radtke III" Date: Fri, 3 Nov 2023 06:14:33 -0400 Subject: [PATCH] re-use header functions from role module --- src/proto/h1/conn.rs | 4 +++- src/proto/h1/encode.rs | 29 +++++++++++------------------ src/proto/h1/role.rs | 6 ++---- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index 69883b335b..f53318236f 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -651,7 +651,9 @@ where match self.state.writing { Writing::Body(ref encoder) => { - if let Some(enc_buf) = encoder.encode_trailers(trailers) { + if let Some(enc_buf) = + encoder.encode_trailers(trailers, self.state.title_case_headers) + { self.io.buffer(enc_buf); self.state.writing = if encoder.is_last() || encoder.is_close_delimited() { diff --git a/src/proto/h1/encode.rs b/src/proto/h1/encode.rs index 0b983295e1..689edf53f4 100644 --- a/src/proto/h1/encode.rs +++ b/src/proto/h1/encode.rs @@ -13,6 +13,7 @@ use http::{ }; use super::io::WriteBuf; +use super::role::{write_headers, write_headers_title_case}; type StaticBuf = &'static [u8]; @@ -161,7 +162,11 @@ impl Encoder { EncodedBuf { kind } } - pub(crate) fn encode_trailers(&self, mut trailers: HeaderMap) -> Option> { + pub(crate) fn encode_trailers( + &self, + mut trailers: HeaderMap, + title_case_headers: bool, + ) -> Option> { match &self.kind { Kind::Chunked(allowed_trailer_fields) => { let allowed_trailer_fields_map = match allowed_trailer_fields { @@ -188,7 +193,11 @@ impl Encoder { } let mut buf = Vec::new(); - write_headers(&allowed_trailers, &mut buf); + if title_case_headers { + write_headers_title_case(&allowed_trailers, &mut buf); + } else { + write_headers(&allowed_trailers, &mut buf); + } Some(EncodedBuf { kind: BufKind::Trailers(b"0\r\n".chain(Bytes::from(buf)).chain(b"\r\n")), @@ -276,22 +285,6 @@ fn allowed_trailer_field_map(allowed_trailer_fields: &Vec) -> HashM trailer_map } -// FIXME: dry up -fn write_headers(headers: &HeaderMap, dst: &mut Vec) { - for (name, value) in headers { - extend(dst, name.as_str().as_bytes()); - extend(dst, b": "); - extend(dst, value.as_bytes()); - extend(dst, b"\r\n"); - } -} - -#[inline] -fn extend(dst: &mut Vec, data: &[u8]) { - dst.extend_from_slice(data); -} -// end FIXME: dry up - impl Buf for EncodedBuf where B: Buf, diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index 7196180f82..fa0cab0a7f 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -1507,8 +1507,7 @@ fn title_case(dst: &mut Vec, name: &[u8]) { } } -#[cfg(feature = "client")] -fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec) { +pub(crate) fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec) { for (name, value) in headers { title_case(dst, name.as_str().as_bytes()); extend(dst, b": "); @@ -1517,8 +1516,7 @@ fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec) { } } -#[cfg(feature = "client")] -fn write_headers(headers: &HeaderMap, dst: &mut Vec) { +pub(crate) fn write_headers(headers: &HeaderMap, dst: &mut Vec) { for (name, value) in headers { extend(dst, name.as_str().as_bytes()); extend(dst, b": ");