tower_http/limit/
layer.rs

1use super::RequestBodyLimit;
2use tower_layer::Layer;
3
4/// Layer that applies the [`RequestBodyLimit`] middleware that intercepts requests
5/// with body lengths greater than the configured limit and converts them into
6/// `413 Payload Too Large` responses.
7///
8/// See the [module docs](crate::limit) for an example.
9///
10/// [`RequestBodyLimit`]: super::RequestBodyLimit
11#[derive(Clone, Copy, Debug)]
12pub struct RequestBodyLimitLayer {
13    limit: usize,
14}
15
16impl RequestBodyLimitLayer {
17    /// Create a new `RequestBodyLimitLayer` with the given body length limit.
18    pub fn new(limit: usize) -> Self {
19        Self { limit }
20    }
21}
22
23impl<S> Layer<S> for RequestBodyLimitLayer {
24    type Service = RequestBodyLimit<S>;
25
26    fn layer(&self, inner: S) -> Self::Service {
27        RequestBodyLimit {
28            inner,
29            limit: self.limit,
30        }
31    }
32}