Skip to content

Commit 018d8ef

Browse files
Merge pull request #5022 from apple/urlsessiondelegate-default-implementations
Add default implementations for three default protocol conformances in the URLSessionDelegate family
2 parents 4418280 + 3420746 commit 018d8ef

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Diff for: Sources/FoundationNetworking/URLSession/URLSessionDelegate.swift

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

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

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

245247
extension URLSessionDataDelegate {
246248

247-
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @Sendable @escaping (URLSession.ResponseDisposition) -> Void) { }
249+
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @Sendable @escaping (URLSession.ResponseDisposition) -> Void) {
250+
if self === dataTask.delegate, let sessionDelegate = session.delegate as? URLSessionDataDelegate, self !== sessionDelegate {
251+
sessionDelegate.urlSession(session, dataTask: dataTask, didReceive: response, completionHandler: completionHandler)
252+
} else {
253+
completionHandler(.allow)
254+
}
255+
}
248256

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

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

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

255-
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @Sendable @escaping (CachedURLResponse?) -> Void) { }
263+
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @Sendable @escaping (CachedURLResponse?) -> Void) {
264+
if self === dataTask.delegate, let sessionDelegate = session.delegate as? URLSessionDataDelegate, self !== sessionDelegate {
265+
sessionDelegate.urlSession(session, dataTask: dataTask, willCacheResponse: proposedResponse, completionHandler: completionHandler)
266+
} else {
267+
completionHandler(proposedResponse)
268+
}
269+
}
256270
}
257271

258272
/*

0 commit comments

Comments
 (0)