Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Host::IPv4/IPv6 with single variant taking IpAddr #818

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions url/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::cmp;
use std::fmt::{self, Formatter};
use std::net::{Ipv4Addr, Ipv6Addr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use percent_encoding::{percent_decode, utf8_percent_encode, CONTROLS};
#[cfg(feature = "serde")]
Expand All @@ -30,8 +30,8 @@ impl From<Host<String>> for HostInternal {
match host {
Host::Domain(ref s) if s.is_empty() => HostInternal::None,
Host::Domain(_) => HostInternal::Domain,
Host::Ipv4(address) => HostInternal::Ipv4(address),
Host::Ipv6(address) => HostInternal::Ipv6(address),
Host::Ip(IpAddr::V4(address)) => HostInternal::Ipv4(address),
Host::Ip(IpAddr::V6(address)) => HostInternal::Ipv6(address),
}
}
}
Expand All @@ -45,31 +45,30 @@ pub enum Host<S = String> {
/// a special URL, or percent encoded for non-special URLs. Hosts for
/// non-special URLs are also called opaque hosts.
Domain(S),

/// An IPv4 address.
/// `Url::host_str` returns the serialization of this address,
/// as four decimal integers separated by `.` dots.
Ipv4(Ipv4Addr),

/// An IPv6 address.
/// `Url::host_str` returns the serialization of that address between `[` and `]` brackets,
/// in the format per [RFC 5952 *A Recommendation
/// for IPv6 Address Text Representation*](https://tools.ietf.org/html/rfc5952):
/// Either an IPv4 or IPv6 address. `Url::host_str` returns the serializatin of this address,
/// either as four decimal integers separated by `.` dots for an IPv4 address, or an address
/// between [ and ] brackets, in the format per [RFC 5952 *A Recommendation for IPv6 Address
/// Text Representation*](https://tools.ietf.org/html/rfc5952):
/// lowercase hexadecimal with maximal `::` compression.
Ipv6(Ipv6Addr),
Ip(IpAddr),
}

impl<'a> Host<&'a str> {
/// Return a copy of `self` that owns an allocated `String` but does not borrow an `&Url`.
pub fn to_owned(&self) -> Host<String> {
match *self {
Host::Domain(domain) => Host::Domain(domain.to_owned()),
Host::Ipv4(address) => Host::Ipv4(address),
Host::Ipv6(address) => Host::Ipv6(address),
Host::Ip(address) => Host::Ip(address),
}
}
}

impl<S> From<IpAddr> for Host<S> {
fn from(address: IpAddr) -> Self {
Host::Ip(address)
}
}

