Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DefaultClient] Use incoming http version to outer server #56

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ async fn main() {
Some(Cache::new(128)),
);

let client = DefaultClient::new(
tokio_native_tls::native_tls::TlsConnector::builder()
// You must set ALPN if you want to support HTTP/2
.request_alpns(&["h2", "http/1.1"])
.build()
.unwrap(),
);
let client = DefaultClient::new().unwrap();
let server = proxy
.bind(("127.0.0.1", 3003), move |_client_addr, req| {
let client = client.clone();
Expand All @@ -111,8 +105,6 @@ async fn main() {
// Modifying upgraded traffic is not supported yet.

// You can try https://echo.websocket.org/.ws to test websocket.
// But you need to disable alpn of DefaultClient to disable HTTP2 because echo.websocket.org does not support HTTP/2 for Websocket.
// It should be match incoming and outgoing HTTP version on DefaultClient, I'll fix this later. #54
println!("Upgrade connection");
let Upgrade {
mut client_to_server,
Expand Down
8 changes: 1 addition & 7 deletions examples/dev_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,7 @@ async fn main() {
Some(Cache::new(128)),
);

let client = DefaultClient::new(
tokio_native_tls::native_tls::TlsConnector::builder()
// You must set ALPN if you want to support HTTP/2
.request_alpns(&["h2", "http/1.1"])
.build()
.unwrap(),
);
let client = DefaultClient::new().unwrap();
let proxy = proxy
.bind(("127.0.0.1", 3003), move |_client_addr, mut req| {
let client = client.clone();
Expand Down
10 changes: 1 addition & 9 deletions examples/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ async fn main() {
Some(Cache::new(128)),
);

let client = DefaultClient::new(
tokio_native_tls::native_tls::TlsConnector::builder()
// You must set ALPN if you want to support HTTP/2
.request_alpns(&["h2", "http/1.1"])
.build()
.unwrap(),
);
let client = DefaultClient::new().unwrap();
let server = proxy
.bind(("127.0.0.1", 3003), move |_client_addr, req| {
let client = client.clone();
Expand All @@ -98,8 +92,6 @@ async fn main() {
// Modifying upgraded traffic is not supported yet.

// You can try https://echo.websocket.org/.ws to test websocket.
// But you need to disable alpn of DefaultClient to disable HTTP2 because echo.websocket.org does not support HTTP/2 for Websocket.
// It should be match incoming and outgoing HTTP version on DefaultClient, I'll fix this later. #54
println!("Upgrade connection");
let Upgrade {
mut client_to_server,
Expand Down
32 changes: 25 additions & 7 deletions src/default_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use futures::channel::mpsc::{UnboundedReceiver, UnboundedSender};
use http_body_util::Empty;
use hyper::{
body::{Body, Incoming},
client, header, Request, Response, StatusCode, Uri,
client, header, Request, Response, StatusCode, Uri, Version,
};
use hyper_util::rt::{TokioExecutor, TokioIo};
use std::task::{Context, Poll};
Expand Down Expand Up @@ -37,10 +37,28 @@ pub struct Upgrade {
}
#[derive(Clone)]
/// Default HTTP client for this crate
pub struct DefaultClient(tokio_native_tls::TlsConnector);
pub struct DefaultClient {
tls_connector_no_alpn: tokio_native_tls::TlsConnector,
tls_connector_alpn_h2: tokio_native_tls::TlsConnector,
}
impl DefaultClient {
pub fn new(tls_connector: native_tls::TlsConnector) -> Self {
Self(tls_connector.into())
pub fn new() -> native_tls::Result<Self> {
let tls_connector_no_alpn = native_tls::TlsConnector::builder().build()?;
let tls_connector_alpn_h2 = native_tls::TlsConnector::builder()
.request_alpns(&["h2", "http/1.1"])
.build()?;

Ok(Self {
tls_connector_no_alpn: tokio_native_tls::TlsConnector::from(tls_connector_no_alpn),
tls_connector_alpn_h2: tokio_native_tls::TlsConnector::from(tls_connector_alpn_h2),
})
}

fn tls_connector(&self, http_version: Version) -> &tokio_native_tls::TlsConnector {
match http_version {
Version::HTTP_2 => &self.tls_connector_alpn_h2,
_ => &self.tls_connector_no_alpn,
}
}

/// Send a request and return a response.
Expand All @@ -55,7 +73,7 @@ impl DefaultClient {
B::Data: Send,
B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let mut send_request = self.connect(req.uri()).await?;
let mut send_request = self.connect(req.uri(), req.version()).await?;

let (req_parts, req_body) = req.into_parts();

Expand Down Expand Up @@ -99,7 +117,7 @@ impl DefaultClient {
Ok((res, None))
}

async fn connect<B>(&self, uri: &Uri) -> Result<SendRequest<B>, Error>
async fn connect<B>(&self, uri: &Uri, http_version: Version) -> Result<SendRequest<B>, Error>
where
B: Body + Unpin + Send + 'static,
B::Data: Send,
Expand All @@ -120,7 +138,7 @@ impl DefaultClient {

if uri.scheme() == Some(&hyper::http::uri::Scheme::HTTPS) {
let tls = self
.0
.tls_connector(http_version)
.connect(host, tcp)
.await
.map_err(|err| Error::TlsConnectError(uri.clone(), err))?;
Expand Down
7 changes: 1 addition & 6 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ fn client_tls(proxy_port: u16, cert: &rcgen::CertifiedKey) -> reqwest::Client {
}

fn proxy_client() -> DefaultClient {
DefaultClient::new(
tokio_native_tls::native_tls::TlsConnector::builder()
.request_alpns(&["h2", "http/1.1"])
.build()
.unwrap(),
)
DefaultClient::new().unwrap()
}

async fn setup<B, E, E2, S, F>(app: Router, service: S) -> (u16, u16)
Expand Down
Loading