tez 0.0.3

A lightweight, ergonomic, and fast high-level HTTP client for Rust
Documentation
use std::{
    pin::Pin,
    sync::{Arc, Mutex, PoisonError},
    task::{Context, Poll},
};

use bytes::{Bytes, BytesMut};
use futures_io::AsyncRead;
#[cfg(any(
    feature = "brotli",
    feature = "deflate",
    feature = "gzip",
    feature = "zstd"
))]
use futures_util::io::BufReader;
use http::{
    HeaderMap,
    header::{CONTENT_ENCODING, CONTENT_LENGTH},
};
use http_body::Body as _;

use crate::{Body, Error};

const READ_CHUNK: usize = 32 * 1024;

type Reader = Pin<Box<dyn AsyncRead + Send>>;

pub(crate) fn accepted() -> Option<http::HeaderValue> {
    let encodings: &[&str] = &[
        #[cfg(feature = "gzip")]
        "gzip",
        #[cfg(feature = "brotli")]
        "br",
        #[cfg(feature = "deflate")]
        "deflate",
        #[cfg(feature = "zstd")]
        "zstd",
    ];
    (!encodings.is_empty()).then(|| {
        http::HeaderValue::from_str(&encodings.join(", "))
            .expect("compiled content encodings are valid header tokens")
    })
}

pub(crate) fn response(headers: &mut HeaderMap, body: Body) -> Body {
    let mut encodings = Vec::new();
    for value in headers.get_all(CONTENT_ENCODING) {
        let Ok(value) = value.to_str() else {
            return body;
        };
        encodings.extend(
            value
                .split(',')
                .map(str::trim)
                .filter(|encoding| !encoding.eq_ignore_ascii_case("identity")),
        );
    }
    if encodings.is_empty() || encodings.iter().any(|encoding| !supported(encoding)) {
        return body;
    }

    let source = Arc::new(Mutex::new(Source::new(body)));
    let mut reader: Reader = Box::pin(BodyReader::new(Arc::clone(&source)));
    for encoding in encodings.into_iter().rev() {
        reader = decoder(encoding, reader);
    }
    headers.remove(CONTENT_ENCODING);
    headers.remove(CONTENT_LENGTH);
    Body::from_http_body(ReaderBody::new(reader, source), |error| error)
}

fn supported(encoding: &str) -> bool {
    match encoding {
        #[cfg(feature = "gzip")]
        encoding if encoding.eq_ignore_ascii_case("gzip") => true,
        #[cfg(feature = "brotli")]
        encoding if encoding.eq_ignore_ascii_case("br") => true,
        #[cfg(feature = "deflate")]
        encoding if encoding.eq_ignore_ascii_case("deflate") => true,
        #[cfg(feature = "zstd")]
        encoding if encoding.eq_ignore_ascii_case("zstd") => true,
        _ => false,
    }
}

fn decoder(encoding: &str, reader: Reader) -> Reader {
    #[cfg(not(any(
        feature = "brotli",
        feature = "deflate",
        feature = "gzip",
        feature = "zstd"
    )))]
    {
        let _ = (encoding, reader);
        unreachable!("no content decoders were compiled")
    }

    #[cfg(any(
        feature = "brotli",
        feature = "deflate",
        feature = "gzip",
        feature = "zstd"
    ))]
    match encoding {
        #[cfg(feature = "gzip")]
        encoding if encoding.eq_ignore_ascii_case("gzip") => Box::pin(
            async_compression::futures::bufread::GzipDecoder::new(BufReader::new(reader)),
        ),
        #[cfg(feature = "brotli")]
        encoding if encoding.eq_ignore_ascii_case("br") => Box::pin(
            async_compression::futures::bufread::BrotliDecoder::new(BufReader::new(reader)),
        ),
        #[cfg(feature = "deflate")]
        encoding if encoding.eq_ignore_ascii_case("deflate") => Box::pin(
            async_compression::futures::bufread::ZlibDecoder::new(BufReader::new(reader)),
        ),
        #[cfg(feature = "zstd")]
        encoding if encoding.eq_ignore_ascii_case("zstd") => Box::pin(
            async_compression::futures::bufread::ZstdDecoder::new(BufReader::new(reader)),
        ),
        _ => unreachable!("unsupported encodings are rejected before decoder construction"),
    }
}

struct BodyReader {
    source: Arc<Mutex<Source>>,
}

struct Source {
    body: Body,
    data: Bytes,
    offset: usize,
    finished: bool,
    trailers: Option<HeaderMap>,
}

impl Source {
    fn new(body: Body) -> Self {
        Self {
            body,
            data: Bytes::new(),
            offset: 0,
            finished: false,
            trailers: None,
        }
    }

