Skip to content

Commit f8e1f87

Browse files
authoredJul 6, 2024··
feat(codec): Make error when not utf8 value in compression encoding (#1768)
1 parent 4dda0a9 commit f8e1f87

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed
 

‎tonic/src/codec/compression.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,30 @@ impl CompressionEncoding {
129129
return Ok(None);
130130
};
131131

132-
let header_value_str = if let Ok(value) = header_value.to_str() {
133-
value
134-
} else {
135-
return Ok(None);
136-
};
137-
138-
match header_value_str {
132+
match header_value.as_bytes() {
139133
#[cfg(feature = "gzip")]
140-
"gzip" if enabled_encodings.is_enabled(CompressionEncoding::Gzip) => {
134+
b"gzip" if enabled_encodings.is_enabled(CompressionEncoding::Gzip) => {
141135
Ok(Some(CompressionEncoding::Gzip))
142136
}
143137
#[cfg(feature = "zstd")]
144-
"zstd" if enabled_encodings.is_enabled(CompressionEncoding::Zstd) => {
138+
b"zstd" if enabled_encodings.is_enabled(CompressionEncoding::Zstd) => {
145139
Ok(Some(CompressionEncoding::Zstd))
146140
}
147-
"identity" => Ok(None),
141+
b"identity" => Ok(None),
148142
other => {
143+
// NOTE: Workaround for lifetime limitation. Resolved at Rust 1.79.
144+
// https://blog.rust-lang.org/2024/06/13/Rust-1.79.0.html#extending-automatic-temporary-lifetime-extension
145+
let other_debug_string;
146+
149147
let mut status = Status::unimplemented(format!(
150148
"Content is compressed with `{}` which isn't supported",
151-
other
149+
match std::str::from_utf8(other) {
150+
Ok(s) => s,
151+
Err(_) => {
152+
other_debug_string = format!("{other:?}");
153+
&other_debug_string
154+
}
155+
}
152156
));
153157

154158
let header_value = enabled_encodings

0 commit comments

Comments
 (0)
Please sign in to comment.