Module tower_http::decompression
source · Available on crate features
decompression-br
or decompression-deflate
or decompression-gzip
or decompression-zstd
only.Expand description
Middleware that decompresses request and response bodies.
Examples
Request
use bytes::BytesMut;
use flate2::{write::GzEncoder, Compression};
use http::{header, HeaderValue, Request, Response};
use http_body::Body as _; // for Body::data
use hyper::Body;
use std::{error::Error, io::Write};
use tower::{Service, ServiceBuilder, service_fn, ServiceExt};
use tower_http::{BoxError, decompression::{DecompressionBody, RequestDecompressionLayer}};
// A request encoded with gzip coming from some HTTP client.
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
encoder.write_all(b"Hello?")?;
let request = Request::builder()
.header(header::CONTENT_ENCODING, "gzip")
.body(Body::from(encoder.finish()?))?;
// Our HTTP server
let mut server = ServiceBuilder::new()
// Automatically decompress request bodies.
.layer(RequestDecompressionLayer::new())
.service(service_fn(handler));
// Send the request, with the gzip encoded body, to our server.
let _response = server.ready().await?.call(request).await?;
// Handler receives request whose body is decoded when read
async fn handler(mut req: Request<DecompressionBody<Body>>) -> Result<Response<Body>, BoxError>{
let mut data = BytesMut::new();
while let Some(chunk) = req.body_mut().data().await {
let chunk = chunk?;
data.extend_from_slice(&chunk[..]);
}
assert_eq!(data.freeze().to_vec(), b"Hello?");
Ok(Response::new(Body::from("Hello, World!")))
}
Response
use bytes::BytesMut;
use http::{Request, Response};
use http_body::Body as _; // for Body::data
use hyper::Body;
use std::convert::Infallible;
use tower::{Service, ServiceExt, ServiceBuilder, service_fn};
use tower_http::{compression::Compression, decompression::DecompressionLayer, BoxError};
// Some opaque service that applies compression.
let service = Compression::new(service_fn(handle));
// Our HTTP client.
let mut client = ServiceBuilder::new()
// Automatically decompress response bodies.
.layer(DecompressionLayer::new())
.service(service);
// Call the service.
//
// `DecompressionLayer` takes care of setting `Accept-Encoding`.
let request = Request::new(Body::empty());
let response = client
.ready()
.await?
.call(request)
.await?;
// Read the body
let mut body = response.into_body();
let mut bytes = BytesMut::new();
while let Some(chunk) = body.data().await {
let chunk = chunk?;
bytes.extend_from_slice(&chunk[..]);
}
let body = String::from_utf8(bytes.to_vec()).map_err(Into::<BoxError>::into)?;
assert_eq!(body, "Hello, World!");
Structs
- Decompresses response bodies of the underlying service.
- Response body of
RequestDecompression
andDecompression
. - Decompresses response bodies of the underlying service.
- Decompresses request bodies and calls its underlying service.
- Response future of [
RequestDecompression
] - Decompresses request bodies and calls its underlying service.
- Response future of
Decompression
.