Skip to content

Commit fc7510d

Browse files
authored
Add unix_socket_path that exposes more Unix socket control (#347)
The existing `unix_socket` method has some limitations in its design: - You cannot unset a previously set path. - You cannot set non-UTF-8 paths. This adds a second method named `unix_socket_path` that instead takes an `Option<impl AsRef<Path>>` which more closely aligns with what libcurl accepts for this option. We could consider soft-deprecating `unix_socket`, but I left it alone for now.
1 parent 4a656fd commit fc7510d

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/easy/handle.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ impl Easy {
165165
self.inner.unix_socket(unix_domain_socket)
166166
}
167167

168+
/// Same as [`Easy2::unix_socket_path`](struct.Easy2.html#method.unix_socket_path)
169+
pub fn unix_socket_path<P: AsRef<Path>>(&mut self, path: Option<P>) -> Result<(), Error> {
170+
self.inner.unix_socket_path(path)
171+
}
172+
168173
// =========================================================================
169174
// Callback options
170175

src/easy/handler.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,18 +791,45 @@ impl<H> Easy2<H> {
791791
self.setopt_long(curl_sys::CURLOPT_WILDCARDMATCH, m as c_long)
792792
}
793793

794-
/// Provides the unix domain socket which this handle will work with.
794+
/// Provides the Unix domain socket which this handle will work with.
795795
///
796-
/// The string provided must be unix domain socket -encoded with the format:
796+
/// The string provided must be a path to a Unix domain socket encoded with
797+
/// the format:
797798
///
798799
/// ```text
799800
/// /path/file.sock
800801
/// ```
802+
///
803+
/// By default this option is not set and corresponds to
804+
/// [`CURLOPT_UNIX_SOCKET_PATH`](https://curl.haxx.se/libcurl/c/CURLOPT_UNIX_SOCKET_PATH.html).
801805
pub fn unix_socket(&mut self, unix_domain_socket: &str) -> Result<(), Error> {
802806
let socket = CString::new(unix_domain_socket)?;
803807
self.setopt_str(curl_sys::CURLOPT_UNIX_SOCKET_PATH, &socket)
804808
}
805809

810+
/// Provides the Unix domain socket which this handle will work with.
811+
///
812+
/// The string provided must be a path to a Unix domain socket encoded with
813+
/// the format:
814+
///
815+
/// ```text
816+
/// /path/file.sock
817+
/// ```
818+
///
819+
/// This function is an alternative to [`Easy2::unix_socket`] that supports
820+
/// non-UTF-8 paths and also supports disabling Unix sockets by setting the
821+
/// option to `None`.
822+
///
823+
/// By default this option is not set and corresponds to
824+
/// [`CURLOPT_UNIX_SOCKET_PATH`](https://curl.haxx.se/libcurl/c/CURLOPT_UNIX_SOCKET_PATH.html).
825+
pub fn unix_socket_path<P: AsRef<Path>>(&mut self, path: Option<P>) -> Result<(), Error> {
826+
if let Some(path) = path {
827+
self.setopt_path(curl_sys::CURLOPT_UNIX_SOCKET_PATH, path.as_ref())
828+
} else {
829+
self.setopt_ptr(curl_sys::CURLOPT_UNIX_SOCKET_PATH, 0 as _)
830+
}
831+
}
832+
806833
// =========================================================================
807834
// Internal accessors
808835

0 commit comments

Comments
 (0)