Skip to content

Commit 29c2a83

Browse files
committed
reuse shared content_type_header
1 parent 6a4f28b commit 29c2a83

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

crates/common/src/utils.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
fmt::Display,
66
net::Ipv4Addr,
77
str::FromStr,
8+
sync::LazyLock,
89
time::{SystemTime, UNIX_EPOCH},
910
};
1011

@@ -747,6 +748,22 @@ impl EncodingType {
747748
EncodingType::Ssz => APPLICATION_OCTET_STREAM,
748749
}
749750
}
751+
752+
/// Pre-built `Content-Type` header for this encoding. `HeaderValue` is
753+
/// not `const`-constructible in stable Rust, so the values are
754+
/// lazy-initialized once per process via `LazyLock` from static ASCII
755+
/// strings. Callers can clone the returned reference cheaply (the
756+
/// underlying bytes are shared).
757+
pub fn content_type_header(&self) -> &'static HeaderValue {
758+
static JSON_HEADER: LazyLock<HeaderValue> =
759+
LazyLock::new(|| HeaderValue::from_static(APPLICATION_JSON));
760+
static SSZ_HEADER: LazyLock<HeaderValue> =
761+
LazyLock::new(|| HeaderValue::from_static(APPLICATION_OCTET_STREAM));
762+
match self {
763+
EncodingType::Json => &JSON_HEADER,
764+
EncodingType::Ssz => &SSZ_HEADER,
765+
}
766+
}
750767
}
751768

752769
impl std::fmt::Display for EncodingType {

crates/pbs/src/routes/get_header.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ pub async fn handle_get_header<S: BuilderApiState, A: BuilderApi<S>>(
6363
let consensus_version_header =
6464
HeaderValue::from_str(&light_bid.version.to_string())
6565
.expect("fork name is always a valid header value");
66-
let content_type = light_bid.encoding_type.content_type();
67-
let content_type_header = HeaderValue::from_str(content_type)
68-
.expect("content type is a static ASCII string");
66+
let content_type_header =
67+
light_bid.encoding_type.content_type_header().clone();
6968

7069
// Build response
7170
let mut res = light_bid.raw_bytes.into_response();
7271
res.headers_mut()
7372
.insert(CONSENSUS_VERSION_HEADER, consensus_version_header);
7473
res.headers_mut().insert(CONTENT_TYPE, content_type_header);
75-
info!("sending response as {} (light)", content_type);
74+
info!("sending response as {} (light)", light_bid.encoding_type);
7675
Ok(res)
7776
}
7877
CompoundGetHeaderResponse::Full(max_bid) => {
@@ -94,8 +93,7 @@ pub async fn handle_get_header<S: BuilderApiState, A: BuilderApi<S>>(
9493
HeaderValue::from_str(&max_bid.version.to_string())
9594
.expect("fork name is always a valid header value");
9695
let content_type_header =
97-
HeaderValue::from_str(EncodingType::Ssz.content_type())
98-
.expect("content type is a static ASCII string");
96+
EncodingType::Ssz.content_type_header().clone();
9997

10098
let mut res = max_bid.data.as_ssz_bytes().into_response();
10199
res.headers_mut()

crates/pbs/src/routes/submit_block.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ async fn handle_submit_block_impl<S: BuilderApiState, A: BuilderApi<S>>(
108108
)));
109109
}
110110
}?;
111-
let content_type = payload_and_blobs.encoding_type.content_type();
112-
let content_type_header = HeaderValue::from_str(content_type).unwrap();
111+
let content_type_header =
112+
payload_and_blobs.encoding_type.content_type_header().clone();
113113

114114
// Build response
115115
let mut res = payload_and_blobs.raw_bytes.into_response();
116116
res.headers_mut().insert(CONSENSUS_VERSION_HEADER, consensus_version_header);
117117
res.headers_mut().insert(CONTENT_TYPE, content_type_header);
118-
info!("sending response as {} (light)", content_type);
118+
info!("sending response as {} (light)", payload_and_blobs.encoding_type);
119119
Ok(res)
120120
}
121121
CompoundSubmitBlockResponse::Full(payload_and_blobs) => {
@@ -130,9 +130,7 @@ async fn handle_submit_block_impl<S: BuilderApiState, A: BuilderApi<S>>(
130130
if accepts_ssz {
131131
let mut response = payload_and_blobs.data.as_ssz_bytes().into_response();
132132

133-
// This won't actually fail since the string is a const
134-
let content_type_header =
135-
HeaderValue::from_str(EncodingType::Ssz.content_type()).unwrap();
133+
let content_type_header = EncodingType::Ssz.content_type_header().clone();
136134
response.headers_mut().insert(CONTENT_TYPE, content_type_header);
137135
response.headers_mut().insert(
138136
CONSENSUS_VERSION_HEADER,

0 commit comments

Comments
 (0)