swift-foundation icon indicating copy to clipboard operation
swift-foundation copied to clipboard

URL.init(string:) handling of IPv6 addresses has changed in Swift 6

Open rnro opened this issue 1 year ago • 1 comments

Initializing a URL from an IPv6 address in Swift 6 on Linux now does not strip enclosing square-brackets. In earlier Swift versions and on macOS the square-brackets are removed.

As well as being a behavior change this can cause issues when passing the host component on e.g. to inet_pton which doesn't tolerate the square braces.

On Linux Swift 5.10:

  1> import Foundation
  2> let u = Foundation.URL(string: "http://[::1]")
  [snip]
  3> print(u!.host)
Optional("::1")

On Linux Swift 6.0.1:

  1> import Foundation
  2> let u = Foundation.URL(string: "http://[::1]")
  [snip]
  3> print(u!.host)
Optional("[::1]")

rnro avatar Oct 02 '24 14:10 rnro

This is expected from the update of URL to RFC 3986, which defines:

host          = IP-literal / IPv4address / reg-name
IP-literal    = "[" ( IPv6address / IPvFuture  ) "]"

with host including the [ and ]. We'd like to maintain this behavior for .host, as it unifies the behaviors of URL.host, URL.host(), and URLComponents.host, but we'll keep an eye out if there's more bincompat issues from this change.

jrflat avatar Oct 03 '24 16:10 jrflat

Something about this that may impact existing code is discrepancy between macOS and Linux: URL(string: "http://[::1]")!.host! is "::1" (without square brackets) on macOS even in Swift 6.

I think we'd better to resolve such discord at least.

YOCKOW avatar Oct 11 '24 08:10 YOCKOW