Skip to content

[6.0.x] Restore URL.host bracket stripping for compatibility #1023

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

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -642,6 +642,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