Skip to main content

netget/server/grpc/
http1.rs

1//! Shared affine body lifetime and HTTP header limits for native HTTP/1 RPC bindings.
2use http::HeaderMap;
3use http_body_util::{BodyExt, StreamBody};
4use tonic::{body::BoxBody, Status};
5
6pub fn hold_body<G: Send + 'static>(body: BoxBody, guard: G) -> BoxBody {
7    StreamBody::new(futures::stream::try_unfold(
8        (body, guard),
9        |(mut body, guard)| async move {
10            let frame = body.frame().await.transpose()?;
11            Ok::<_, Status>(frame.map(|frame| (frame, (body, guard))))
12        },
13    ))
14    .boxed_unsync()
15}
16pub fn bounded_headers(headers: &HeaderMap) -> bool {
17    headers.len() <= 64
18        && headers
19            .iter()
20            .try_fold(0usize, |total, (name, value)| {
21                total
22                    .checked_add(name.as_str().len())?
23                    .checked_add(value.as_bytes().len())?
24                    .checked_add(4)
25            })
26            .is_some_and(|total| total <= 32768)
27}