Skip to content

Commit

Permalink
chore: generalize type of body
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Nov 20, 2023
1 parent 25bd84d commit f5eaead
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 9 additions & 6 deletions relay-lib/src/hyper_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ use hyper::body::{Bytes, Incoming};

/// Type for synthetic boxed body
pub(crate) type BoxBody = combinators::BoxBody<Bytes, hyper::Error>;
/// Type for either passthrough body or synthetic body
pub(crate) type EitherBody = Either<Incoming, BoxBody>;
/// 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(response: Response<Incoming>) -> Result<Response<EitherBody>> {
Ok(response.map(EitherBody::Left))
pub(crate) fn passthrough_response<B>(response: Response<Incoming>) -> Result<Response<IncomingOr<B>>>
where
B: hyper::body::Body,
{
Ok(response.map(IncomingOr::Left))
}

/// build http response with status code of 4xx and 5xx
pub(crate) fn synthetic_error_response(status_code: StatusCode) -> Result<Response<EitherBody>> {
pub(crate) fn synthetic_response(status_code: StatusCode) -> Result<Response<IncomingOr<BoxBody>>> {
let res = Response::builder()
.status(status_code)
.body(EitherBody::Right(BoxBody::new(empty())))
.body(IncomingOr::Right(BoxBody::new(empty())))
.unwrap();
Ok(res)
}
Expand Down
8 changes: 4 additions & 4 deletions relay-lib/src/relay/relay_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{count::RequestCount, forwarder::InnerForwarder, socket::bind_tcp_soc
use crate::{
error::*,
globals::Globals,
hyper_body::{passthrough_response, synthetic_error_response, EitherBody},
hyper_body::{passthrough_response, synthetic_response, BoxBody, IncomingOr},
hyper_executor::LocalExecutor,
log::*,
validator::Validator,
Expand Down Expand Up @@ -47,7 +47,7 @@ pub async fn serve_request_with_validation<C>(
// forwarder: Arc<InnerForwarder<C, B>>,
forwarder: Arc<InnerForwarder<C>>,
validator: Option<Arc<Validator<C>>>,
) -> Result<hyper::Response<EitherBody>>
) -> Result<hyper::Response<IncomingOr<BoxBody>>>
where
C: Send + Sync + Connect + Clone + 'static,
{
Expand All @@ -62,7 +62,7 @@ where
}
Err(e) => {
warn!("token validation failed: {}", e);
return synthetic_error_response(StatusCode::from(e));
return synthetic_response(StatusCode::from(e));
}
};
debug!(
Expand All @@ -75,7 +75,7 @@ where
// serve query as relay
let res = match forwarder.serve(req, peer_addr, validation_passed).await {
Ok(res) => passthrough_response(res),
Err(e) => synthetic_error_response(StatusCode::from(e)),
Err(e) => synthetic_response(StatusCode::from(e)),
};
debug!("serve query finish");
res
Expand Down

0 comments on commit f5eaead

Please sign in to comment.