rama_net/stream/layer/http/
body_limit.rs1use crate::stream::Stream;
2use rama_core::{Context, Layer, Service};
3use rama_http_types::BodyLimit;
4use rama_utils::macros::define_inner_service_accessors;
5use std::fmt;
6
7#[derive(Debug, Clone)]
16pub struct BodyLimitLayer {
17 limit: BodyLimit,
18}
19
20impl BodyLimitLayer {
21 pub fn request_only(limit: usize) -> Self {
25 Self {
26 limit: BodyLimit::request_only(limit),
27 }
28 }
29
30 pub fn response_only(limit: usize) -> Self {
34 Self {
35 limit: BodyLimit::response_only(limit),
36 }
37 }
38
39 pub fn symmetric(limit: usize) -> Self {
43 Self {
44 limit: BodyLimit::symmetric(limit),
45 }
46 }
47
48 pub fn asymmetric(request: usize, response: usize) -> Self {
53 Self {
54 limit: BodyLimit::asymmetric(request, response),
55 }
56 }
57}
58
59impl<S> Layer<S> for BodyLimitLayer {
60 type Service = BodyLimitService<S>;
61
62 fn layer(&self, inner: S) -> Self::Service {
63 BodyLimitService::new(inner, self.limit)
64 }
65}
66
67#[derive(Clone)]
71pub struct BodyLimitService<S> {
72 inner: S,
73 limit: BodyLimit,
74}
75
76impl<S> BodyLimitService<S> {
77 pub const fn new(service: S, limit: BodyLimit) -> Self {
79 Self {
80 inner: service,
81 limit,
82 }
83 }
84
85 define_inner_service_accessors!();
86
87 pub fn request_only(service: S, limit: usize) -> Self {
91 BodyLimitLayer::request_only(limit).into_layer(service)
92 }
93
94 pub fn response_only(service: S, limit: usize) -> Self {
98 BodyLimitLayer::response_only(limit).into_layer(service)
99 }
100
101 pub fn symmetric(service: S, limit: usize) -> Self {
105 BodyLimitLayer::symmetric(limit).into_layer(service)
106 }
107
108 pub fn asymmetric(service: S, request: usize, response: usize) -> Self {
113 BodyLimitLayer::asymmetric(request, response).into_layer(service)
114 }
115}
116
117impl<S, State, IO> Service<State, IO> for BodyLimitService<S>
118where
119 S: Service<State, IO>,
120 State: Clone + Send + Sync + 'static,
121 IO: Stream,
122{
123 type Response = S::Response;
124 type Error = S::Error;
125
126 async fn serve(
127 &self,
128 mut ctx: Context<State>,
129 stream: IO,
130 ) -> Result<Self::Response, Self::Error> {
131 ctx.insert(self.limit);
132 self.inner.serve(ctx, stream).await
133 }
134}
135
136impl<S> fmt::Debug for BodyLimitService<S>
137where
138 S: fmt::Debug,
139{
140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 f.debug_struct("BodyLimitService")
142 .field("inner", &self.inner)
143 .field("limit", &self.limit)
144 .finish()
145 }
146}