Skip to content

Commit

Permalink
fix: don't crash on utf-8 headers
Browse files Browse the repository at this point in the history
  • Loading branch information
OneOfOne committed Apr 8, 2024
1 parent dd3d41d commit 82270e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
14 changes: 7 additions & 7 deletions src/headers/header_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>) -> Result<Self, Error> {
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
Expand Down Expand Up @@ -74,7 +72,7 @@ impl FromStr for HeaderValue {
///
/// This checks it's valid ASCII.
fn from_str(s: &str) -> Result<Self, Self::Err> {
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) })
}
}
Expand Down Expand Up @@ -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), "\"Â\"");
}
}

0 comments on commit 82270e7

Please sign in to comment.