use std::{
convert::Infallible,
error::Error as StdError,
pin::Pin,
task::{Context, Poll},
};
use bytes::Bytes;
use futures_io::AsyncRead;
use futures_util::{Stream, StreamExt as _};
use http_body::{Body as HttpBody, Frame, SizeHint};
use http_body_util::{BodyExt as _, StreamBody, combinators::UnsyncBoxBody};
use pin_project_lite::pin_project;
use crate::Error;
pub(crate) enum Kind {
Empty,
Buffered(Option<Bytes>),
Streaming(UnsyncBoxBody<Bytes, Error>),
}
pub(crate) enum Replayable {
Empty,
Buffered(Bytes),
}
pub struct Body {
kind: Kind,
}
impl Body {
#[must_use]
pub const fn empty() -> Self {
Self { kind: Kind::Empty }
}
#[must_use]
pub fn wrap<B>(body: B) -> Self
where
B: HttpBody + Send + 'static,
B::Data: Into<Bytes>,
B::Error: StdError + Send + Sync + 'static,
{
Self::from_http_body(body, |_| Error::body())
}
#[must_use]
pub fn stream<S, D, E>(stream: S) -> Self
where
S: Stream<Item = Result<D, E>> + Send + 'static,
D: Into<Bytes>,
E: StdError + Send + Sync + 'static,
{
let frames = stream.map(|chunk| chunk.map(|chunk| Frame::data(chunk.into())));
Self::from_http_body(StreamBody::new(frames), |_| Error::body())
}
#[must_use]
pub fn stream_with_length<S, D, E>(stream: S, length: u64) -> Self
where
S: Stream<Item = Result<D, E>> + Send + 'static,
D: Into<Bytes>,
E: StdError + Send + Sync + 'static,
{
Self::stream(stream).with_exact_length(length)
}
#[must_use]
pub fn reader<R>(reader: R) -> Self
where
R: AsyncRead + Send + Unpin + 'static,
{
Self::from_http_body(ReaderBody::new(reader, None), |error| error)
}
#[must_use]
pub fn reader_with_length<R>(reader: R, length: u64) -> Self
where
R: AsyncRead + Send + Unpin + 'static,
{
Self::from_http_body(ReaderBody::new(reader, Some(length)), |error| error)
}
pub(crate) fn from_http_body<B, F>(body: B, map_error: F) -> Self
where
B: HttpBody + Send + 'static,
B::Data: Into<Bytes>,
F: FnMut(B::Error) -> Error + Send + 'static,
{
let remaining = body.size_hint();
let body = HintedBody {
inner: body,
map_error,
remaining,
finished: false,
}
.boxed_unsync();
Self {
kind: Kind::Streaming(body),
}
}
#[must_use]
pub fn as_bytes(&self) -> Option<&[u8]> {
match &self.kind {
Kind::Empty | Kind::Buffered(None) => Some(&[]),
Kind::Buffered(Some(bytes)) => Some(bytes),
Kind::Streaming(_) => None,
}
}
pub(crate) fn into_replayable(self) -> Result<Replayable, Self> {
match self.kind {
Kind::Empty => Ok(Replayable::Empty),
Kind::Buffered(Some(bytes)) => Ok(Replayable::Buffered(bytes)),
Kind::Buffered(None) => Ok(Replayable::Buffered(Bytes::new())),
Kind::Streaming(body) => Err(Self::from_kind(Kind::Streaming(body))),
}
}
pub(crate) fn into_buffered_bytes(self) -> Result<Bytes, Self> {
match self.kind {
Kind::Empty | Kind::Buffered(None) => Ok(Bytes::new()),
Kind::Buffered(Some(bytes)) => Ok(bytes),
Kind::Streaming(body) => Err(Self::from_kind(Kind::Streaming(body))),
}
}
pub(crate) const fn from_kind(kind: Kind) -> Self {
Self { kind }
}
pub(crate) fn with_exact_length(self, length: u64) -> Self {
self.with_length(length, LengthMode::Truncate)
}
pub(crate) fn validate_length(self, length: u64) -> Self {
self.with_length(length, LengthMode::Validate)
}
fn with_length(self, length: u64, mode: LengthMode) -> Self {
Self::from_http_body(
ExactBody {
inner: self,
remaining: length,
finished: false,
mode,
},
|error| error,
)
}
}
impl Replayable {
pub(crate) fn body(&self) -> Body {
match self {
Self::Empty => Body::empty(),
Self::Buffered(bytes) => Body::from(bytes.clone()),
}
}
}
impl Default for Body {
fn default() -> Self {
Self::empty()
}
}
impl From<Bytes> for Body {
fn from(bytes: Bytes) -> Self {
Self {
kind: Kind::Buffered(Some(bytes)),
}
}
}
impl From<Vec<u8>> for Body {
fn from(bytes: Vec<u8>) -> Self {
Self::from(Bytes::from(bytes))
}
}
impl From<String> for Body {
fn from(text: String) -> Self {
Self::from(text.into_bytes())
}
}
impl From<&str> for Body {
fn from(text: &str) -> Self {
Self::from(text.as_bytes())
}
}
impl From<&[u8]> for Body {
fn from(bytes: &[u8]) -> Self {
Self::from(bytes.to_vec())
}
}
impl Unpin for Body {}
struct ReaderBody<R> {
reader: R,
buffer: Vec<u8>,
remaining: Option<u64>,
finished: bool,
}
impl<R> ReaderBody<R> {
fn new(reader: R, remaining: Option<u64>) -> Self {
Self {
reader,
buffer: vec![0; 16 * 1024],
remaining,
finished: false,
}
}
}
impl<R> HttpBody for ReaderBody<R>
where
R: AsyncRead + Unpin,
{
type Data = Bytes;
type Error = Error;
fn poll_frame(
self: Pin<&mut Self>,
context: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
let this = self.get_mut();
if this.finished {
return Poll::Ready(None);
}
let allowed = this
.remaining
.and_then(|remaining| usize::try_from(remaining).ok())
.map_or(this.buffer.len(), |remaining| {
remaining.min(this.buffer.len())
});
if allowed == 0 {
this.finished = true;
return Poll::Ready(None);
}
match Pin::new(&mut this.reader).poll_read(context, &mut this.buffer[..allowed]) {
Poll::Ready(Ok(0)) => {
this.finished = true;
if this.remaining.is_some_and(|remaining| remaining > 0) {
Poll::Ready(Some(Err(Error::body())))
} else {
Poll::Ready(None)
}
}
Poll::Ready(Ok(read)) => {
if let Some(remaining) = &mut this.remaining {
*remaining = remaining.saturating_sub(read as u64);
}
Poll::Ready(Some(Ok(Frame::data(Bytes::copy_from_slice(
&this.buffer[..read],
)))))
}
Poll::Ready(Err(_)) => {
this.finished = true;
Poll::Ready(Some(Err(Error::body())))
}
Poll::Pending => Poll::Pending,
}
}
fn is_end_stream(&self) -> bool {
self.finished || self.remaining == Some(0)
}
fn size_hint(&self) -> SizeHint {
self.remaining
.map_or_else(SizeHint::new, SizeHint::with_exact)
}
}
pin_project! {
struct HintedBody<B, F> {
#[pin]
inner: B,
map_error: F,
remaining: SizeHint,
finished: bool,
}
}
struct ExactBody {
inner: Body,
remaining: u64,
finished: bool,
mode: LengthMode,
}
#[derive(Clone, Copy, Eq, PartialEq)]
enum LengthMode {
Truncate,
Validate,
}
impl HttpBody for ExactBody {
type Data = Bytes;
type Error = Error;
fn poll_frame(
self: Pin<&mut Self>,
context: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
let this = self.get_mut();
if this.finished {
return Poll::Ready(None);
}
if this.remaining == 0 {
if this.mode == LengthMode::Truncate {
this.finished = true;
return Poll::Ready(None);
}
loop {
match Pin::new(&mut this.inner).poll_frame(context) {
Poll::Ready(Some(Ok(frame))) => match frame.into_data() {
Ok(data) if data.is_empty() => {}
Ok(_) => {
this.finished = true;
return Poll::Ready(Some(Err(Error::body())));
}
Err(frame) => return Poll::Ready(Some(Ok(frame))),
},
Poll::Ready(Some(Err(error))) => {
this.finished = true;
return Poll::Ready(Some(Err(error)));
}
Poll::Ready(None) => {
this.finished = true;
return Poll::Ready(None);
}
Poll::Pending => return Poll::Pending,
}
}
}
loop {
match Pin::new(&mut this.inner).poll_frame(context) {
Poll::Ready(Some(Ok(frame))) => match frame.into_data() {
Ok(data) if data.is_empty() => {}
Ok(data) => {
let allowed = usize::try_from(this.remaining)
.unwrap_or(usize::MAX)
.min(data.len());
if this.mode == LengthMode::Validate && allowed < data.len() {
this.finished = true;
return Poll::Ready(Some(Err(Error::body())));
}
this.remaining -= allowed as u64;
return Poll::Ready(Some(Ok(Frame::data(data.slice(..allowed)))));
}
Err(frame) => return Poll::Ready(Some(Ok(frame))),
},
Poll::Ready(Some(Err(error))) => {
this.finished = true;
return Poll::Ready(Some(Err(error)));
}
Poll::Ready(None) => {
this.finished = true;
return Poll::Ready(Some(Err(Error::body())));
}
Poll::Pending => return Poll::Pending,
}
}
}
fn is_end_stream(&self) -> bool {
self.finished || self.mode == LengthMode::Truncate && self.remaining == 0
}
fn size_hint(&self) -> SizeHint {
SizeHint::with_exact(self.remaining)
}
}
impl<B, F> HttpBody for HintedBody<B, F>
where
B: HttpBody,
B::Data: Into<Bytes>,
F: FnMut(B::Error) -> Error,
{
type Data = Bytes;
type Error = Error;
fn poll_frame(
self: Pin<&mut Self>,
context: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
let mut this = self.project();
if *this.finished {
return Poll::Ready(None);
}
match this.inner.as_mut().poll_frame(context) {
Poll::Ready(Some(Ok(frame))) => {
let frame = frame.map_data(Into::into);
if let Some(data) = frame.data_ref() {
let consumed = data.len() as u64;
let lower = this.remaining.lower().saturating_sub(consumed);
let upper = this
.remaining
.upper()
.map(|upper| upper.saturating_sub(consumed));
this.remaining.set_lower(lower);
if let Some(upper) = upper {
this.remaining.set_upper(upper);
}
}
Poll::Ready(Some(Ok(frame)))
}
Poll::Ready(Some(Err(error))) => {
*this.finished = true;
*this.remaining = SizeHint::with_exact(0);
Poll::Ready(Some(Err((this.map_error)(error))))
}
Poll::Ready(None) => {
*this.finished = true;
*this.remaining = SizeHint::with_exact(0);
Poll::Ready(None)
}
Poll::Pending => Poll::Pending,
}
}
fn is_end_stream(&self) -> bool {
self.finished || self.inner.is_end_stream()
}
fn size_hint(&self) -> SizeHint {
self.remaining
}
}
impl HttpBody for Body {
type Data = Bytes;
type Error = Error;
fn poll_frame(
self: Pin<&mut Self>,
context: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
match &mut self.get_mut().kind {
Kind::Empty => Poll::Ready(None),
Kind::Buffered(bytes) => Poll::Ready(bytes.take().map(Frame::data).map(Ok)),
Kind::Streaming(body) => Pin::new(body).poll_frame(context),
}
}
fn is_end_stream(&self) -> bool {
match &self.kind {
Kind::Empty => true,
Kind::Buffered(bytes) => bytes.is_none(),
Kind::Streaming(body) => body.is_end_stream(),
}
}
fn size_hint(&self) -> SizeHint {
match &self.kind {
Kind::Buffered(Some(bytes)) => SizeHint::with_exact(bytes.len() as u64),
Kind::Empty | Kind::Buffered(None) => SizeHint::with_exact(0),
Kind::Streaming(body) => body.size_hint(),
}
}
}
impl From<Infallible> for Error {
fn from(never: Infallible) -> Self {
match never {}
}
}
#[cfg(test)]
mod tests {
use super::*;
use http_body_util::Full;
use std::io;
#[tokio::test]
async fn bytes_body_yields_one_data_frame() {
let mut body = Body::from("tez");
assert_eq!(body.as_bytes(), Some(&b"tez"[..]));
assert_eq!(body.size_hint().exact(), Some(3));
assert_eq!(
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"tez"
);
assert!(body.frame().await.is_none());
assert_eq!(body.as_bytes(), Some(&[][..]));
assert!(body.is_end_stream());
}
#[tokio::test]
async fn wrapped_http_body_preserves_standard_frames() {
let mut body = Body::wrap(Full::new(Bytes::from_static(b"stream")));
assert_eq!(body.as_bytes(), None);
assert_eq!(body.size_hint().exact(), Some(6));
assert_eq!(
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"stream"
);
assert_eq!(body.size_hint().exact(), Some(0));
assert!(body.frame().await.is_none());
}
#[tokio::test]
async fn an_exact_reader_reports_early_eof_and_stops_at_its_length() {
let mut exact =
Body::reader_with_length(futures_util::io::Cursor::new(b"tez!".to_vec()), 3);
assert_eq!(
exact.frame().await.unwrap().unwrap().into_data().unwrap(),
"tez"
);
assert!(exact.frame().await.is_none());
let mut short = Body::reader_with_length(futures_util::io::Cursor::new(b"te".to_vec()), 3);
assert!(short.frame().await.unwrap().is_ok());
assert!(short.frame().await.unwrap().unwrap_err().is_body());
}
#[tokio::test]
async fn byte_streams_preserve_chunks_and_enforce_an_exact_length() {
let chunks = futures_util::stream::iter([
Ok::<_, io::Error>(Bytes::from_static(b"one")),
Ok(Bytes::from_static(b"two")),
]);
let mut body = Body::stream(chunks);
assert_eq!(
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"one"
);
assert_eq!(
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"two"
);
assert!(body.frame().await.is_none());
let chunks = futures_util::stream::iter([
Ok::<_, io::Error>(Bytes::from_static(b"one")),
Ok(Bytes::from_static(b"two")),
]);
let mut exact = Body::stream_with_length(chunks, 5);
assert_eq!(exact.size_hint().exact(), Some(5));
assert_eq!(
exact.frame().await.unwrap().unwrap().into_data().unwrap(),
"one"
);
assert_eq!(
exact.frame().await.unwrap().unwrap().into_data().unwrap(),
"tw"
);
assert!(exact.frame().await.is_none());
let chunks = futures_util::stream::iter([Ok::<_, io::Error>(Bytes::from_static(b"one"))]);
let mut short = Body::stream_with_length(chunks, 4);
assert!(short.frame().await.unwrap().is_ok());
assert!(short.frame().await.unwrap().unwrap_err().is_body());
}
#[tokio::test]
async fn byte_streams_end_after_their_first_error() {
let chunks = futures_util::stream::iter([
Ok::<_, io::Error>(Bytes::from_static(b"before")),
Err(io::Error::other("failed")),
Ok(Bytes::from_static(b"after")),
]);
let mut body = Body::stream(chunks);
assert!(body.frame().await.unwrap().is_ok());
assert!(body.frame().await.unwrap().unwrap_err().is_body());
assert!(body.frame().await.is_none());
}
#[tokio::test]
async fn validated_lengths_reject_a_source_that_breaks_its_size_contract() {
let chunks = futures_util::stream::iter([Ok::<_, io::Error>(Bytes::from_static(b"long"))]);
let mut body = Body::stream(chunks).validate_length(3);
assert!(body.frame().await.unwrap().unwrap_err().is_body());
assert!(body.frame().await.is_none());
let chunks = futures_util::stream::iter([
Ok::<_, io::Error>(Bytes::from_static(b"one")),
Ok(Bytes::from_static(b"more")),
]);
let mut body = Body::stream(chunks).validate_length(3);
assert_eq!(
body.frame().await.unwrap().unwrap().into_data().unwrap(),
"one"
);
assert!(body.frame().await.unwrap().unwrap_err().is_body());
assert!(body.frame().await.is_none());
}
}