diff options
Diffstat (limited to '')
-rw-r--r-- | third_party/rust/http-body/src/lib.rs | 242 | ||||
-rw-r--r-- | third_party/rust/http-body/src/next.rs | 31 | ||||
-rw-r--r-- | third_party/rust/http-body/src/size_hint.rs | 86 |
3 files changed, 359 insertions, 0 deletions
diff --git a/third_party/rust/http-body/src/lib.rs b/third_party/rust/http-body/src/lib.rs new file mode 100644 index 0000000000..0d0e061d98 --- /dev/null +++ b/third_party/rust/http-body/src/lib.rs @@ -0,0 +1,242 @@ +#![doc(html_root_url = "https://docs.rs/http-body/0.3.1")] +#![deny(missing_debug_implementations, missing_docs, unreachable_pub)] +#![cfg_attr(test, deny(warnings))] + +//! Asynchronous HTTP request or response body. +//! +//! See [`Body`] for more details. +//! +//! [`Body`]: trait.Body.html + +mod next; +mod size_hint; + +pub use self::next::{Data, Trailers}; +pub use self::size_hint::SizeHint; + +use bytes::Buf; +use http::HeaderMap; +use std::ops; +use std::pin::Pin; +use std::task::{Context, Poll}; + +/// Trait representing a streaming body of a Request or Response. +/// +/// Data is streamed via the `poll_data` function, which asynchronously yields `T: Buf` values. The +/// `size_hint` function provides insight into the total number of bytes that will be streamed. +/// +/// The `poll_trailers` function returns an optional set of trailers used to finalize the request / +/// response exchange. This is mostly used when using the HTTP/2.0 protocol. +/// +pub trait Body { + /// Values yielded by the `Body`. + type Data: Buf; + + /// The error type this `Body` might generate. + type Error; + + /// Attempt to pull out the next data buffer of this stream. + fn poll_data( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Option<Result<Self::Data, Self::Error>>>; + + /// Poll for an optional **single** `HeaderMap` of trailers. + /// + /// This function should only be called once `poll_data` returns `None`. + fn poll_trailers( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Result<Option<HeaderMap>, Self::Error>>; + + /// Returns `true` when the end of stream has been reached. + /// + /// An end of stream means that both `poll_data` and `poll_trailers` will + /// return `None`. + /// + /// A return value of `false` **does not** guarantee that a value will be + /// returned from `poll_stream` or `poll_trailers`. + fn is_end_stream(&self) -> bool { + false + } + + /// Returns the bounds on the remaining length of the stream. + /// + /// When the **exact** remaining length of the stream is known, the upper bound will be set and + /// will equal the lower bound. + fn size_hint(&self) -> SizeHint { + SizeHint::default() + } + + /// Returns future that resolves to next data chunk, if any. + fn data(&mut self) -> Data<'_, Self> + where + Self: Unpin + Sized, + { + Data(self) + } + + /// Returns future that resolves to trailers, if any. + fn trailers(&mut self) -> Trailers<'_, Self> + where + Self: Unpin + Sized, + { + Trailers(self) + } +} + +impl<T: Body + Unpin + ?Sized> Body for &mut T { + type Data = T::Data; + type Error = T::Error; + + fn poll_data( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Option<Result<Self::Data, Self::Error>>> { + Pin::new(&mut **self).poll_data(cx) + } + + fn poll_trailers( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Result<Option<HeaderMap>, Self::Error>> { + Pin::new(&mut **self).poll_trailers(cx) + } + + fn is_end_stream(&self) -> bool { + Pin::new(&**self).is_end_stream() + } + + fn size_hint(&self) -> SizeHint { + Pin::new(&**self).size_hint() + } +} + +impl<P> Body for Pin<P> +where + P: Unpin + ops::DerefMut, + P::Target: Body, +{ + type Data = <<P as ops::Deref>::Target as Body>::Data; + type Error = <<P as ops::Deref>::Target as Body>::Error; + + fn poll_data( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Option<Result<Self::Data, Self::Error>>> { + Pin::get_mut(self).as_mut().poll_data(cx) + } + + fn poll_trailers( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Result<Option<HeaderMap>, Self::Error>> { + Pin::get_mut(self).as_mut().poll_trailers(cx) + } + + fn is_end_stream(&self) -> bool { + self.as_ref().is_end_stream() + } + + fn size_hint(&self) -> SizeHint { + self.as_ref().size_hint() + } +} + +impl<T: Body + Unpin + ?Sized> Body for Box<T> { + type Data = T::Data; + type Error = T::Error; + + fn poll_data( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Option<Result<Self::Data, Self::Error>>> { + Pin::new(&mut **self).poll_data(cx) + } + + fn poll_trailers( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Result<Option<HeaderMap>, Self::Error>> { + Pin::new(&mut **self).poll_trailers(cx) + } + + fn is_end_stream(&self) -> bool { + self.as_ref().is_end_stream() + } + + fn size_hint(&self) -> SizeHint { + self.as_ref().size_hint() + } +} + +impl<B: Body> Body for http::Request<B> { + type Data = B::Data; + type Error = B::Error; + + fn poll_data( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Option<Result<Self::Data, Self::Error>>> { + unsafe { + self.map_unchecked_mut(http::Request::body_mut) + .poll_data(cx) + } + } + + fn poll_trailers( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Result<Option<HeaderMap>, Self::Error>> { + unsafe { + self.map_unchecked_mut(http::Request::body_mut) + .poll_trailers(cx) + } + } + + fn is_end_stream(&self) -> bool { + self.body().is_end_stream() + } + + fn size_hint(&self) -> SizeHint { + self.body().size_hint() + } +} + +impl<B: Body> Body for http::Response<B> { + type Data = B::Data; + type Error = B::Error; + + fn poll_data( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Option<Result<Self::Data, Self::Error>>> { + unsafe { + self.map_unchecked_mut(http::Response::body_mut) + .poll_data(cx) + } + } + + fn poll_trailers( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll<Result<Option<HeaderMap>, Self::Error>> { + unsafe { + self.map_unchecked_mut(http::Response::body_mut) + .poll_trailers(cx) + } + } + + fn is_end_stream(&self) -> bool { + self.body().is_end_stream() + } + + fn size_hint(&self) -> SizeHint { + self.body().size_hint() + } +} + +#[cfg(test)] +fn _assert_bounds() { + fn can_be_trait_object(_: &dyn Body<Data = std::io::Cursor<Vec<u8>>, Error = std::io::Error>) {} +} diff --git a/third_party/rust/http-body/src/next.rs b/third_party/rust/http-body/src/next.rs new file mode 100644 index 0000000000..fc87ffcf01 --- /dev/null +++ b/third_party/rust/http-body/src/next.rs @@ -0,0 +1,31 @@ +use crate::Body; + +use core::future::Future; +use core::pin::Pin; +use core::task; + +#[must_use = "futures don't do anything unless polled"] +#[derive(Debug)] +/// Future that resolves to the next data chunk from `Body` +pub struct Data<'a, T: ?Sized>(pub(crate) &'a mut T); + +impl<'a, T: Body + Unpin + ?Sized> Future for Data<'a, T> { + type Output = Option<Result<T::Data, T::Error>>; + + fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> { + Pin::new(&mut self.0).poll_data(ctx) + } +} + +#[must_use = "futures don't do anything unless polled"] +#[derive(Debug)] +/// Future that resolves to the optional trailers from `Body` +pub struct Trailers<'a, T: ?Sized>(pub(crate) &'a mut T); + +impl<'a, T: Body + Unpin + ?Sized> Future for Trailers<'a, T> { + type Output = Result<Option<http::HeaderMap>, T::Error>; + + fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> { + Pin::new(&mut self.0).poll_trailers(ctx) + } +} diff --git a/third_party/rust/http-body/src/size_hint.rs b/third_party/rust/http-body/src/size_hint.rs new file mode 100644 index 0000000000..00a8f19177 --- /dev/null +++ b/third_party/rust/http-body/src/size_hint.rs @@ -0,0 +1,86 @@ +use std::u64; + +/// A `Body` size hint +/// +/// The default implementation returns: +/// +/// * 0 for `lower` +/// * `None` for `upper`. +#[derive(Debug, Default, Clone)] +pub struct SizeHint { + lower: u64, + upper: Option<u64>, +} + +impl SizeHint { + /// Returns a new `SizeHint` with default values + #[inline] + pub fn new() -> SizeHint { + SizeHint::default() + } + + /// Returns a new `SizeHint` with both upper and lower bounds set to the + /// given value. + #[inline] + pub fn with_exact(value: u64) -> SizeHint { + SizeHint { + lower: value, + upper: Some(value), + } + } + + /// Returns the lower bound of data that the `Body` will yield before + /// completing. + #[inline] + pub fn lower(&self) -> u64 { + self.lower + } + + /// Set the value of the `lower` hint. + /// + /// # Panics + /// + /// The function panics if `value` is greater than `upper`. + #[inline] + pub fn set_lower(&mut self, value: u64) { + assert!(value <= self.upper.unwrap_or(u64::MAX)); + self.lower = value; + } + + /// Returns the upper bound of data the `Body` will yield before + /// completing, or `None` if the value is unknown. + #[inline] + pub fn upper(&self) -> Option<u64> { + self.upper + } + + /// Set the value of the `upper` hint value. + /// + /// # Panics + /// + /// This function panics if `value` is less than `lower`. + #[inline] + pub fn set_upper(&mut self, value: u64) { + assert!(value >= self.lower, "`value` is less than than `lower`"); + + self.upper = Some(value); + } + + /// Returns the exact size of data that will be yielded **if** the + /// `lower` and `upper` bounds are equal. + #[inline] + pub fn exact(&self) -> Option<u64> { + if Some(self.lower) == self.upper { + self.upper + } else { + None + } + } + + /// Set the value of the `lower` and `upper` bounds to exactly the same. + #[inline] + pub fn set_exact(&mut self, value: u64) { + self.lower = value; + self.upper = Some(value); + } +} |