-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace headers module fork by an in-tree handler for the A…
…ccept-Encoding header (#354) * Replaced fork of the headers module by an in-tree handler for the Accept-Encoding header * Added test for various allowed whitespace within the Accept-Encoding header * Add tests for ContentCoding::ANY * Fixed links in documentation * Removed CargoCoding::ANY for now * Added license headers to imported files * Remove unused AcceptEncoding::gzip method * Make headers_ext module internal This required removing some unused code and code examples. Also, additional clippy warnings had to be addressed. * refactor: reduce the visibility of headers_ext and its methods --------- Co-authored-by: Jose Quintana <1700322+joseluisq@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
457 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,83 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// Original code by Parker Timmerman | ||
// Original code sourced from https://github.com/hyperium/headers/pull/70 | ||
|
||
use headers::{Error, Header}; | ||
use hyper::header::{HeaderName, HeaderValue, ACCEPT_ENCODING}; | ||
|
||
use super::{ContentCoding, QualityValue}; | ||
|
||
/// `Accept-Encoding` header, defined in | ||
/// [RFC7231](https://tools.ietf.org/html/rfc7231#section-5.3.4) | ||
/// | ||
/// The `Accept-Encoding` header field can be used by user agents to | ||
/// indicate what response content-codings are acceptable in the response. | ||
/// An "identity" token is used as a synonym for "no encoding" in | ||
/// order to communicate when no encoding is preferred. | ||
/// | ||
/// # ABNF | ||
/// | ||
/// ```text | ||
/// Accept-Encoding = #( codings [ weight ] ) | ||
/// codings = content-coding / "identity" / "*" | ||
/// ``` | ||
/// | ||
/// # Example Values | ||
/// | ||
/// * `gzip` | ||
/// * `br;q=1.0, gzip;q=0.8` | ||
/// | ||
#[derive(Clone, Debug)] | ||
pub(crate) struct AcceptEncoding(QualityValue); | ||
|
||
impl Header for AcceptEncoding { | ||
fn name() -> &'static HeaderName { | ||
&ACCEPT_ENCODING | ||
} | ||
|
||
fn decode<'i, I>(values: &mut I) -> Result<Self, Error> | ||
where | ||
I: Iterator<Item = &'i HeaderValue>, | ||
{ | ||
QualityValue::try_from_values(values).map(Self) | ||
} | ||
|
||
fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) { | ||
values.extend(std::iter::once((&self.0).into())) | ||
} | ||
} | ||
|
||
impl AcceptEncoding { | ||
/// Returns the most preferred encoding that is specified by the header, | ||
/// if one is specified. | ||
pub(crate) fn preferred_encoding(&self) -> Option<ContentCoding> { | ||
self.0.iter().next().map(ContentCoding::from) | ||
} | ||
|
||
/// Returns a quality sorted iterator of the `ContentCoding` | ||
pub(crate) fn sorted_encodings(&self) -> impl Iterator<Item = ContentCoding> + '_ { | ||
self.0.iter().map(ContentCoding::from) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn from_static() { | ||
let val = HeaderValue::from_static("deflate, gzip;q=1.0, br;q=0.9"); | ||
let accept_enc = AcceptEncoding(val.into()); | ||
|
||
assert_eq!( | ||
accept_enc.preferred_encoding(), | ||
Some(ContentCoding::DEFLATE) | ||
); | ||
|
||
let mut encodings = accept_enc.sorted_encodings(); | ||
assert_eq!(encodings.next(), Some(ContentCoding::DEFLATE)); | ||
assert_eq!(encodings.next(), Some(ContentCoding::GZIP)); | ||
assert_eq!(encodings.next(), Some(ContentCoding::BROTLI)); | ||
assert_eq!(encodings.next(), None); | ||
} | ||
} |
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,122 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// Original code by Parker Timmerman | ||
// Original code sourced from https://github.com/hyperium/headers/pull/70 | ||
|
||
// Derives an enum to represent content codings and some helpful impls | ||
macro_rules! define_content_coding { | ||
($($coding:ident; $str:expr,)+) => { | ||
use hyper::header::HeaderValue; | ||
use std::str::FromStr; | ||
|
||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
/// Values that are used with headers like [`Content-Encoding`](headers::ContentEncoding) or | ||
/// [`Accept-Encoding`](super::AcceptEncoding) | ||
/// | ||
/// [RFC7231](https://www.iana.org/assignments/http-parameters/http-parameters.xhtml) | ||
pub enum ContentCoding { | ||
$( | ||
#[allow(clippy::upper_case_acronyms)] | ||
#[doc = $str] | ||
$coding, | ||
)+ | ||
} | ||
|
||
impl ContentCoding { | ||
/// Returns a `&'static str` for a `ContentCoding` | ||
#[inline] | ||
pub(crate) fn as_str(&self) -> &'static str { | ||
match *self { | ||
$(ContentCoding::$coding => $str,)+ | ||
} | ||
} | ||
} | ||
|
||
impl From<&str> for ContentCoding { | ||
/// Given a `&str` returns a `ContentCoding` | ||
/// | ||
/// Note this will never fail, in the case of `&str` being an invalid content coding, | ||
/// will return `ContentCoding::IDENTITY` because `'identity'` is generally always an | ||
/// accepted coding. | ||
#[inline] | ||
fn from(s: &str) -> Self { | ||
ContentCoding::from_str(s).unwrap_or_else(|_| ContentCoding::IDENTITY) | ||
} | ||
} | ||
|
||
impl FromStr for ContentCoding { | ||
type Err = (); | ||
|
||
/// Given a `&str` will try to return a `ContentCoding` | ||
/// | ||
/// Different from `ContentCoding::from(&str)`, if `&str` is an invalid content | ||
/// coding, it will return `Err(())` | ||
#[inline] | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
$( | ||
stringify!($coding) | ||
| $str => Ok(Self::$coding), | ||
)+ | ||
_ => Err(()) | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for ContentCoding { | ||
#[inline] | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { | ||
write!(f, "{}", match *self { | ||
$(ContentCoding::$coding => $str.to_string(),)+ | ||
}) | ||
} | ||
} | ||
|
||
impl From<ContentCoding> for HeaderValue { | ||
fn from(coding: ContentCoding) -> HeaderValue { | ||
match coding { | ||
$(ContentCoding::$coding => HeaderValue::from_static($str),)+ | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
define_content_coding! { | ||
BROTLI; "br", | ||
COMPRESS; "compress", | ||
DEFLATE; "deflate", | ||
GZIP; "gzip", | ||
IDENTITY; "identity", | ||
ZSTD; "zstd", | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::ContentCoding; | ||
use std::str::FromStr; | ||
|
||
#[test] | ||
fn as_str() { | ||
assert_eq!(ContentCoding::GZIP.as_str(), "gzip"); | ||
} | ||
|
||
#[test] | ||
fn to_string() { | ||
assert_eq!(ContentCoding::DEFLATE.to_string(), "deflate".to_string()); | ||
} | ||
|
||
#[test] | ||
fn from() { | ||
assert_eq!(ContentCoding::from("br"), ContentCoding::BROTLI); | ||
assert_eq!(ContentCoding::from("GZIP"), ContentCoding::GZIP); | ||
assert_eq!(ContentCoding::from("zstd"), ContentCoding::ZSTD); | ||
assert_eq!(ContentCoding::from("blah blah"), ContentCoding::IDENTITY); | ||
} | ||
|
||
#[test] | ||
fn from_str() { | ||
assert_eq!(ContentCoding::from_str("br"), Ok(ContentCoding::BROTLI)); | ||
assert_eq!(ContentCoding::from_str("zstd"), Ok(ContentCoding::ZSTD)); | ||
assert_eq!(ContentCoding::from_str("blah blah"), Err(())); | ||
} | ||
} |
Oops, something went wrong.