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

Add Content-MD5 header #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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