Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codec): Make error when not utf8 value in compression encoding #1768

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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