Created
October 1, 2025 23:18
-
-
Save cjnevin/a4626ccf759423b7f0f82294930e182e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func resolve(url: URL) async -> URL? { | |
await withCheckedContinuation { continuation in | |
var request = URLRequest(url: url) | |
request.httpMethod = "HEAD" | |
let task = URLSession.shared.dataTask(with: request) { _, response, error in | |
guard let httpResponse = response as? HTTPURLResponse, error == nil else { | |
continuation.resume(returning: nil) | |
return | |
} | |
if let location = httpResponse.value(forHTTPHeaderField: "Location"), | |
let redirectedURL = URL(string: location) { | |
continuation.resume(returning: redirectedURL) | |
} else { | |
continuation.resume(returning: httpResponse.url) | |
} | |
} | |
task.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment