use crate::client::{HttpError, HttpErrorKind};
use crate::{PutPayload, collect_bytes};
use bytes::Bytes;
use futures_util::StreamExt;
use futures_util::stream::BoxStream;
use http_body_util::combinators::BoxBody;
use http_body_util::{BodyExt, Full};
use hyper::body::{Body, Frame, SizeHint};
use std::pin::Pin;
use std::task::{Context, Poll};
pub type HttpRequest = http::Request<HttpRequestBody>;
#[derive(Debug, Clone)]
pub struct HttpRequestBody(Inner);
impl HttpRequestBody {
pub fn empty() -> Self {
Self(Inner::Bytes(Bytes::new()))
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn into_reqwest(self) -> reqwest::Body {
match self.0 {
Inner::Bytes(b) => b.into(),
Inner::PutPayload(_, payload) => reqwest::Body::wrap_stream(
futures_util::stream::iter(payload.into_iter().map(Ok::<_, HttpError>)),
),
}
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
pub(crate) fn into_reqwest(self) -> reqwest::Body {
match self.0 {
Inner::Bytes(b) => b.into(),
Inner::PutPayload(_, payload) => Bytes::from(payload).into(),
}
}
pub fn is_empty(&self) -> bool {
match &self.0 {
Inner::Bytes(x) => x.is_empty(),
Inner::PutPayload(_, x) => x.iter().any(|x| !x.is_empty()),
}
}
pub fn content_length(&self) -> usize {
match &self.0 {
Inner::Bytes(x) => x.len(),
Inner::PutPayload(_, x) => x.content_length(),
}
}
pub fn as_bytes(&self) -> Option<&Bytes> {
match &self.0 {
Inner::Bytes(x) => Some(x),
_ => None,
}
}
}
impl From<Bytes> for HttpRequestBody {
fn from(value: Bytes) -> Self {
Self(Inner::Bytes(value))
}
}
impl From<Vec<u8>> for HttpRequestBody {
fn from(value: Vec<u8>) -> Self {
Self(Inner::Bytes(value.into()))
}
}
impl From<String> for HttpRequestBody {
fn from(value: String) -> Self {
Self(Inner::Bytes(value.into()))
}
}
impl From<PutPayload> for HttpRequestBody {
fn from(value: PutPayload) -> Self {
Self(Inner::PutPayload(0, value))
}
}
#[derive(Debug, Clone)]
enum Inner {
Bytes(Bytes),
PutPayload(usize, PutPayload),
}
impl Body for HttpRequestBody {
type Data = Bytes;
type Error = HttpError;
fn poll_frame(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
Poll::Ready(match &mut self.0 {
Inner::Bytes(bytes) => {
let out = bytes.split_off(0);
if out.is_empty() {
None
} else {
Some(Ok(Frame::data(out)))
}
}
Inner::PutPayload(offset, payload) => {
let slice = payload.as_ref();
if *offset == slice.len() {
None
} else {
Some(Ok(Frame::data(
slice[std::mem::replace(offset, *offset + 1)].clone(),
)))
}
}
})
}
fn is_end_stream(&self) -> bool {
match self.0 {
Inner::Bytes(ref bytes) => bytes.is_empty(),
Inner::PutPayload(offset, ref body) => offset == body.as_ref().len(),
}
}
fn size_hint(&self) -> SizeHint {
match self.0 {
Inner::Bytes(ref bytes) => SizeHint::with_exact(bytes.len() as u64),
Inner::PutPayload(offset, ref payload) => {
let iter = payload.as_ref().iter().skip(offset);
SizeHint::with_exact(iter.map(|x| x.len() as u64).sum())
}
}
}
}
pub type HttpResponse = http::Response<HttpResponseBody>;
#[derive(Debug)]
pub struct HttpResponseBody(BoxBody<Bytes, HttpError>);
impl HttpResponseBody {
pub fn new<B>(body: B) -> Self
where
B: Body<Data = Bytes, Error = HttpError> + Send + Sync + 'static,
{
Self(BoxBody::new(body))
}
pub async fn bytes(self) -> Result<Bytes, HttpError> {
let size_hint = self.0.size_hint().lower();
let s = self.0.into_data_stream();
collect_bytes(s, Some(size_hint)).await
}
pub fn bytes_stream(self) -> BoxStream<'static, Result<Bytes, HttpError>> {
self.0.into_data_stream().boxed()
}
pub(crate) async fn text(self) -> Result<String, HttpError> {
let b = self.bytes().await?;
String::from_utf8(b.into()).map_err(|e| HttpError::new(HttpErrorKind::Decode, e))
}
#[cfg(any(feature = "aws", feature = "gcp", feature = "azure"))]
pub(crate) async fn json<B: serde::de::DeserializeOwned>(self) -> Result<B, HttpError> {
let b = self.bytes().await?;
serde_json::from_slice(&b).map_err(|e| HttpError::new(HttpErrorKind::Decode, e))
}
}
impl Body for HttpResponseBody {
type Data = Bytes;
type Error = HttpError;
fn poll_frame(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
Pin::new(&mut self.0).poll_frame(cx)
}
fn is_end_stream(&self) -> bool {
self.0.is_end_stream()
}
fn size_hint(&self) -> SizeHint {
self.0.size_hint()
}
}
impl From<Bytes> for HttpResponseBody {
fn from(value: Bytes) -> Self {
Self::new(Full::new(value).map_err(|e| match e {}))
}
}
impl From<Vec<u8>> for HttpResponseBody {
fn from(value: Vec<u8>) -> Self {
Bytes::from(value).into()
}
}
impl From<String> for HttpResponseBody {
fn from(value: String) -> Self {
Bytes::from(value).into()
}
}