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

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

Closed
rnro opened this issue Oct 2, 2024 · 2 comments · Fixed by #1008
Closed

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

rnro opened this issue Oct 2, 2024 · 2 comments · Fixed by #1008

Comments

@rnro
Copy link

rnro commented Oct 2, 2024

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 added a commit to swift-server/async-http-client that referenced this issue Oct 3, 2024
`Foundation.URL` has various behavior changes in Swift 6 to better match
RFC 3986 which impact AHC.

In particular it now no longer strips the square brackets in IPv6 hosts
which are not tolerated by `inet_pton` so these must be manually
stripped.

swiftlang/swift-foundation#957
swiftlang/swift-foundation#958
swiftlang/swift-foundation#962
@jrflat
Copy link
Contributor

jrflat commented Oct 3, 2024

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.

@YOCKOW
Copy link
Member

YOCKOW commented Oct 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants