1use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use bytes::Bytes;
6use futures_core::Stream;
7use h2::RecvStream;
8
9mod dispatcher;
10mod service;
11
12pub use self::dispatcher::Dispatcher;
13pub use self::service::H2Service;
14use crate::error::PayloadError;
15
16pub struct Payload {
18 pl: RecvStream,
19}
20
21impl Payload {
22 pub(crate) fn new(pl: RecvStream) -> Self {
23 Self { pl }
24 }
25}
26
27impl Stream for Payload {
28 type Item = Result<Bytes, PayloadError>;
29
30 fn poll_next(
31 self: Pin<&mut Self>,
32 cx: &mut Context<'_>,
33 ) -> Poll<Option<Self::Item>> {
34 let this = self.get_mut();
35
36 match Pin::new(&mut this.pl).poll_data(cx) {
37 Poll::Ready(Some(Ok(chunk))) => {
38 let len = chunk.len();
39 if let Err(err) = this.pl.flow_control().release_capacity(len) {
40 Poll::Ready(Some(Err(err.into())))
41 } else {
42 Poll::Ready(Some(Ok(chunk)))
43 }
44 }
45 Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(err.into()))),
46 Poll::Pending => Poll::Pending,
47 Poll::Ready(None) => Poll::Ready(None),
48 }
49 }
50}