tower_http/map_request_body.rs
1//! Apply a transformation to the request body.
2//!
3//! # Example
4//!
5//! ```
6//! use http_body_util::Full;
7//! use bytes::Bytes;
8//! use http::{Request, Response};
9//! use std::convert::Infallible;
10//! use std::{pin::Pin, task::{ready, Context, Poll}};
11//! use tower::{ServiceBuilder, service_fn, ServiceExt, Service};
12//! use tower_http::map_request_body::MapRequestBodyLayer;
13//!
14//! // A wrapper for a `Full<Bytes>`
15//! struct BodyWrapper {
16//! inner: Full<Bytes>,
17//! }
18//!
19//! impl BodyWrapper {
20//! fn new(inner: Full<Bytes>) -> Self {
21//! Self { inner }
22//! }
23//! }
24//!
25//! impl http_body::Body for BodyWrapper {
26//! // ...
27//! # type Data = Bytes;
28//! # type Error = tower::BoxError;
29//! # fn poll_frame(
30//! # self: Pin<&mut Self>,
31//! # cx: &mut Context<'_>
32//! # ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> { unimplemented!() }
33//! # fn is_end_stream(&self) -> bool { unimplemented!() }
34//! # fn size_hint(&self) -> http_body::SizeHint { unimplemented!() }
35//! }
36//!
37//! async fn handle<B>(_: Request<B>) -> Result<Response<Full<Bytes>>, Infallible> {
38//! // ...
39//! # Ok(Response::new(Full::default()))
40//! }
41//!
42//! # #[tokio::main]
43//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
44//! let mut svc = ServiceBuilder::new()
45//! // Wrap response bodies in `BodyWrapper`
46//! .layer(MapRequestBodyLayer::new(BodyWrapper::new))
47//! .service_fn(handle);
48//!
49//! // Call the service
50//! let request = Request::new(Full::default());
51//!
52//! svc.ready().await?.call(request).await?;
53//! # Ok(())
54//! # }
55//! ```
56
57use http::{Request, Response};
58use std::{
59 fmt,
60 task::{Context, Poll},
61};
62use tower_layer::Layer;
63use tower_service::Service;
64
65/// Apply a transformation to the request body.
66///
67/// See the [module docs](crate::map_request_body) for an example.
68#[derive(Clone)]
69pub struct MapRequestBodyLayer<F> {
70 f: F,
71}
72
73impl<F> MapRequestBodyLayer<F> {
74 /// Create a new [`MapRequestBodyLayer`].
75 ///
76 /// `F` is expected to be a function that takes a body and returns another body.
77 pub fn new(f: F) -> Self {
78 Self { f }
79 }
80}
81
82impl<S, F> Layer<S> for MapRequestBodyLayer<F>
83where
84 F: Clone,
85{
86 type Service = MapRequestBody<S, F>;
87
88 fn layer(&self, inner: S) -> Self::Service {
89 MapRequestBody::new(inner, self.f.clone())
90 }
91}
92
93impl<F> fmt::Debug for MapRequestBodyLayer<F> {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 f.debug_struct("MapRequestBodyLayer")
96 .field("f", &std::any::type_name::<F>())
97 .finish()
98 }
99}
100
101/// Apply a transformation to the request body.
102///
103/// See the [module docs](crate::map_request_body) for an example.
104#[derive(Clone)]
105pub struct MapRequestBody<S, F> {
106 inner: S,
107 f: F,
108}
109
110impl<S, F> MapRequestBody<S, F> {
111 /// Create a new [`MapRequestBody`].
112 ///
113 /// `F` is expected to be a function that takes a body and returns another body.
114 pub fn new(service: S, f: F) -> Self {
115 Self { inner: service, f }
116 }
117
118 /// Returns a new [`Layer`] that wraps services with a `MapRequestBodyLayer` middleware.
119 ///
120 /// [`Layer`]: tower_layer::Layer
121 pub fn layer(f: F) -> MapRequestBodyLayer<F> {
122 MapRequestBodyLayer::new(f)
123 }
124
125 define_inner_service_accessors!();
126}
127
128impl<F, S, ReqBody, ResBody, NewReqBody> Service<Request<ReqBody>> for MapRequestBody<S, F>
129where
130 S: Service<Request<NewReqBody>, Response = Response<ResBody>>,
131 F: FnMut(ReqBody) -> NewReqBody,
132{
133 type Response = S::Response;
134 type Error = S::Error;
135 type Future = S::Future;
136
137 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
138 self.inner.poll_ready(cx)
139 }
140
141 fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
142 let req = req.map(&mut self.f);
143 self.inner.call(req)
144 }
145}
146
147impl<S, F> fmt::Debug for MapRequestBody<S, F>
148where
149 S: fmt::Debug,
150{
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 f.debug_struct("MapRequestBody")
153 .field("inner", &self.inner)
154 .field("f", &std::any::type_name::<F>())
155 .finish()
156 }
157}