Skip to content

Commit 56e9616

Browse files
author
Gopa Kumar
committed
expose poll_data equivalent of recv_data
Useful to have this exposed for example to implement an AsyncRead on the stream. poll_data does exactly what recv_data was doing, recv_data now just calls poll_fn(poll_data)
1 parent 5c16195 commit 56e9616

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

h3/src/client.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,11 @@ where
687687
self.inner.recv_data().await
688688
}
689689

690+
/// Poll for data sent from the server.
691+
pub fn poll_data(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<impl Buf>, Error>> {
692+
self.inner.poll_data(cx)
693+
}
694+
690695
/// Receive an optional set of trailers for the response.
691696
pub async fn recv_trailers(&mut self) -> Result<Option<HeaderMap>, Error> {
692697
let res = self.inner.recv_trailers().await;

h3/src/connection.rs

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -622,47 +622,57 @@ impl<S, B> RequestStream<S, B>
622622
where
623623
S: quic::RecvStream,
624624
{
625-
/// Receive some of the request body.
626-
pub async fn recv_data(&mut self) -> Result<Option<impl Buf>, Error> {
625+
pub fn poll_data(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<impl Buf>, Error>> {
627626
if !self.stream.has_data() {
628-
let frame = future::poll_fn(|cx| self.stream.poll_next(cx))
629-
.await
630-
.map_err(|e| self.maybe_conn_err(e))?;
631-
match frame {
632-
Some(Frame::Data { .. }) => (),
633-
Some(Frame::Headers(encoded)) => {
634-
self.trailers = Some(encoded);
635-
return Ok(None);
636-
}
627+
match self.stream.poll_next(cx) {
628+
Poll::Ready(Ok(frame)) => {
629+
match frame {
630+
Some(Frame::Data { .. }) => (),
631+
Some(Frame::Headers(encoded)) => {
632+
self.trailers = Some(encoded);
633+
return Poll::Ready(Ok(None));
634+
}
637635

638-
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.1
639-
//# Receipt of an invalid sequence of frames MUST be treated as a
640-
//# connection error of type H3_FRAME_UNEXPECTED.
636+
//= https://www.rfc-editor.org/rfc/rfc9114#section-4.1
637+
//# Receipt of an invalid sequence of frames MUST be treated as a
638+
//# connection error of type H3_FRAME_UNEXPECTED.
641639

642-
//= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.3
643-
//# Receiving a
644-
//# CANCEL_PUSH frame on a stream other than the control stream MUST be
645-
//# treated as a connection error of type H3_FRAME_UNEXPECTED.
640+
//= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.3
641+
//# Receiving a
642+
//# CANCEL_PUSH frame on a stream other than the control stream MUST be
643+
//# treated as a connection error of type H3_FRAME_UNEXPECTED.
646644

647-
//= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.4
648-
//# If an endpoint receives a SETTINGS frame on a different
649-
//# stream, the endpoint MUST respond with a connection error of type
650-
//# H3_FRAME_UNEXPECTED.
651-
652-
//= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.6
653-
//# A client MUST treat a GOAWAY frame on a stream other than
654-
//# the control stream as a connection error of type H3_FRAME_UNEXPECTED.
655-
656-
//= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.7
657-
//# The MAX_PUSH_ID frame is always sent on the control stream. Receipt
658-
//# of a MAX_PUSH_ID frame on any other stream MUST be treated as a
659-
//# connection error of type H3_FRAME_UNEXPECTED.
660-
Some(_) => return Err(Code::H3_FRAME_UNEXPECTED.into()),
661-
None => return Ok(None),
645+
//= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.4
646+
//# If an endpoint receives a SETTINGS frame on a different
647+
//# stream, the endpoint MUST respond with a connection error of type
648+
//# H3_FRAME_UNEXPECTED.
649+
650+
//= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.6
651+
//# A client MUST treat a GOAWAY frame on a stream other than
652+
//# the control stream as a connection error of type H3_FRAME_UNEXPECTED.
653+
654+
//= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.7
655+
//# The MAX_PUSH_ID frame is always sent on the control stream. Receipt
656+
//# of a MAX_PUSH_ID frame on any other stream MUST be treated as a
657+
//# connection error of type H3_FRAME_UNEXPECTED.
658+
Some(_) => return Poll::Ready(Err(Code::H3_FRAME_UNEXPECTED.into())),
659+
None => return Poll::Ready(Ok(None)),
660+
}
661+
}
662+
Poll::Ready(Err(e)) => return Poll::Ready(Err(self.maybe_conn_err(e))),
663+
Poll::Pending => return Poll::Pending,
662664
}
663665
}
666+
match self.stream.poll_data(cx) {
667+
Poll::Ready(Ok(data)) => Poll::Ready(Ok(data)),
668+
Poll::Ready(Err(e)) => Poll::Ready(Err(self.maybe_conn_err(e))),
669+
Poll::Pending => Poll::Pending,
670+
}
671+
}
664672

665-
let data = future::poll_fn(|cx| self.stream.poll_data(cx))
673+
/// Receive some of the request body.
674+
pub async fn recv_data(&mut self) -> Result<Option<impl Buf>, Error> {
675+
let data = future::poll_fn(|cx| self.poll_data(cx))
666676
.await
667677
.map_err(|e| self.maybe_conn_err(e))?;
668678
Ok(data)

h3/src/server.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,11 @@ where
674674
self.inner.recv_data().await
675675
}
676676

677+
/// Poll for data sent from the client
678+
pub fn poll_data(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<impl Buf>, Error>> {
679+
self.inner.poll_data(cx)
680+
}
681+
677682
/// Receive an optional set of trailers for the request
678683
pub async fn recv_trailers(&mut self) -> Result<Option<HeaderMap>, Error> {
679684
self.inner.recv_trailers().await

0 commit comments

Comments
 (0)