Skip to content

Commit

Permalink
fix: avoid potential thread crashes of url
Browse files Browse the repository at this point in the history
Internal error in Foundation URL.host(percentEncoded:)
  • Loading branch information
ACTCD committed Sep 11, 2024
1 parent 16afc42 commit b50a23e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions xcode/Shared/UrlPolyfill.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,23 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin
guard let url = _url else {
return nil
}

guard let scheme = url.scheme else {
return nil
}
/*
The issue still exists as of macOS 14.4, iOS 17.0
https://stackoverflow.com/questions/74445926/url-host-deprecated-but-replacement-crashes
https://forums.swift.org/t/does-url-query-percentencoded-calls-url-host-percentencoded-under-the-hood/70452
https://forums.developer.apple.com/forums/thread/722451
*/
guard let hostname = url.host else {
return nil
}
var port = (url.port == nil) ? "" : String(url.port!)
if (scheme == "http" && port == "80") { port = "" }
if (scheme == "https" && port == "443") { port = "" }
if #available(macOS 13.0, iOS 16.0, *) {
let hostname = url.host(percentEncoded: true) ?? ""
// let hostname = url.host(percentEncoded: true) ?? ""
let host = (port == "") ? hostname : "\(hostname):\(port)"
let query = url.query(percentEncoded: true) ?? ""
let fragment = url.fragment(percentEncoded: true) ?? ""
Expand All @@ -75,7 +83,6 @@ func jsLikeURL(_ urlString: String, baseString: String? = nil) -> [String: Strin
"username": url.user(percentEncoded: true) ?? ""
]
} else {
let hostname = url.host ?? ""
let host = (port == "") ? hostname : "\(hostname):\(port)"
let query = url.query ?? ""
let fragment = url.fragment ?? ""
Expand Down

0 comments on commit b50a23e

Please # to comment.