Skip to content

Instantly share code, notes, and snippets.

@cjnevin
Created October 1, 2025 23:18
Show Gist options
  • Save cjnevin/a4626ccf759423b7f0f82294930e182e to your computer and use it in GitHub Desktop.
Save cjnevin/a4626ccf759423b7f0f82294930e182e to your computer and use it in GitHub Desktop.
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