From f5eaeadb8da82b874ed3f6a9abd38259b444b61d Mon Sep 17 00:00:00 2001 From: Jun Kurihara Date: Mon, 20 Nov 2023 10:58:16 +0900 Subject: [PATCH] chore: generalize type of body --- relay-lib/src/hyper_body.rs | 15 +++++++++------ relay-lib/src/relay/relay_main.rs | 8 ++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/relay-lib/src/hyper_body.rs b/relay-lib/src/hyper_body.rs index 2a5694d..609d6fe 100644 --- a/relay-lib/src/hyper_body.rs +++ b/relay-lib/src/hyper_body.rs @@ -5,19 +5,22 @@ use hyper::body::{Bytes, Incoming}; /// Type for synthetic boxed body pub(crate) type BoxBody = combinators::BoxBody; -/// Type for either passthrough body or synthetic body -pub(crate) type EitherBody = Either; +/// Type for either passthrough body or given body type, specifically synthetic boxed body +pub(crate) type IncomingOr = Either; /// helper function to build http response with passthrough body -pub(crate) fn passthrough_response(response: Response) -> Result> { - Ok(response.map(EitherBody::Left)) +pub(crate) fn passthrough_response(response: Response) -> Result>> +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> { +pub(crate) fn synthetic_response(status_code: StatusCode) -> Result>> { let res = Response::builder() .status(status_code) - .body(EitherBody::Right(BoxBody::new(empty()))) + .body(IncomingOr::Right(BoxBody::new(empty()))) .unwrap(); Ok(res) } diff --git a/relay-lib/src/relay/relay_main.rs b/relay-lib/src/relay/relay_main.rs index 5239869..45fae17 100644 --- a/relay-lib/src/relay/relay_main.rs +++ b/relay-lib/src/relay/relay_main.rs @@ -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, @@ -47,7 +47,7 @@ pub async fn serve_request_with_validation( // forwarder: Arc>, forwarder: Arc>, validator: Option>>, -) -> Result> +) -> Result>> where C: Send + Sync + Connect + Clone + 'static, { @@ -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!( @@ -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