1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
//! Middleware that decompresses request and response bodies.
//!
//! # Examples
//!
//! #### Request
//!
//! ```rust
//! use bytes::Bytes;
//! use flate2::{write::GzEncoder, Compression};
//! use http::{header, HeaderValue, Request, Response};
//! use http_body_util::{Full, BodyExt};
//! use std::{error::Error, io::Write};
//! use tower::{Service, ServiceBuilder, service_fn, ServiceExt};
//! use tower_http::{BoxError, decompression::{DecompressionBody, RequestDecompressionLayer}};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), BoxError> {
//! // 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(Full::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<Full<Bytes>>>,
//! ) -> Result<Response<Full<Bytes>>, BoxError>{
//! let data = req.into_body().collect().await?.to_bytes();
//! assert_eq!(&data[..], b"Hello?");
//! Ok(Response::new(Full::from("Hello, World!")))
//! }
//! # Ok(())
//! # }
//! ```
//!
//! #### Response
//!
//! ```rust
//! use bytes::Bytes;
//! use http::{Request, Response};
//! use http_body_util::{Full, BodyExt};
//! use std::convert::Infallible;
//! use tower::{Service, ServiceExt, ServiceBuilder, service_fn};
//! use tower_http::{compression::Compression, decompression::DecompressionLayer, BoxError};
//! #
//! # #[tokio::main]
//! # async fn main() -> Result<(), tower_http::BoxError> {
//! # async fn handle(req: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, Infallible> {
//! # let body = Full::from("Hello, World!");
//! # Ok(Response::new(body))
//! # }
//!
//! // 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(Full::<Bytes>::default());
//!
//! let response = client
//! .ready()
//! .await?
//! .call(request)
//! .await?;
//!
//! // Read the body
//! let body = response.into_body();
//! let bytes = body.collect().await?.to_bytes().to_vec();
//! let body = String::from_utf8(bytes).map_err(Into::<BoxError>::into)?;
//!
//! assert_eq!(body, "Hello, World!");
//! #
//! # Ok(())
//! # }
//! ```
mod request;
mod body;
mod future;
mod layer;
mod service;
pub use self::{
body::DecompressionBody, future::ResponseFuture, layer::DecompressionLayer,
service::Decompression,
};
pub use self::request::future::RequestDecompressionFuture;
pub use self::request::layer::RequestDecompressionLayer;
pub use self::request::service::RequestDecompression;
#[cfg(test)]
mod tests {
use std::convert::Infallible;
use std::io::Write;
use super::*;
use crate::test_helpers::Body;
use crate::{compression::Compression, test_helpers::WithTrailers};
use flate2::write::GzEncoder;
use http::Response;
use http::{HeaderMap, HeaderName, Request};
use http_body_util::BodyExt;
use tower::{service_fn, Service, ServiceExt};
#[tokio::test]
async fn works() {
let mut client = Decompression::new(Compression::new(service_fn(handle)));
let req = Request::builder()
.header("accept-encoding", "gzip")
.body(Body::empty())
.unwrap();
let res = client.ready().await.unwrap().call(req).await.unwrap();
// read the body, it will be decompressed automatically
let body = res.into_body();
let collected = body.collect().await.unwrap();
let trailers = collected.trailers().cloned().unwrap();
let decompressed_data = String::from_utf8(collected.to_bytes().to_vec()).unwrap();
assert_eq!(decompressed_data, "Hello, World!");
// maintains trailers
assert_eq!(trailers["foo"], "bar");
}
async fn handle(_req: Request<Body>) -> Result<Response<WithTrailers<Body>>, Infallible> {
let mut trailers = HeaderMap::new();
trailers.insert(HeaderName::from_static("foo"), "bar".parse().unwrap());
let body = Body::from("Hello, World!").with_trailers(trailers);
Ok(Response::builder().body(body).unwrap())
}
#[tokio::test]
async fn decompress_multi_gz() {
let mut client = Decompression::new(service_fn(handle_multi_gz));
let req = Request::builder()
.header("accept-encoding", "gzip")
.body(Body::empty())
.unwrap();
let res = client.ready().await.unwrap().call(req).await.unwrap();
// read the body, it will be decompressed automatically
let body = res.into_body();
let decompressed_data =
String::from_utf8(body.collect().await.unwrap().to_bytes().to_vec()).unwrap();
assert_eq!(decompressed_data, "Hello, World!");
}
async fn handle_multi_gz(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
let mut buf = Vec::new();
let mut enc1 = GzEncoder::new(&mut buf, Default::default());
enc1.write_all(b"Hello, ").unwrap();
enc1.finish().unwrap();
let mut enc2 = GzEncoder::new(&mut buf, Default::default());
enc2.write_all(b"World!").unwrap();
enc2.finish().unwrap();
let mut res = Response::new(Body::from(buf));
res.headers_mut()
.insert("content-encoding", "gzip".parse().unwrap());
Ok(res)
}
#[allow(dead_code)]
async fn is_compatible_with_hyper() {
let client =
hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new())
.build_http();
let mut client = Decompression::new(client);
let req = Request::new(Body::empty());
let _: Response<DecompressionBody<_>> =
client.ready().await.unwrap().call(req).await.unwrap();
}
}