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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::{Multiaddr, Protocol};
use std::{error, fmt, iter, net::IpAddr};

/// Attempts to parse an URL into a multiaddress.
///
/// This function will return an error if some information in the URL cannot be retained in the
/// generated multiaddress. This includes a username, password, path (if not supported by the
/// multiaddr), and query string.
///
/// This function is only present if the `url` feature is enabled, and it is
/// enabled by default.
///
/// The supported URL schemes are:
///
/// - `ws://example.com/`
/// - `wss://example.com/`
/// - `http://example.com/`
/// - `https://example.com/`
/// - `unix:/foo/bar`
///
/// # Example
///
/// ```
/// let addr = multiaddr::from_url("ws://127.0.0.1:8080/").unwrap();
/// assert_eq!(addr, "/ip4/127.0.0.1/tcp/8080/ws".parse().unwrap());
/// ```
///
pub fn from_url(url: &str) -> std::result::Result<Multiaddr, FromUrlErr> {
    from_url_inner(url, false)
}

/// Attempts to parse an URL into a multiaddress. Ignores possible loss of information.
///
/// This function is similar to [`from_url`], except that we don't return an error if some
/// information in the URL cannot be retain in the generated multiaddres.
///
/// This function is only present if the `url` feature is enabled, and it is
/// enabled by default.
///
/// # Example
///
/// ```
/// let addr = "ws://user:pass@127.0.0.1:8080/";
/// assert!(multiaddr::from_url(addr).is_err());
/// assert!(multiaddr::from_url_lossy(addr).is_ok());
/// ```
///
pub fn from_url_lossy(url: &str) -> std::result::Result<Multiaddr, FromUrlErr> {
    from_url_inner(url, true)
}

/// Underlying implementation of `from_url` and `from_url_lossy`.
fn from_url_inner(url: &str, lossy: bool) -> std::result::Result<Multiaddr, FromUrlErr> {
    let url = url::Url::parse(url).map_err(|_| FromUrlErr::BadUrl)?;

    match url.scheme() {
        // Note: if you add support for a new scheme, please update the documentation as well.
        "ws" | "wss" | "http" | "https" => from_url_inner_http_ws(url, lossy),
        "unix" => from_url_inner_path(url, lossy),
        _ => Err(FromUrlErr::UnsupportedScheme),
    }
}

/// Called when `url.scheme()` is an Internet-like URL.
fn from_url_inner_http_ws(
    url: url::Url,
    lossy: bool,
) -> std::result::Result<Multiaddr, FromUrlErr> {
    let (protocol, lost_path, default_port) = match url.scheme() {
        "ws" => (Protocol::Ws(url.path().to_owned().into()), false, 80),
        "wss" => (Protocol::Wss(url.path().to_owned().into()), false, 443),
        "http" => (Protocol::Http, true, 80),
        "https" => (Protocol::Https, true, 443),
        _ => unreachable!("We only call this function for one of the given schemes; qed"),
    };

    let port = Protocol::Tcp(url.port().unwrap_or(default_port));
    let ip = if let Some(hostname) = url.host_str() {
        if let Ok(ip) = hostname.parse::<IpAddr>() {
            Protocol::from(ip)
        } else {
            Protocol::Dns(hostname.into())
        }
    } else {
        return Err(FromUrlErr::BadUrl);
    };

    if !lossy
        && (!url.username().is_empty()
            || url.password().is_some()
            || (lost_path && url.path() != "/" && !url.path().is_empty())
            || url.query().is_some()
            || url.fragment().is_some())
    {
        return Err(FromUrlErr::InformationLoss);
    }

    Ok(iter::once(ip)
        .chain(iter::once(port))
        .chain(iter::once(protocol))
        .collect())
}

/// Called when `url.scheme()` is a path-like URL.
fn from_url_inner_path(url: url::Url, lossy: bool) -> std::result::Result<Multiaddr, FromUrlErr> {
    let protocol = match url.scheme() {
        "unix" => Protocol::Unix(url.path().to_owned().into()),
        _ => unreachable!("We only call this function for one of the given schemes; qed"),
    };

    if !lossy
        && (!url.username().is_empty()
            || url.password().is_some()
            || url.query().is_some()
            || url.fragment().is_some())
    {
        return Err(FromUrlErr::InformationLoss);
    }

    Ok(Multiaddr::from(protocol))
}

/// Error while parsing an URL.
#[derive(Debug)]
pub enum FromUrlErr {
    /// Failed to parse the URL.
    BadUrl,
    /// The URL scheme was not recognized.
    UnsupportedScheme,
    /// Some information in the URL would be lost. Never returned by `from_url_lossy`.
    InformationLoss,
}

impl fmt::Display for FromUrlErr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FromUrlErr::BadUrl => write!(f, "Bad URL"),
            FromUrlErr::UnsupportedScheme => write!(f, "Unrecognized URL scheme"),
            FromUrlErr::InformationLoss => write!(f, "Some information in the URL would be lost"),
        }
    }
}

