Skip to content

Commit d43a53f

Browse files
committed
Add default implementations for three default protocol conformances in the URLSessionDelegate family
Implementing these callbacks without calling the completion handler causes hangs in cases where these methods are called. Add reasonable default behaviors for all of them, to prevent this. This issue has been in place for at least 8 years for one of these callbacks.
1 parent 20967d3 commit d43a53f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/FoundationNetworking/URLSession/URLSessionDelegate.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public protocol URLSessionDelegate : NSObjectProtocol {
7676

7777
extension URLSessionDelegate {
7878
public func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) { }
79-
public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { }
79+
public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
80+
completionHandler(.performDefaultHandling, nil)
81+
}
8082
}
8183

8284
/* If an application has received an
@@ -244,15 +246,19 @@ public protocol URLSessionDataDelegate : URLSessionTaskDelegate {
244246

245247
extension URLSessionDataDelegate {
246248

247-
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) { }
249+
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
250+
completionHandler(.allow)
251+
}
248252

249253
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didBecome downloadTask: URLSessionDownloadTask) { }
250254

251255
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didBecome streamTask: URLSessionStreamTask) { }
252256

253257
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { }
254258

255-
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @escaping (CachedURLResponse?) -> Void) { }
259+
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @escaping (CachedURLResponse?) -> Void) {
260+
completionHandler(proposedResponse)
261+
}
256262
}
257263

258264
/*

0 commit comments

Comments
 (0)