Skip to content

Commit 1b5778e

Browse files
committed
Add Content-Encoding negotiation example
1 parent 5f09d81 commit 1b5778e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/content/accept_encoding.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ use std::option;
1010
use std::slice;
1111

1212
/// Client header advertising available compression algorithms.
13+
///
14+
/// # Specifications
15+
///
16+
/// - [RFC 7231, section 5.3.4: Accept-Encoding](https://tools.ietf.org/html/rfc7231#section-5.3.4)
17+
///
18+
/// # Examples
19+
///
20+
/// ```
21+
/// # fn main() -> http_types::Result<()> {
22+
/// #
23+
/// use http_types::content::{AcceptEncoding, ContentEncoding, Encoding, EncodingProposal};
24+
/// use http_types::Response;
25+
///
26+
/// let mut accept = AcceptEncoding::new();
27+
/// accept.push(EncodingProposal::new(Encoding::Brotli, Some(0.8))?);
28+
/// accept.push(EncodingProposal::new(Encoding::Gzip, Some(0.4))?);
29+
/// accept.push(EncodingProposal::new(Encoding::Identity, None)?);
30+
///
31+
/// let mut res = Response::new(200);
32+
/// let encoding = accept.negotiate(&[Encoding::Gzip, Encoding::Brotli])?;
33+
/// encoding.apply(&mut res);
34+
///
35+
/// assert_eq!(res["Content-Encoding"], "br");
36+
/// #
37+
/// # Ok(()) }
38+
/// ```
1339
pub struct AcceptEncoding {
1440
wildcard: bool,
1541
entries: Vec<EncodingProposal>,

0 commit comments

Comments
 (0)