servo / rust-url

URL parser for Rust
https://docs.rs/url/
Apache License 2.0
1.27k stars 318 forks source link

non-special domain is never an `Ipv4` #803

Closed samuelcolvin closed 1 year ago

samuelcolvin commented 1 year ago

The following code fails on the final assert:

use url::{Url, Host};

fn main () {
  let u = Url::parse("http://[::1]").unwrap();
  assert_eq!(u.host(), Some(Host::Ipv6("::1".parse().unwrap())));

  let u = Url::parse("http://127.0.0.1").unwrap();
  assert_eq!(u.host(), Some(Host::Ipv4("127.0.0.1".parse().unwrap())));

  let u = Url::parse("foo://[::1]").unwrap();
  assert_eq!(u.host(), Some(Host::Ipv6("::1".parse().unwrap())));

  let u = Url::parse("foo://127.0.0.1").unwrap();
  assert_eq!(u.host(), Some(Host::Ipv4("127.0.0.1".parse().unwrap())));
}

With:

  left: `Some(Domain("127.0.0.1"))`,
 right: `Some(Ipv4(127.0.0.1))`', src/main.rs:14:3

I think this is a bug, I think the behaviour should be the same as for a v6 ip address.

valenting commented 1 year ago

According to the URL standard non-special URLs are parsed using the opaque host parser. That means the host is just a string, not an IPv4 address.

To exemplify, see parsing foo vs http. http:// rejects an invalid IPv4, whereas foo:// does not (because it parses it as a string)

samuelcolvin commented 1 year ago

Interesting, thanks for clarifying.

Sorry for the noise.