feat: changed options for http version of requests to upstream app servers

This commit is contained in:
Jun Kurihara 2023-08-09 02:13:04 +09:00
commit 02c333905f
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
6 changed files with 35 additions and 23 deletions

View file

@ -4,6 +4,7 @@ use derive_builder::Builder;
use hyper::{
body::{Body, HttpBody},
client::{connect::Connect, HttpConnector},
http::Version,
Client, Request, Response,
};
use hyper_rustls::HttpsConnector;
@ -21,24 +22,28 @@ pub struct Forwarder<C, B = Body>
where
C: Connect + Clone + Sync + Send + 'static,
{
// TODO: need `h2c` or http/2-only client separately
// TODO: maybe this forwarder definition is suitable for cache handling.
inner: Client<C, B>,
inner_h2: Client<C, B>, // `h2c` or http/2-only client is defined separately
}
#[async_trait]
impl<C, B> ForwardRequest<B> for Forwarder<C, B>
where
B: HttpBody + Send + 'static,
B: HttpBody + Send + Sync + 'static,
B::Data: Send,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
C: Connect + Clone + Sync + Send + 'static,
{
type Error = RpxyError;
async fn request(&self, req: Request<B>) -> Result<Response<Body>, Self::Error> {
// TODO:
// TODO: Implement here a client that handles `h2c` requests
// TODO:
self.inner.request(req).await.map_err(RpxyError::Hyper)
// TODO: This 'match' condition is always evaluated at every 'request' invocation. So, it is inefficient.
// Needs to be reconsidered. Currently, this is a kind of work around.
// This possibly relates to https://github.com/hyperium/hyper/issues/2417.
match req.version() {
Version::HTTP_2 => self.inner_h2.request(req).await.map_err(RpxyError::Hyper), // handles `h2c` requests
_ => self.inner.request(req).await.map_err(RpxyError::Hyper),
}
}
}
@ -51,8 +56,14 @@ impl Forwarder<HttpsConnector<HttpConnector>, Body> {
.enable_http1()
.enable_http2()
.build();
let connector_h2 = hyper_rustls::HttpsConnectorBuilder::new()
.with_webpki_roots()
.https_or_http()
.enable_http1()
.build();
let inner = Client::builder().build::<_, Body>(connector);
Self { inner }
let inner_h2 = Client::builder().http2_only(true).build::<_, Body>(connector_h2);
Self { inner, inner_h2 }
}
}