Skip to content

Commit

Permalink
(138168197) Restore URL.host bracket stripping for compatibility (#1008
Browse files Browse the repository at this point in the history
…) (#1023)
  • Loading branch information
jrflat authored Nov 4, 2024
1 parent a28f6ac commit 26f4864
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
41 changes: 29 additions & 12 deletions Sources/FoundationEssentials/URL/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1207,21 +1207,38 @@ public struct URL: Equatable, Sendable, Hashable {
return nil
}
#endif
guard let encodedHost else { return nil }
let didPercentEncodeHost = hasAuthority ? _parseInfo.didPercentEncodeHost : _baseParseInfo?.didPercentEncodeHost ?? false
if percentEncoded {
if didPercentEncodeHost {
return String(encodedHost)
}
guard let decoded = Parser.IDNADecodeHost(encodedHost) else {
guard let encodedHost else {
return nil
}

func requestedHost() -> String? {
let didPercentEncodeHost = hasAuthority ? _parseInfo.didPercentEncodeHost : _baseParseInfo?.didPercentEncodeHost ?? false
if percentEncoded {
if didPercentEncodeHost {
return encodedHost
}
guard let decoded = Parser.IDNADecodeHost(encodedHost) else {
return encodedHost
}
return Parser.percentEncode(decoded, component: .host)
} else {
if didPercentEncodeHost {
return Parser.percentDecode(encodedHost)
}
return encodedHost
}
return Parser.percentEncode(decoded, component: .host)
}

guard let requestedHost = requestedHost() else {
return nil
}

let isIPLiteral = hasAuthority ? _parseInfo.isIPLiteral : _baseParseInfo?.isIPLiteral ?? false
if isIPLiteral {
// Strip square brackets to be compatible with old URL.host behavior
return String(requestedHost.utf8.dropFirst().dropLast())
} else {
if didPercentEncodeHost {
return Parser.percentDecode(encodedHost)
}
return String(encodedHost)
return requestedHost
}
}

Expand Down
36 changes: 36 additions & 0 deletions Tests/FoundationEssentialsTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,42 @@ final class URLTests : XCTestCase {
XCTAssertEqual(url.host, "*.xn--poema-9qae5a.com.br")
}

func testURLHostIPLiteralCompatibility() throws {
var url = URL(string: "http://[::]")!
XCTAssertEqual(url.host, "::")
XCTAssertEqual(url.host(), "::")

url = URL(string: "https://[::1]:433/")!
XCTAssertEqual(url.host, "::1")
XCTAssertEqual(url.host(), "::1")

url = URL(string: "https://[2001:db8::]/")!
XCTAssertEqual(url.host, "2001:db8::")
XCTAssertEqual(url.host(), "2001:db8::")

url = URL(string: "https://[2001:db8::]:433")!
XCTAssertEqual(url.host, "2001:db8::")
XCTAssertEqual(url.host(), "2001:db8::")

url = URL(string: "http://[fe80::a%25en1]")!
XCTAssertEqual(url.absoluteString, "http://[fe80::a%25en1]")
XCTAssertEqual(url.host, "fe80::a%en1")
XCTAssertEqual(url.host(percentEncoded: true), "fe80::a%25en1")
XCTAssertEqual(url.host(percentEncoded: false), "fe80::a%en1")

url = URL(string: "http://[fe80::a%en1]")!
XCTAssertEqual(url.absoluteString, "http://[fe80::a%25en1]")
XCTAssertEqual(url.host, "fe80::a%en1")
XCTAssertEqual(url.host(percentEncoded: true), "fe80::a%25en1")
XCTAssertEqual(url.host(percentEncoded: false), "fe80::a%en1")

url = URL(string: "http://[fe80::a%100%CustomZone]")!
XCTAssertEqual(url.absoluteString, "http://[fe80::a%25100%25CustomZone]")
XCTAssertEqual(url.host, "fe80::a%100%CustomZone")
XCTAssertEqual(url.host(percentEncoded: true), "fe80::a%25100%25CustomZone")
XCTAssertEqual(url.host(percentEncoded: false), "fe80::a%100%CustomZone")
}

func testURLTildeFilePath() throws {
var url = URL(filePath: "~")
// "~" must either be expanded to an absolute path or resolved against a base URL
Expand Down

0 comments on commit 26f4864

Please sign in to comment.