Skip to content

Commit

Permalink
feat(codec): Make error when not utf8 value in compression encoding (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Jul 6, 2024
1 parent 4dda0a9 commit f8e1f87
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions tonic/src/codec/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,30 @@ impl CompressionEncoding {
return Ok(None);
};

let header_value_str = if let Ok(value) = header_value.to_str() {
value
} else {
return Ok(None);
};

match header_value_str {
match header_value.as_bytes() {
#[cfg(feature = "gzip")]
"gzip" if enabled_encodings.is_enabled(CompressionEncoding::Gzip) => {
b"gzip" if enabled_encodings.is_enabled(CompressionEncoding::Gzip) => {
Ok(Some(CompressionEncoding::Gzip))
}
#[cfg(feature = "zstd")]
"zstd" if enabled_encodings.is_enabled(CompressionEncoding::Zstd) => {
b"zstd" if enabled_encodings.is_enabled(CompressionEncoding::Zstd) => {
Ok(Some(CompressionEncoding::Zstd))
}
"identity" => Ok(None),
b"identity" => Ok(None),
other => {
// NOTE: Workaround for lifetime limitation. Resolved at Rust 1.79.
// https://blog.rust-lang.org/2024/06/13/Rust-1.79.0.html#extending-automatic-temporary-lifetime-extension
let other_debug_string;

let mut status = Status::unimplemented(format!(
"Content is compressed with `{}` which isn't supported",
other
match std::str::from_utf8(other) {
Ok(s) => s,
Err(_) => {
other_debug_string = format!("{other:?}");
&other_debug_string
}
}
));

let header_value = enabled_encodings
Expand Down

0 comments on commit f8e1f87

Please sign in to comment.