impl error::Error for FromUrlErr {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_garbage_doesnt_panic() {
        for _ in 0..50 {
            let url = (0..16).map(|_| rand::random::<u8>()).collect::<Vec<_>>();
            let url = String::from_utf8_lossy(&url);
            assert!(from_url(&url).is_err());
        }
    }

    #[test]
    fn normal_usage_ws() {
        let addr = from_url("ws://127.0.0.1:8000").unwrap();
        assert_eq!(addr, "/ip4/127.0.0.1/tcp/8000/ws".parse().unwrap());
    }

    #[test]
    fn normal_usage_wss() {
        let addr = from_url("wss://127.0.0.1:8000").unwrap();
        assert_eq!(addr, "/ip4/127.0.0.1/tcp/8000/wss".parse().unwrap());
    }

    #[test]
    fn default_ws_port() {
        let addr = from_url("ws://127.0.0.1").unwrap();
        assert_eq!(addr, "/ip4/127.0.0.1/tcp/80/ws".parse().unwrap());
    }

    #[test]
    fn default_http_port() {
        let addr = from_url("http://127.0.0.1").unwrap();
        assert_eq!(addr, "/ip4/127.0.0.1/tcp/80/http".parse().unwrap());
    }

    #[test]
    fn default_wss_port() {
        let addr = from_url("wss://127.0.0.1").unwrap();
        assert_eq!(addr, "/ip4/127.0.0.1/tcp/443/wss".parse().unwrap());
    }

    #[test]
    fn default_https_port() {
        let addr = from_url("https://127.0.0.1").unwrap();
        assert_eq!(addr, "/ip4/127.0.0.1/tcp/443/https".parse().unwrap());
    }

    #[test]
    fn dns_addr_ws() {
        let addr = from_url("ws://example.com").unwrap();
        assert_eq!(addr, "/dns/example.com/tcp/80/ws".parse().unwrap());
    }

    #[test]
    fn dns_addr_http() {
        let addr = from_url("http://example.com").unwrap();
        assert_eq!(addr, "/dns/example.com/tcp/80/http".parse().unwrap());
    }

    #[test]
    fn dns_addr_wss() {
        let addr = from_url("wss://example.com").unwrap();
        assert_eq!(addr, "/dns/example.com/tcp/443/wss".parse().unwrap());
    }

    #[test]
    fn dns_addr_https() {
        let addr = from_url("https://example.com").unwrap();
        assert_eq!(addr, "/dns/example.com/tcp/443/https".parse().unwrap());
    }

    #[test]
    fn bad_hostname() {
        let addr = from_url("wss://127.0.0.1x").unwrap();
        assert_eq!(addr, "/dns/127.0.0.1x/tcp/443/wss".parse().unwrap());
    }

    #[test]
    fn wrong_scheme() {
        match from_url("foo://127.0.0.1") {
            Err(FromUrlErr::UnsupportedScheme) => {}
            _ => panic!(),
        }
    }

    #[test]
    fn dns_and_port() {
        let addr = from_url("http://example.com:1000").unwrap();
        assert_eq!(addr, "/dns/example.com/tcp/1000/http".parse().unwrap());
    }

    #[test]
    fn username_lossy() {
        let addr = "http://foo@example.com:1000/";
        assert!(from_url(addr).is_err());
        assert!(from_url_lossy(addr).is_ok());
        assert!(from_url("http://@example.com:1000/").is_ok());
    }

    #[test]
    fn password_lossy() {
        let addr = "http://:bar@example.com:1000/";
        assert!(from_url(addr).is_err());
        assert!(from_url_lossy(addr).is_ok());
    }

    #[test]
    fn path_lossy() {
        let addr = "http://example.com:1000/foo";
        assert!(from_url(addr).is_err());
        assert!(from_url_lossy(addr).is_ok());
    }

    #[test]
    fn fragment_lossy() {
        let addr = "http://example.com:1000/#foo";
        assert!(from_url(addr).is_err());
        assert!(from_url_lossy(addr).is_ok());
    }

    #[test]
    fn unix() {
        let addr = from_url("unix:/foo/bar").unwrap();
        assert_eq!(addr, Multiaddr::from(Protocol::Unix("/foo/bar".into())));
    }

    #[test]
    fn ws_path() {
        let addr = from_url("ws://1.2.3.4:1000/foo/bar").unwrap();
        assert_eq!(
            addr,
            "/ip4/1.2.3.4/tcp/1000/x-parity-ws/%2ffoo%2fbar"
                .parse()
                .unwrap()
        );

        let addr = from_url("ws://1.2.3.4:1000/").unwrap();
        assert_eq!(addr, "/ip4/1.2.3.4/tcp/1000/ws".parse().unwrap());

        let addr = from_url("wss://1.2.3.4:1000/foo/bar").unwrap();
        assert_eq!(
            addr,
            "/ip4/1.2.3.4/tcp/1000/x-parity-wss/%2ffoo%2fbar"
                .parse()
                .unwrap()
        );

        let addr = from_url("wss://1.2.3.4:1000").unwrap();
        assert_eq!(addr, "/ip4/1.2.3.4/tcp/1000/wss".parse().unwrap());
    }
}