Skip to main content

web_url/parse/pre_path/
host.rs

1use crate::parse::Error;
2use crate::parse::Error::InvalidHost;
3use address::{Domain, IPAddress, IPv4Address, IPv6Address};
4use std::str::FromStr;
5
6/// Parses the host string from the prefix of `s`.
7///
8/// The host will **not** be validated.
9///
10/// Returns `(host_string, rest_of_s)`.
11pub fn parse_host(s: &str) -> (&str, &str) {
12    let host_and_port: &str = if let Some(slash) = s.as_bytes().iter().position(|c| *c == b'/') {
13        &s[..slash]
14    } else {
15        s
16    };
17    if host_and_port.is_empty() {
18        ("", s)
19    } else {
20        let bracketed: bool = host_and_port.as_bytes()[0] == b'['
21            && host_and_port.as_bytes()[host_and_port.len() - 1] == b']';
22        if bracketed {
23            (host_and_port, &s[host_and_port.len()..])
24        } else if let Some(last_colon) = host_and_port.as_bytes().iter().rposition(|c| *c == b':') {
25            s.split_at(last_colon)
26        } else {
27            (host_and_port, &s[host_and_port.len()..])
28        }
29    }
30}
31
32/// Parses the optional IP address from the `host` string.
33/// If the host is not an IP address the domain will be validated (case-insensitively).
34///
35/// Returns `Ok(Some(ip_address))` if the `host` is an IP address.
36/// Returns `Ok(None)` if the `host` is a domain name.
37/// Returns `Err(InvalidHost)` if the `host` is invalid.
38pub fn parse_ip_and_validate_domain(host: &str) -> Result<Option<IPAddress>, Error> {
39    if host.is_empty() {
40        Err(InvalidHost)
41    } else if host.as_bytes()[0] == b'[' {
42        if host.as_bytes()[host.len() - 1] != b']' {
43            Err(InvalidHost)
44        } else {
45            let host: &str = &host[1..(host.len() - 1)];
46            let ip: IPv6Address = IPv6Address::from_str(host).map_err(|_| InvalidHost)?;
47            Ok(Some(ip.to_ip()))
48        }
49    } else if let Ok(ip) = IPv4Address::from_str(host) {
50        Ok(Some(ip.to_ip()))
51    } else if Domain::is_valid_name_str(host, true) {
52        Ok(None)
53    } else {
54        Err(InvalidHost)
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use address::{IPAddress, IPv4Address, IPv6Address};
61
62    use crate::parse::pre_path::{parse_host, parse_ip_and_validate_domain};
63    use crate::parse::Error;
64    use crate::parse::Error::InvalidHost;
65
66    #[test]
67    fn fn_extract_host() {
68        let test_cases: &[(&str, (&str, &str))] = &[
69            ("", ("", "")),
70            ("host", ("host", "")),
71            ("host/", ("host", "/")),
72            ("host/rest", ("host", "/rest")),
73            ("host:port/rest", ("host", ":port/rest")),
74            ("[host:port/rest", ("[host", ":port/rest")),
75            ("[host:port]/rest", ("[host:port]", "/rest")),
76            ("[host:port]", ("[host:port]", "")),
77            ("[host:port]80", ("[host", ":port]80")),
78            ("host:", ("host", ":")),
79        ];
80        for (s, expected) in test_cases {
81            let result: (&str, &str) = parse_host(s);
82            assert_eq!(result, *expected, "s={}", s);
83        }
84    }
85
86    #[test]
87    fn fn_parse_ip() {
88        let test_cases: &[(&str, Result<Option<IPAddress>, Error>)] = &[
89            ("", Err(InvalidHost)),
90            ("[::1", Err(InvalidHost)),
91            ("[127.0.0.1]", Err(InvalidHost)),
92            ("[::1]", Ok(Some(IPv6Address::LOCALHOST.to_ip()))),
93            ("!", Err(InvalidHost)),
94            ("127.0.0.1", Ok(Some(IPv4Address::LOCALHOST.to_ip()))),
95            ("localhost", Ok(None)),
96            ("LocalHost", Ok(None)),
97            ("Local!Host", Err(InvalidHost)),
98        ];
99        for (host, expected) in test_cases {
100            let result: Result<Option<IPAddress>, Error> = parse_ip_and_validate_domain(host);
101            assert_eq!(result, *expected, "host={}", *host);
102        }
103    }
104}