Skip to content

Commit

Permalink
re-use header functions from role module
Browse files Browse the repository at this point in the history
  • Loading branch information
hjr3 committed Nov 3, 2023
1 parent bfc2009 commit a7f2019
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
29 changes: 11 additions & 18 deletions src/proto/h1/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use http::{
};

use super::io::WriteBuf;
use super::role::{write_headers, write_headers_title_case};

type StaticBuf = &'static [u8];

Expand Down Expand Up @@ -161,7 +162,11 @@ impl Encoder {
EncodedBuf { kind }
}

pub(crate) fn encode_trailers<B>(&self, mut trailers: HeaderMap) -> Option<EncodedBuf<B>> {
pub(crate) fn encode_trailers<B>(
&self,
mut trailers: HeaderMap,
title_case_headers: bool,
) -> Option<EncodedBuf<B>> {
match &self.kind {
Kind::Chunked(allowed_trailer_fields) => {
let allowed_trailer_fields_map = match allowed_trailer_fields {
Expand All @@ -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")),
Expand Down Expand Up @@ -276,22 +285,6 @@ fn allowed_trailer_field_map(allowed_trailer_fields: &Vec<HeaderValue>) -> HashM
trailer_map
}

// FIXME: dry up
fn write_headers(headers: &HeaderMap, dst: &mut Vec<u8>) {
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<u8>, data: &[u8]) {
dst.extend_from_slice(data);
}
// end FIXME: dry up

impl<B> Buf for EncodedBuf<B>
where
B: Buf,
Expand Down
6 changes: 2 additions & 4 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1507,8 +1507,7 @@ fn title_case(dst: &mut Vec<u8>, name: &[u8]) {
}
}

#[cfg(feature = "client")]
fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec<u8>) {
pub(crate) fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec<u8>) {
for (name, value) in headers {
title_case(dst, name.as_str().as_bytes());
extend(dst, b": ");
Expand All @@ -1517,8 +1516,7 @@ fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec<u8>) {
}
}

#[cfg(feature = "client")]
fn write_headers(headers: &HeaderMap, dst: &mut Vec<u8>) {
pub(crate) fn write_headers(headers: &HeaderMap, dst: &mut Vec<u8>) {
for (name, value) in headers {
extend(dst, name.as_str().as_bytes());
extend(dst, b": ");
Expand Down

0 comments on commit a7f2019

Please sign in to comment.