1use bytes::Bytes;
7use futures::StreamExt as _;
8use http::{HeaderMap, HeaderValue};
9use web_faith_encoding::{
10 AcceptEncoding, Coding, DEFAULT_ACCEPT_ENCODING, compress_buffer, decision, decode_stream,
11 layer_content_encoding, strip_decoded_headers,
12};
13
14#[tokio::main]
15async fn main() {
16 let accept = AcceptEncoding::parse(DEFAULT_ACCEPT_ENCODING);
17
18 let mut headers = HeaderMap::new();
20 headers.insert("content-encoding", HeaderValue::from_static("gzip"));
21 headers.insert("content-length", HeaderValue::from_static("42"));
22 let coding = decision(&headers, &accept).expect("gzip is in the default Accept-Encoding");
23 println!("negotiated: {coding:?}");
24
25 strip_decoded_headers(&mut headers);
27 println!("headers after decoding: {:?}", headers.keys().count());
28
29 let original = b"the quick brown fox jumps over the lazy dog".repeat(8);
31 let compressed = compress_buffer(&original, coding)
32 .await
33 .expect("gzip compresses");
34 println!(
35 "{} bytes in, {} bytes out",
36 original.len(),
37 compressed.len()
38 );
39
40 let stream = futures::stream::once(async move { Ok(Bytes::from(compressed)) });
41 let mut decoded = decode_stream(Box::pin(stream), coding);
42 let mut round_tripped = Vec::new();
43 while let Some(chunk) = decoded.next().await {
44 round_tripped.extend_from_slice(&chunk.expect("the body decodes"));
45 }
46 println!("round-tripped intact: {}", round_tripped == original);
47
48 println!(
50 "Content-Encoding: {}",
51 layer_content_encoding(Some("br"), Coding::Gzip)
52 );
53}