wip: feat: change request body from either to explicit enum

This commit is contained in:
Jun Kurihara 2023-12-12 20:17:13 +09:00
commit 1c18f3836a
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
8 changed files with 45 additions and 41 deletions

View file

@ -1,25 +1,11 @@
// use http::Response;
use http_body_util::{combinators, BodyExt, Either, Empty, Full};
use super::body::IncomingLike;
use crate::error::RpxyError;
use http_body_util::{combinators, BodyExt, Empty, Full};
use hyper::body::{Body, Bytes, Incoming};
use std::pin::Pin;
/// Type for synthetic boxed body
pub(crate) type BoxBody = combinators::BoxBody<Bytes, hyper::Error>;
/// Type for either passthrough body or given body type, specifically synthetic boxed body
pub(crate) type IncomingOr<B> = Either<Incoming, B>;
// /// helper function to build http response with passthrough body
// pub(crate) fn wrap_incoming_body_response<B>(response: Response<Incoming>) -> Response<IncomingOr<B>>
// where
// B: hyper::body::Body,
// {
// response.map(IncomingOr::Left)
// }
// /// helper function to build http response with synthetic body
// pub(crate) fn wrap_synthetic_body_response<B>(response: Response<B>) -> Response<IncomingOr<B>> {
// response.map(IncomingOr::Right)
// }
/// helper function to build a empty body
pub(crate) fn empty() -> BoxBody {
@ -31,6 +17,30 @@ pub(crate) fn full(body: Bytes) -> BoxBody {
Full::new(body).map_err(|never| match never {}).boxed()
}
/* ------------------------------------ */
/// Request body used in this project
/// - Incoming: just a type that only forwards the downstream request body to upstream.
/// - IncomingLike: a Incoming-like type in which channel is used
pub(crate) enum RequestBody {
Incoming(Incoming),
IncomingLike(IncomingLike),
}
impl Body for RequestBody {
type Data = bytes::Bytes;
type Error = RpxyError;
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
match self.get_mut() {
RequestBody::Incoming(incoming) => Pin::new(incoming).poll_frame(cx).map_err(RpxyError::HyperBodyError),
RequestBody::IncomingLike(incoming_like) => Pin::new(incoming_like).poll_frame(cx),
}
}
}
/* ------------------------------------ */
#[cfg(feature = "cache")]
use futures::channel::mpsc::UnboundedReceiver;
@ -44,8 +54,8 @@ pub(crate) type UnboundedStreamBody = StreamBody<UnboundedReceiver<Result<Frame<
/// Response body use in this project
/// - Incoming: just a type that only forwards the upstream response body to downstream.
/// - BoxedCache: a type that is generated from cache, e.g.,, small byte object.
/// - StreamedCache: another type that is generated from cache as stream, e.g., large byte object.
/// - Boxed: a type that is generated from cache or synthetic response body, e.g.,, small byte object.
/// - Streamed: another type that is generated from stream, e.g., large byte object.
pub(crate) enum ResponseBody {
Incoming(Incoming),
Boxed(BoxBody),
@ -55,7 +65,7 @@ pub(crate) enum ResponseBody {
impl Body for ResponseBody {
type Data = bytes::Bytes;
type Error = hyper::Error;
type Error = RpxyError;
fn poll_frame(
self: Pin<&mut Self>,
@ -68,5 +78,6 @@ impl Body for ResponseBody {
#[cfg(feature = "cache")]
ResponseBody::Streamed(streamed) => Pin::new(streamed).poll_frame(cx),
}
.map_err(RpxyError::HyperBodyError)
}
}

View file

@ -12,5 +12,5 @@ pub(crate) mod rt {
#[allow(unused)]
pub(crate) mod body {
pub(crate) use super::body_incoming_like::IncomingLike;
pub(crate) use super::body_type::{empty, full, BoxBody, IncomingOr, ResponseBody, UnboundedStreamBody};
pub(crate) use super::body_type::{empty, full, BoxBody, RequestBody, ResponseBody, UnboundedStreamBody};
}