Skip to content

Commit

Permalink
Add Content-MD5 header
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsided committed Jun 24, 2023
1 parent f01cc90 commit 386bc3a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/common/content_md5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::convert::TryInto;
use {Header, HeaderValue};

/// `Content-MD5` header, defined in
/// [RFC1864](https://datatracker.ietf.org/doc/html/rfc1864)
///
/// ## ABNF
///
/// ```text
/// Content-Length = 1*DIGIT
/// ```
///
/// ## Example values
///
/// * `Q2hlY2sgSW50ZWdyaXR5IQ==`
///
/// # Example
///
/// ```
/// # extern crate headers;
/// # extern crate http;
/// # extern crate headers_core;
/// use http::HeaderValue;
/// use headers::ContentMd5;
/// use headers_core::Header;
///
/// let value = HeaderValue::from_static("Q2hlY2sgSW50ZWdyaXR5IQ==");
///
/// let md5 = ContentMd5::decode(&mut [value].into_iter()).unwrap();
/// assert_eq!(md5.0, "Check Integrity!".as_bytes())
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ContentMd5(pub [u8; 16]);

static CONTENT_MD5: ::http::header::HeaderName =
::http::header::HeaderName::from_static("content-md5");

impl Header for ContentMd5 {
fn name() -> &'static ::http::header::HeaderName {
&CONTENT_MD5
}

fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, ::Error> {
let value = values.next().ok_or_else(::Error::invalid)?;

// Ensure base64 encoded length fits the expected MD5 digest length.
if value.len() < 22 || value.len() > 24 {
return Err(::Error::invalid());
}

let value = value.to_str().map_err(|_| ::Error::invalid())?;
let vec = base64::decode(value).map_err(|_| ::Error::invalid())?;
Ok(Self(vec[..16].try_into().map_err(|_| ::Error::invalid())?))
}

fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) {
let encoded = base64::encode(self.0);
if let Ok(value) = HeaderValue::from_str(&encoded) {
values.extend(std::iter::once(value));
}
}
}
2 changes: 2 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use self::content_encoding::ContentEncoding;
//pub use self::content_language::ContentLanguage;
pub use self::content_length::ContentLength;
pub use self::content_location::ContentLocation;
pub use self::content_md5::ContentMd5;
pub use self::content_range::ContentRange;
pub use self::content_type::ContentType;
pub use self::cookie::Cookie;
Expand Down Expand Up @@ -149,6 +150,7 @@ mod content_encoding;
//mod content_language;
mod content_length;
mod content_location;
mod content_md5;
mod content_range;
mod content_type;
mod cookie;
Expand Down

0 comments on commit 386bc3a

Please sign in to comment.