From f8e1f87eb862676147fd6215b58c9090d259104d Mon Sep 17 00:00:00 2001 From: tottoto Date: Sat, 6 Jul 2024 23:33:44 +0900 Subject: [PATCH] feat(codec): Make error when not utf8 value in compression encoding (#1768) --- tonic/src/codec/compression.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tonic/src/codec/compression.rs b/tonic/src/codec/compression.rs index 2e75ef4fc..a19a4dc0b 100644 --- a/tonic/src/codec/compression.rs +++ b/tonic/src/codec/compression.rs @@ -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