Skip to content

Commit 10ad5af

Browse files
committed
feat: add unix_socket() option to client builder
1 parent 036f9c7 commit 10ad5af

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 {
@@ -380,6 +385,8 @@ impl ClientBuilder {
380385
#[cfg(feature = "http3")]
381386
h3_send_grease: None,
382387
dns_resolver: None,
388+
#[cfg(unix)]
389+
unix_socket: None,
383390
},
384391
}
385392
}
@@ -909,6 +916,12 @@ impl ClientBuilder {
909916
#[cfg(feature = "socks")]
910917
connector_builder.set_socks_resolver(resolver);
911918

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

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

17171749
/// Add a custom root certificate.
@@ -2754,6 +2786,11 @@ impl Config {
27542786
f.field("tls_enable_early_data", &true);
27552787
}
27562788
}
2789+
2790+
#[cfg(unix)]
2791+
if let Some(ref p) = self.unix_socket {
2792+
f.field("unix_socket", p);
2793+
}
27572794
}
27582795
}
27592796

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;
@@ -624,6 +626,24 @@ impl ClientBuilder {
624626
self.with_inner(move |inner| inner.tcp_user_timeout(val))
625627
}
626628

629+
// Alt Transports
630+
631+
/// Set that all connections will use this Unix socket.
632+
///
633+
/// If a request URI uses the `https` scheme, TLS will still be used over
634+
/// the Unix socket.
635+
///
636+
/// # Note
637+
///
638+
/// This option is not compatible with any of the TCP or Proxy options.
639+
/// Setting this will ignore all those options previously set.
640+
///
641+
/// Likewise, DNS resolution will not be done on the domain name.
642+
#[cfg(unix)]
643+
pub fn unix_socket(self, path: impl UnixSocketProvider) -> ClientBuilder {
644+
self.with_inner(move |inner| inner.unix_socket(path))
645+
}
646+
627647
// TLS options
628648

629649
/// Add a custom root certificate.

0 commit comments

Comments
 (0)