From 82270e7322a82fb751116b5e9d8a90cc7c11c8f6 Mon Sep 17 00:00:00 2001 From: OneOfOne Date: Mon, 8 Apr 2024 12:47:20 -0500 Subject: [PATCH] fix: don't crash on utf-8 headers --- Cargo.toml | 2 +- src/headers/header_value.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6dc79528..04459565 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "http-types-rs" -version = "4.0.3" +version = "4.0.4" license = "MIT OR Apache-2.0" repository = "https://github.com/OneOfone/http-types" documentation = "https://docs.rs/http-types-rs" diff --git a/src/headers/header_value.rs b/src/headers/header_value.rs index 288ab7f4..81adf75d 100644 --- a/src/headers/header_value.rs +++ b/src/headers/header_value.rs @@ -15,17 +15,15 @@ pub struct HeaderValue { } impl HeaderValue { - /// Create a new `HeaderValue` from a Vec of ASCII bytes. + /// Create a new `HeaderValue` from a vec of valid ascii or utf8 bytes. /// /// # Error /// /// This function will error if the bytes is not valid ASCII. pub fn from_bytes(bytes: Vec) -> Result { - crate::ensure!(bytes.is_ascii(), "Bytes should be valid ASCII"); - - // This is permitted because ASCII is valid UTF-8, and we just checked that. - let string = unsafe { String::from_utf8_unchecked(bytes) }; - Ok(Self { inner: string }) + Ok(Self { + inner: String::from_utf8(bytes)?, + }) } /// Converts a vector of bytes to a `HeaderValue` without checking that the string contains @@ -74,7 +72,7 @@ impl FromStr for HeaderValue { /// /// This checks it's valid ASCII. fn from_str(s: &str) -> Result { - crate::ensure!(s.is_ascii(), "String slice should be valid ASCII"); + // crate::ensure!(s.is_ascii(), "String slice should be valid ASCII"); Ok(Self { inner: String::from(s) }) } } @@ -138,5 +136,7 @@ mod tests { fn test_debug() { let header_value = HeaderValue::from_str("foo0").unwrap(); assert_eq!(format!("{:?}", header_value), "\"foo0\""); + let header_value = HeaderValue::from_str("Â").unwrap(); + assert_eq!(format!("{:?}", header_value), "\"Â\""); } }