impl Host<String> {
/// Parse a host: either an IPv6 address in [] square brackets, or a domain.
///
Expand All @@ -79,7 +78,9 @@ impl Host<String> {
if !input.ends_with(']') {
return Err(ParseError::InvalidIpv6Address);
}
return parse_ipv6addr(&input[1..input.len() - 1]).map(Host::Ipv6);
return parse_ipv6addr(&input[1..input.len() - 1])
.map(IpAddr::V6)
.map(From::from);
}
let domain = percent_decode(input.as_bytes()).decode_utf8_lossy();

Expand Down Expand Up @@ -115,7 +116,7 @@ impl Host<String> {
Err(ParseError::InvalidDomainCharacter)
} else if ends_in_a_number(&domain) {
let address = parse_ipv4addr(&domain)?;
Ok(Host::Ipv4(address))
Ok(Host::Ip(IpAddr::V4(address)))
} else {
Ok(Host::Domain(domain))
}
Expand All @@ -127,7 +128,9 @@ impl Host<String> {
if !input.ends_with(']') {
return Err(ParseError::InvalidIpv6Address);
}
return parse_ipv6addr(&input[1..input.len() - 1]).map(Host::Ipv6);
return parse_ipv6addr(&input[1..input.len() - 1])
.map(IpAddr::V6)
.map(|x| x.into());
}

let is_invalid_host_char = |c| {
Expand Down Expand Up @@ -171,8 +174,8 @@ impl<S: AsRef<str>> fmt::Display for Host<S> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self {
Host::Domain(ref domain) => domain.as_ref().fmt(f),
Host::Ipv4(ref addr) => addr.fmt(f),
Host::Ipv6(ref addr) => {
Host::Ip(IpAddr::V4(ref addr)) => addr.fmt(f),
Host::Ip(IpAddr::V6(ref addr)) => {
f.write_str("[")?;
write_ipv6(addr, f)?;
f.write_str("]")
Expand All @@ -188,8 +191,7 @@ where
fn eq(&self, other: &Host<T>) -> bool {
match (self, other) {
(Host::Domain(a), Host::Domain(b)) => a == b,
(Host::Ipv4(a), Host::Ipv4(b)) => a == b,
(Host::Ipv6(a), Host::Ipv6(b)) => a == b,
(Host::Ip(a), Host::Ip(b)) => a == b,
(_, _) => false,
}
}
Expand Down
15 changes: 5 additions & 10 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ impl Url {
HostInternal::None => assert_eq!(host_str, ""),
HostInternal::Ipv4(address) => assert_eq!(host_str, address.to_string()),
HostInternal::Ipv6(address) => {
let h: Host<String> = Host::Ipv6(address);
let h: Host<String> = Host::Ip(address.into());
assert_eq!(host_str, h.to_string())
}
HostInternal::Domain => {
Expand Down Expand Up @@ -1086,8 +1086,8 @@ impl Url {
match self.host {
HostInternal::None => None,
HostInternal::Domain => Some(Host::Domain(self.slice(self.host_start..self.host_end))),
HostInternal::Ipv4(address) => Some(Host::Ipv4(address)),
HostInternal::Ipv6(address) => Some(Host::Ipv6(address)),
HostInternal::Ipv4(address) => Some(Host::Ip(address.into())),
HostInternal::Ipv6(address) => Some(Host::Ip(address.into())),
}
}

Expand Down Expand Up @@ -1232,8 +1232,7 @@ impl Url {
)?;
Ok(match host {
Host::Domain(domain) => (domain, port).to_socket_addrs()?.collect(),
Host::Ipv4(ip) => vec![(ip, port).into()],
Host::Ipv6(ip) => vec![(ip, port).into()],
Host::Ip(ip) => vec![(ip, port).into()],
})
}

Expand Down Expand Up @@ -2022,11 +2021,7 @@ impl Url {
return Err(());
}

let address = match address {
IpAddr::V4(address) => Host::Ipv4(address),
IpAddr::V6(address) => Host::Ipv6(address),
};
self.set_host_internal(address, None);
self.set_host_internal(address.into(), None);
Ok(())
}

Expand Down
29 changes: 19 additions & 10 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,34 @@ fn host() {
assert_host("http://www.mozilla.org", Host::Domain("www.mozilla.org"));
assert_host(
"http://1.35.33.49",
Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)),
Host::Ip(Ipv4Addr::new(1, 35, 33, 49).into()),
);
assert_host(
"http://[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]",
Host::Ipv6(Ipv6Addr::new(
0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344,
)),
Host::Ip(
Ipv6Addr::new(
0x2001, 0x0db8, 0x85a3, 0x08d3, 0x1319, 0x8a2e, 0x0370, 0x7344,
)
.into(),
),
);
assert_host(
"http://[::]",
Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)),
Host::Ip(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).into()),
);
assert_host(
"http://[::1]",
Host::Ipv6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
Host::Ip(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).into()),
);
assert_host(
"http://0x1.0X23.0x21.061",
Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)),
Host::Ip(Ipv4Addr::new(1, 35, 33, 49).into()),
);
assert_host(
"http://0x1232131",
Host::Ip(Ipv4Addr::new(1, 35, 33, 49).into()),
);
assert_host("http://0x1232131", Host::Ipv4(Ipv4Addr::new(1, 35, 33, 49)));
assert_host("http://111", Host::Ipv4(Ipv4Addr::new(0, 0, 0, 111)));
assert_host("http://111", Host::Ip(Ipv4Addr::new(0, 0, 0, 111).into()));
assert!(Url::parse("http://1.35.+33.49").is_err());
assert!(Url::parse("http://2..2.3").is_err());
assert!(Url::parse("http://42.0x1232131").is_err());
Expand Down Expand Up @@ -780,7 +786,10 @@ fn test_windows_unc_path() {
assert_eq!(url.as_str(), "file://xn--hst-sna/share/path/file.txt");

let url = Url::from_file_path(Path::new(r"\\192.168.0.1\share\path\file.txt")).unwrap();
assert_eq!(url.host(), Some(Host::Ipv4(Ipv4Addr::new(192, 168, 0, 1))));
assert_eq!(
url.host(),
Some(Host::Ip(Ipv4Addr::new(192, 168, 0, 1).into()))
);

let path = url.to_file_path().unwrap();
assert_eq!(path.to_str(), Some(r"\\192.168.0.1\share\path\file.txt"));
Expand Down