Skip to content

Commit 8cf5c41

Browse files
committed
feat: add unix_socket() option to client builder
1 parent 6358cef commit 8cf5c41

File tree

7 files changed

+576
-5
lines changed

7 files changed

+576
-5
lines changed

src/async_impl/client.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use crate::async_impl::h3_client::connect::{H3ClientConfig, H3Connector};
2020
#[cfg(feature = "http3")]
2121
use crate::async_impl::h3_client::H3Client;
2222
use crate::config::{RequestConfig, RequestTimeout};
23+
#[cfg(unix)]
24+
use crate::connect::uds::UnixSocketProvider;
2325
use crate::connect::{
2426
sealed::{Conn, Unnameable},
2527
BoxedConnectorLayer, BoxedConnectorService, Connector, ConnectorBuilder,
@@ -256,6 +258,9 @@ struct Config {
256258
h3_send_grease: Option<bool>,
257259
dns_overrides: HashMap<String, Vec<SocketAddr>>,
258260
dns_resolver: Option<Arc<dyn Resolve>>,
261+
262+
#[cfg(unix)]
263+
unix_socket: Option<Arc<std::path::Path>>,
259264
}
260265

261266
impl Default for ClientBuilder {
@@ -378,6 +383,8 @@ impl ClientBuilder {
378383
#[cfg(feature = "http3")]
379384
h3_send_grease: None,
380385
dns_resolver: None,
386+
#[cfg(unix)]
387+
unix_socket: None,
381388
},
382389
}
383390
}
@@ -907,6 +914,12 @@ impl ClientBuilder {
907914
#[cfg(feature = "socks")]
908915
connector_builder.set_socks_resolver(resolver);
909916

917+
// TODO: It'd be best to refactor this so the HttpConnector is never
918+
// constructed at all. But there's a lot of code for all the different
919+
// ways TLS can be configured...
920+
#[cfg(unix)]
921+
connector_builder.set_unix_socket(config.unix_socket);
922+
910923
let mut builder =
911924
hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new());
912925
#[cfg(feature = "http2")]
@@ -1710,6 +1723,25 @@ impl ClientBuilder {
17101723
self
17111724
}
17121725

1726+
// Alt Transports
1727+
1728+
/// Set that all connections will use this Unix socket.
1729+
///
1730+
/// If a request URI uses the `https` scheme, TLS will still be used over
1731+
/// the Unix socket.
1732+
///
1733+
/// # Note
1734+
///
1735+
/// This option is not compatible with any of the TCP or Proxy options.
1736+
/// Setting this will ignore all those options previously set.
1737+
///
1738+
/// Likewise, DNS resolution will not be done on the domain name.
1739+
#[cfg(unix)]
1740+
pub fn unix_socket(mut self, path: impl UnixSocketProvider) -> ClientBuilder {
1741+
self.config.unix_socket = Some(path.reqwest_uds_path(crate::connect::uds::Internal).into());
1742+
self
1743+
}
1744+
17131745
// TLS options
17141746

17151747
/// Add a custom root certificate.
@@ -2752,6 +2784,11 @@ impl Config {
27522784
f.field("tls_enable_early_data", &true);
27532785
}
27542786
}
2787+
2788+
#[cfg(unix)]
2789+
if let Some(ref p) = self.unix_socket {
2790+
f.field("unix_socket", p);
2791+
}
27552792
}
27562793
}
27572794

src/blocking/client.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use super::request::{Request, RequestBuilder};
2020
use super::response::Response;
2121
use super::wait;
2222
use crate::connect::sealed::{Conn, Unnameable};
23+
#[cfg(unix)]
24+
use crate::connect::uds::UnixSocketProvider;
2325
use crate::connect::BoxedConnectorService;
2426
use crate::dns::Resolve;
2527
use crate::error::BoxError;
@@ -723,6 +725,24 @@ impl ClientBuilder {
723725
self.with_inner(move |inner| inner.tcp_user_timeout(val))
724726
}
725727

728+
// Alt Transports
729+
730+
/// Set that all connections will use this Unix socket.
731+
///
732+
/// If a request URI uses the `https` scheme, TLS will still be used over
733+
/// the Unix socket.
734+
///
735+
/// # Note
736+
///
737+
/// This option is not compatible with any of the TCP or Proxy options.
738+
/// Setting this will ignore all those options previously set.
739+
///
740+
/// Likewise, DNS resolution will not be done on the domain name.
741+
#[cfg(unix)]
742+
pub fn unix_socket(self, path: impl UnixSocketProvider) -> ClientBuilder {
743+
self.with_inner(move |inner| inner.unix_socket(path))
744+
}
745+
726746
// TLS options
727747

728748
/// Add a custom root certificate.

0 commit comments

Comments
 (0)