    fn poll_read(
        &mut self,
        context: &mut Context<'_>,
        output: &mut [u8],
    ) -> Poll<std::io::Result<usize>> {
        loop {
            if self.offset < self.data.len() {
                let length = (self.data.len() - self.offset).min(output.len());
                output[..length].copy_from_slice(&self.data[self.offset..self.offset + length]);
                self.offset += length;
                return Poll::Ready(Ok(length));
            }
            if self.finished {
                return Poll::Ready(Ok(0));
            }

            match Pin::new(&mut self.body).poll_frame(context) {
                Poll::Ready(Some(Ok(frame))) => match frame.into_data() {
                    Ok(data) => {
                        self.data = data;
                        self.offset = 0;
                    }
                    Err(frame) => {
                        if let Ok(trailers) = frame.into_trailers() {
                            self.trailers = Some(trailers);
                            self.finished = true;
                            return Poll::Ready(Ok(0));
                        }
                    }
                },
                Poll::Ready(Some(Err(error))) => {
                    self.finished = true;
                    return Poll::Ready(Err(std::io::Error::other(error)));
                }
                Poll::Ready(None) => {
                    self.finished = true;
                    return Poll::Ready(Ok(0));
                }
                Poll::Pending => return Poll::Pending,
            }
        }
    }

    fn poll_trailers(
        &mut self,
        context: &mut Context<'_>,
    ) -> Poll<Result<Option<HeaderMap>, Error>> {
        if self.offset < self.data.len() {
            return Poll::Ready(Err(Error::decode(
                "HTTP response content encoding is invalid",
            )));
        }
        if self.finished {
            return Poll::Ready(Ok(self.trailers.take()));
        }
        loop {
            match Pin::new(&mut self.body).poll_frame(context) {
                Poll::Ready(Some(Ok(frame))) => match frame.into_trailers() {
                    Ok(trailers) => {
                        self.finished = true;
                        return Poll::Ready(Ok(Some(trailers)));
                    }
                    Err(frame) if frame.data_ref().is_some_and(Bytes::is_empty) => {}
                    Err(_) => {
                        self.finished = true;
                        return Poll::Ready(Err(Error::decode(
                            "HTTP response content encoding is invalid",
                        )));
                    }
                },
                Poll::Ready(Some(Err(error))) => {
                    self.finished = true;
                    return Poll::Ready(Err(error));
                }
                Poll::Ready(None) => {
                    self.finished = true;
                    return Poll::Ready(Ok(None));
                }
                Poll::Pending => return Poll::Pending,
            }
        }
    }
}

impl BodyReader {
    fn new(source: Arc<Mutex<Source>>) -> Self {
        Self { source }
    }
}

impl AsyncRead for BodyReader {
    fn poll_read(
        self: Pin<&mut Self>,
        context: &mut Context<'_>,
        output: &mut [u8],
    ) -> Poll<std::io::Result<usize>> {
        self.source
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .poll_read(context, output)
    }
}

struct ReaderBody {
    reader: Reader,
    buffer: BytesMut,
    finished: bool,
    source: Arc<Mutex<Source>>,
}

impl ReaderBody {
    fn new(reader: Reader, source: Arc<Mutex<Source>>) -> Self {
        Self {
            reader,
            buffer: BytesMut::zeroed(READ_CHUNK),
            finished: false,
            source,
        }
    }
}

impl http_body::Body for ReaderBody {
    type Data = Bytes;
    type Error = Error;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        context: &mut Context<'_>,
    ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
        if self.finished {
            return Poll::Ready(None);
        }
        let this = &mut *self;
        this.buffer.resize(READ_CHUNK, 0);
        match this.reader.as_mut().poll_read(context, &mut this.buffer) {
            Poll::Ready(Ok(0)) => {
                match this
                    .source
                    .lock()
                    .unwrap_or_else(PoisonError::into_inner)
                    .poll_trailers(context)
                {
                    Poll::Ready(Ok(trailers)) => {
                        this.finished = true;
                        Poll::Ready(trailers.map(http_body::Frame::trailers).map(Ok))
                    }
                    Poll::Ready(Err(error)) => {
                        this.finished = true;
                        Poll::Ready(Some(Err(error)))
                    }
                    Poll::Pending => Poll::Pending,
                }
            }
            Poll::Ready(Ok(read)) => {
                this.buffer.truncate(read);
                Poll::Ready(Some(Ok(http_body::Frame::data(
                    this.buffer.split().freeze(),
                ))))
            }
            Poll::Ready(Err(error)) => {
                this.finished = true;
                Poll::Ready(Some(Err(decompression_error(&error))))
            }
            Poll::Pending => Poll::Pending,
        }
    }

    fn is_end_stream(&self) -> bool {
        self.finished
    }
}

fn decompression_error(error: &std::io::Error) -> Error {
    error
        .get_ref()
        .and_then(|error| error.downcast_ref::<Error>())
        .cloned()
        .unwrap_or_else(|| Error::decode("HTTP response content encoding is invalid"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn advertised_encodings_match_compiled_features() {
        let accepted = accepted().map(|value| value.to_str().unwrap().to_owned());
        assert_eq!(
            accepted.is_some(),
            cfg!(any(
                feature = "gzip",
                feature = "brotli",
                feature = "deflate",
                feature = "zstd"
            ))
        );
    }

    #[test]
    fn transport_errors_survive_the_decoder_boundary() {
        let timeout = std::io::Error::other(Error::timeout());
        assert!(decompression_error(&timeout).is_timeout());

        let invalid = std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid stream");
        assert!(decompression_error(&invalid).is_decode());
    }
}