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

feat: add accept_encoding #191

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
61 changes: 61 additions & 0 deletions src/common/accept_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::iter::FromIterator;

use bytes::BytesMut;
use http::HeaderValue;

use crate::util::FlatCsv;

/// `Accept-Encoding` header, defined in
/// [RFC7231](https://datatracker.ietf.org/doc/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
/// * `compress, gzip`
/// * ``
/// * `*`
/// * `compress;q=0.5, gzip;q=1`
/// * `gzip;q=1.0, identity; q=0.5, *;q=0`
#[derive(Clone, Debug, PartialEq)]
pub struct AcceptEncoding(FlatCsv);

derive_header! {
AcceptEncoding(_),
name: ACCEPT_ENCODING
}

impl AcceptEncoding {
/// Iterator the codings with weight.
pub fn iter(&self) -> impl Iterator<Item = &str> {
self.0.iter()
}

/// Create from a iterator of given codings with optional weigth.
pub fn from_pairs<'a>(pairs: impl Iterator<Item = (&'a str, Option<f32>)>) -> Self {
let iter = pairs.into_iter().filter_map(|(coding, q)| {
if let Some(q) = q {
let mut buf = BytesMut::new();
buf.extend_from_slice(coding.as_bytes());
buf.extend_from_slice(&[b';']);
buf.extend_from_slice(format!("{:.1}", q).as_bytes());
HeaderValue::from_maybe_shared(buf.freeze()).ok()
} else {
HeaderValue::from_str(coding).ok()
}
});
let csv: FlatCsv = FlatCsv::from_iter(iter);

AcceptEncoding(csv)
}
}
4 changes: 2 additions & 2 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! is used, such as `ContentType(pub Mime)`.

//pub use self::accept_charset::AcceptCharset;
//pub use self::accept_encoding::AcceptEncoding;
pub use self::accept_encoding::AcceptEncoding;
//pub use self::accept_language::AcceptLanguage;
pub use self::accept_ranges::AcceptRanges;
//pub use self::accept::Accept;
Expand Down Expand Up @@ -128,7 +128,7 @@ macro_rules! bench_header {

//mod accept;
//mod accept_charset;
//mod accept_encoding;
mod accept_encoding;
//mod accept_language;
mod accept_ranges;
mod access_control_allow_credentials;
Expand Down