Re: [whatwg/url] Expose a URLHost class to JavaScript (#288)

FYI I have implemented this in my Swift library: [documentation](https://karwa.github.io/swift-url/main/documentation/weburl/weburl/host-swift.enum).

For low-level networking applications, you'll find that they often pass the hostname through `inet_pton` to decide whether they have an IP address or should use `getaddrinfo` (or equivalent) to look up the name. That's not really ideal - the URL parser already knows this information, so we can just tell them directly what kind of host they're looking at. At the same time, we don't need to traffic in string values -- we can give them values using a [rich IPv4Address type](https://karwa.github.io/swift-url/main/documentation/weburl/ipv4address). That means things like filtering become a lot easier:

```swift
if case .ipv4Address(let address) = url.host,
   case (10, 0, 0, _) = address.octets {
  // URL has host "10.0.0.???"
}
```

It's quite nice. I'm very happy with it. Possibly less useful on the web, but I could imagine NodeJS could make use of something like this.

Another facet to this API that is quite useful is the ability to parse an opaque hostname in the context of a known URL scheme. In the documentation, I give the example of processing `ssh:` URLs -- the standard says their hostnames are opaque, but in reality, applications will want to process them as if they were part of an `http:` URL (so they get IDNA and IPv4 detection).

```swift
// 🚩 "http:" URLs use a special Unicode -> ASCII conversion
//    (called "IDNA"), designed for compatibility with existing
//    internet infrastructure.

let httpURL = WebURL("http://alice@أهلا.com/data")!
httpURL       // "http://alice@xn--igbi0gl.com/data"
              //               ^^^^^^^^^^^
httpURL.host  // ✅ .domain(Domain { "xn--igbi0gl.com" })

// 🚩 "ssh:" URLs have opaque hostnames, so Unicode characters
//    are just percent-encoded. The URL Standard doesn't even know
//    this a network address, so we don't get any automatic processing.

let sshURL = WebURL("ssh://alice@أهلا.com/data")!
sshURL       // "ssh://alice@%D8%A3%D9%87%D9%84%D8%A7.com/data"
             //              ^^^^^^^^^^^^^^^^^^^^^^^^
sshURL.host  // 😐 .opaque("%D8%A3%D9%87%D9%84%D8%A7.com")

// 🚩 Using the WebURL.Host initializer, we can interpret our
//    SSH hostname as if it were in an HTTP URL.

let sshAsHttp = WebURL.Host(sshURL.hostname!, scheme: "http")
// ✅ .domain(Domain { "xn--igbi0gl.com" })
```

IPv4 support:

```swift
let url = WebURL("ssh://user@192.168.15.21/data")!
url       // "ssh://user@192.168.15.21/data"
url.host  // 😐 .opaque("192.168.15.21")
          //     ^^^^^^

WebURL.Host(url.hostname!, scheme: "http")
// ✅ .ipv4Address(IPv4Address { 192.168.15.21 })
```

Scenarios like that may be more broadly useful on the web.


-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/pull/288#issuecomment-1715926421
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/url/pull/288/c1715926421@github.com>

Received on Tuesday, 12 September 2023 15:17:55 UTC