1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::{
path::PathBuf,
sync::Arc,
};
#[cfg(feature = "tokio")]
use tokio_rs::net::UnixStream;
#[cfg(feature = "async-std")]
use async_std_rs::os::unix::net::UnixStream;
use unicom::{
Url, Error,
Backend, Connector, BoxedConnector, BoxedConnect, BoxedConnection,
};
#[derive(Clone)]
pub struct UnixSocket {
name: String,
description: String,
}
impl Default for UnixSocket {
fn default() -> Self {
Self {
name: "unix-socket".into(),
description: "Support for local unix domain socket connections.".into(),
}
}
}
impl Backend for UnixSocket {
fn name(&self) -> &str {
&self.name
}
fn description(&self) -> &str {
&self.description
}
fn connector(&self, url: &Url) -> Option<BoxedConnector> {
if (url.scheme() == "socket" || url.scheme() == "unix")
&& !url.has_host()
&& url.path() != "/"
{
let path = url.path().into();
let url = url.clone();
Some(Arc::new(UnixConnector { url, path }))
} else {
None
}
}
}
#[derive(Clone)]
struct UnixConnector {
url: Url,
path: PathBuf,
}
impl Connector for UnixConnector {
fn url(&self) -> &Url {
&self.url
}
fn connect(&self) -> BoxedConnect {
let this = self.clone();
Box::pin(async move {
let stm = UnixStream::connect(this.path).await
.map_err(|e| Error::FailedConnect(e.to_string()))?;
Ok(Box::new(stm) as BoxedConnection)
})
}
}