Skip to content
14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ required-features = ["socks"]
name = "form"
path = "examples/form.rs"

[[example]]
name = "connect_via_lower_priority_tokio_runtime"
path = "examples/connect_via_lower_priority_tokio_runtime.rs"
required-features = ["tracing"]

[[example]]
name = "emulation_firefox"
path = "examples/emulation_firefox.rs"
Expand Down Expand Up @@ -276,11 +281,6 @@ name = "request_with_emulation"
path = "examples/request_with_emulation.rs"
required-features = ["gzip", "brotli", "zstd", "deflate", "tracing"]

[[example]]
name = "connect_via_lower_priority_tokio_runtime"
path = "examples/connect_via_lower_priority_tokio_runtime.rs"
required-features = ["tracing"]

[[example]]
name = "request_with_local_address"
path = "examples/request_with_local_address.rs"
Expand All @@ -304,3 +304,7 @@ required-features = ["ws", "futures-util/std", "tracing"]
[[example]]
name = "keylog"
path = "examples/keylog.rs"

[[example]]
name = "unix_socket"
path = "examples/unix_socket.rs"
33 changes: 33 additions & 0 deletions examples/unix_socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#[cfg(unix)]
#[tokio::main]
async fn main() -> wreq::Result<()> {
// Create a Unix socket proxy
let proxy = wreq::Proxy::unix("/var/run/docker.sock")?;

// Build a client
let client = wreq::Client::builder()
// Specify the Unix socket path
.proxy(proxy.clone())
.timeout(std::time::Duration::from_secs(10))
.build()?;

// Use the API you're already familiar with
let resp = client
.get("http://localhost/v1.41/containers/json")
.send()
.await?;
println!("{}", resp.text().await?);

// Or specify the Unix socket directly in the request
let resp = client
.get("http://localhost/v1.41/containers/json")
.proxy(proxy)
.send()
.await?;
println!("{}", resp.text().await?);

Ok(())
}

#[cfg(not(unix))]
fn main() {}
48 changes: 33 additions & 15 deletions src/client/http/connect/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::{
};

use pin_project_lite::pin_project;
#[cfg(unix)]
use tokio::net::UnixStream;
use tokio::{
io::{AsyncRead, AsyncWrite},
net::TcpStream,
Expand All @@ -27,9 +29,9 @@ pin_project! {
/// * absolute-form (`GET http://foo.bar/and/a/path HTTP/1.1`), otherwise.
pub struct Conn {
#[pin]
inner: Box<dyn AsyncConnWithInfo>,
is_proxy: bool,
tls_info: bool,
pub inner: Box<dyn AsyncConnWithInfo>,
pub tls_info: bool,
pub is_proxy: bool,
}
}

Expand All @@ -47,18 +49,6 @@ pin_project! {

// ==== impl Conn ====

impl Conn {
/// Creates a new `Conn` instance with the given inner connection and TLS info flag.
#[inline(always)]
pub(super) fn new(inner: Box<dyn AsyncConnWithInfo>, is_proxy: bool, tls_info: bool) -> Self {
Self {
inner,
is_proxy,
tls_info,
}
}
}

impl Connection for Conn {
fn connected(&self) -> Connected {
let connected = self.inner.connected().proxy(self.is_proxy);
Expand Down Expand Up @@ -135,6 +125,8 @@ where
}
}

// ===== impl TcpStream =====

impl Connection for TlsConn<TcpStream> {
fn connected(&self) -> Connected {
let connected = self.inner.inner().get_ref().connected();
Expand All @@ -157,6 +149,32 @@ impl Connection for TlsConn<TokioIo<MaybeHttpsStream<TcpStream>>> {
}
}

// ===== impl UnixStream =====

#[cfg(unix)]
impl Connection for TlsConn<UnixStream> {
fn connected(&self) -> Connected {
let connected = self.inner.inner().get_ref().connected();
if self.inner.inner().ssl().selected_alpn_protocol() == Some(b"h2") {
connected.negotiated_h2()
} else {
connected
}
}
}

#[cfg(unix)]
impl Connection for TlsConn<TokioIo<MaybeHttpsStream<UnixStream>>> {
fn connected(&self) -> Connected {
let connected = self.inner.inner().get_ref().connected();
if self.inner.inner().ssl().selected_alpn_protocol() == Some(b"h2") {
connected.negotiated_h2()
} else {
connected
}
}
}

impl<T: AsyncRead + AsyncWrite + Unpin> Read for TlsConn<T> {
fn poll_read(
self: Pin<&mut Self>,
Expand Down
Loading