-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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; | ||
/// use headers::ContentMd5; | ||
/// | ||
/// let md5 = ContentMd5([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5]); | ||
/// ``` | ||
#[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.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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters