Skip to content

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

Closed
@rnro

Description

@rnro

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]")

Activity

added a commit that references this issue on Oct 3, 2024
jrflat

jrflat commented on Oct 3, 2024

@jrflat
Contributor

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

YOCKOW commented on Oct 11, 2024

@YOCKOW
Member

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.

added 2 commits that reference this issue on Apr 1, 2025

Merge pull request #63 from YOCKOW/development

0bbc34b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @YOCKOW@jrflat@rnro

      Issue actions

        URL.init(string:) handling of IPv6 addresses has changed in Swift 6 · Issue #957 · swiftlang/swift-foundation