wip: implementing message handler

This commit is contained in:
Jun Kurihara 2023-11-27 15:39:19 +09:00
commit a9ce26ae76
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
16 changed files with 520 additions and 98 deletions

View file

@ -1,5 +1,3 @@
use crate::error::*;
use http::{Response, StatusCode};
use http_body_util::{combinators, BodyExt, Either, Empty, Full};
use hyper::body::{Bytes, Incoming};
@ -8,30 +6,8 @@ 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 passthrough_response<B>(response: Response<Incoming>) -> RpxyResult<Response<IncomingOr<B>>>
where
B: hyper::body::Body,
{
Ok(response.map(IncomingOr::Left))
}
/// helper function to build http response with synthetic body
pub(crate) fn synthetic_response<B>(response: Response<B>) -> RpxyResult<Response<IncomingOr<B>>> {
Ok(response.map(IncomingOr::Right))
}
/// build http response with status code of 4xx and 5xx
pub(crate) fn synthetic_error_response(status_code: StatusCode) -> RpxyResult<Response<IncomingOr<BoxBody>>> {
let res = Response::builder()
.status(status_code)
.body(IncomingOr::Right(BoxBody::new(empty())))
.unwrap();
Ok(res)
}
/// helper function to build a empty body
fn empty() -> BoxBody {
pub(crate) fn empty() -> BoxBody {
Empty::<Bytes>::new().map_err(|never| match never {}).boxed()
}

View file

@ -8,6 +8,5 @@ pub(crate) mod rt {
}
pub(crate) mod body {
pub(crate) use super::body_incoming_like::IncomingLike;
pub(crate) use super::body_type::{BoxBody, IncomingOr};
pub(crate) use super::body_type::{empty, full, BoxBody, IncomingOr};
}
pub(crate) use body_type::{full, passthrough_response, synthetic_error_response, synthetic_response};