aboutsummaryrefslogtreecommitdiff
path: root/src/h3_support.rs
blob: c3e8866c8272bfcc9992ca5e16d639cf29f436e2 (plain)
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
/*
    This file is part of gnix (https://codeberg.org/metamuffin/gnix)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use crate::error::ServiceError;
use bytes::{Buf, Bytes};
use h3::server::RequestStream;
use h3_quinn::RecvStream;
use hyper::body::{Body, Frame};
use std::{
    pin::Pin,
    task::{Context, Poll},
};

pub struct H3RequestBody(pub RequestStream<RecvStream, Bytes>);

impl Body for H3RequestBody {
    type Data = Bytes;
    type Error = ServiceError;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
        match self.0.poll_recv_data(cx) {
            Poll::Ready(Ok(Some(mut f))) => {
                Poll::Ready(Some(Ok(Frame::data(f.copy_to_bytes(f.remaining())))))
            }
            Poll::Ready(Ok(None)) => Poll::Ready(None),
            Poll::Ready(Err(e)) => Poll::Ready(Some(Err(ServiceError::H3Stream(e)))),
            Poll::Pending => Poll::Pending,
        }
    }
}