123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726 |
- import Foundation
- open class SessionDelegate: NSObject {
-
-
- open var sessionDidBecomeInvalidWithError: ((URLSession, Error?) -> Void)?
-
- open var sessionDidReceiveChallenge: ((URLSession, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))?
-
- open var sessionDidReceiveChallengeWithCompletion: ((URLSession, URLAuthenticationChallenge, @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) -> Void)?
-
- open var sessionDidFinishEventsForBackgroundURLSession: ((URLSession) -> Void)?
-
-
- open var taskWillPerformHTTPRedirection: ((URLSession, URLSessionTask, HTTPURLResponse, URLRequest) -> URLRequest?)?
-
-
- open var taskWillPerformHTTPRedirectionWithCompletion: ((URLSession, URLSessionTask, HTTPURLResponse, URLRequest, @escaping (URLRequest?) -> Void) -> Void)?
-
- open var taskDidReceiveChallenge: ((URLSession, URLSessionTask, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))?
-
-
- open var taskDidReceiveChallengeWithCompletion: ((URLSession, URLSessionTask, URLAuthenticationChallenge, @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) -> Void)?
-
- open var taskNeedNewBodyStream: ((URLSession, URLSessionTask) -> InputStream?)?
-
-
- open var taskNeedNewBodyStreamWithCompletion: ((URLSession, URLSessionTask, @escaping (InputStream?) -> Void) -> Void)?
-
- open var taskDidSendBodyData: ((URLSession, URLSessionTask, Int64, Int64, Int64) -> Void)?
-
- open var taskDidComplete: ((URLSession, URLSessionTask, Error?) -> Void)?
-
-
- open var dataTaskDidReceiveResponse: ((URLSession, URLSessionDataTask, URLResponse) -> URLSession.ResponseDisposition)?
-
-
- open var dataTaskDidReceiveResponseWithCompletion: ((URLSession, URLSessionDataTask, URLResponse, @escaping (URLSession.ResponseDisposition) -> Void) -> Void)?
-
- open var dataTaskDidBecomeDownloadTask: ((URLSession, URLSessionDataTask, URLSessionDownloadTask) -> Void)?
-
- open var dataTaskDidReceiveData: ((URLSession, URLSessionDataTask, Data) -> Void)?
-
- open var dataTaskWillCacheResponse: ((URLSession, URLSessionDataTask, CachedURLResponse) -> CachedURLResponse?)?
-
-
- open var dataTaskWillCacheResponseWithCompletion: ((URLSession, URLSessionDataTask, CachedURLResponse, @escaping (CachedURLResponse?) -> Void) -> Void)?
-
-
- open var downloadTaskDidFinishDownloadingToURL: ((URLSession, URLSessionDownloadTask, URL) -> Void)?
-
- open var downloadTaskDidWriteData: ((URLSession, URLSessionDownloadTask, Int64, Int64, Int64) -> Void)?
-
- open var downloadTaskDidResumeAtOffset: ((URLSession, URLSessionDownloadTask, Int64, Int64) -> Void)?
-
- #if !os(watchOS)
-
- @available(iOS 9.0, macOS 10.11, tvOS 9.0, *)
- open var streamTaskReadClosed: ((URLSession, URLSessionStreamTask) -> Void)? {
- get {
- return _streamTaskReadClosed as? (URLSession, URLSessionStreamTask) -> Void
- }
- set {
- _streamTaskReadClosed = newValue
- }
- }
-
- @available(iOS 9.0, macOS 10.11, tvOS 9.0, *)
- open var streamTaskWriteClosed: ((URLSession, URLSessionStreamTask) -> Void)? {
- get {
- return _streamTaskWriteClosed as? (URLSession, URLSessionStreamTask) -> Void
- }
- set {
- _streamTaskWriteClosed = newValue
- }
- }
-
- @available(iOS 9.0, macOS 10.11, tvOS 9.0, *)
- open var streamTaskBetterRouteDiscovered: ((URLSession, URLSessionStreamTask) -> Void)? {
- get {
- return _streamTaskBetterRouteDiscovered as? (URLSession, URLSessionStreamTask) -> Void
- }
- set {
- _streamTaskBetterRouteDiscovered = newValue
- }
- }
-
- @available(iOS 9.0, macOS 10.11, tvOS 9.0, *)
- open var streamTaskDidBecomeInputAndOutputStreams: ((URLSession, URLSessionStreamTask, InputStream, OutputStream) -> Void)? {
- get {
- return _streamTaskDidBecomeInputStream as? (URLSession, URLSessionStreamTask, InputStream, OutputStream) -> Void
- }
- set {
- _streamTaskDidBecomeInputStream = newValue
- }
- }
- var _streamTaskReadClosed: Any?
- var _streamTaskWriteClosed: Any?
- var _streamTaskBetterRouteDiscovered: Any?
- var _streamTaskDidBecomeInputStream: Any?
- #endif
-
- var retrier: RequestRetrier?
- weak var sessionManager: SessionManager?
- var requests: [Int: Request] = [:]
- private let lock = NSLock()
-
- open subscript(task: URLSessionTask) -> Request? {
- get {
- lock.lock() ; defer { lock.unlock() }
- return requests[task.taskIdentifier]
- }
- set {
- lock.lock() ; defer { lock.unlock() }
- requests[task.taskIdentifier] = newValue
- }
- }
-
-
-
-
- public override init() {
- super.init()
- }
-
-
-
-
-
-
-
- open override func responds(to selector: Selector) -> Bool {
- #if !os(macOS)
- if selector == #selector(URLSessionDelegate.urlSessionDidFinishEvents(forBackgroundURLSession:)) {
- return sessionDidFinishEventsForBackgroundURLSession != nil
- }
- #endif
- #if !os(watchOS)
- if #available(iOS 9.0, macOS 10.11, tvOS 9.0, *) {
- switch selector {
- case #selector(URLSessionStreamDelegate.urlSession(_:readClosedFor:)):
- return streamTaskReadClosed != nil
- case #selector(URLSessionStreamDelegate.urlSession(_:writeClosedFor:)):
- return streamTaskWriteClosed != nil
- case #selector(URLSessionStreamDelegate.urlSession(_:betterRouteDiscoveredFor:)):
- return streamTaskBetterRouteDiscovered != nil
- case #selector(URLSessionStreamDelegate.urlSession(_:streamTask:didBecome:outputStream:)):
- return streamTaskDidBecomeInputAndOutputStreams != nil
- default:
- break
- }
- }
- #endif
- switch selector {
- case #selector(URLSessionDelegate.urlSession(_:didBecomeInvalidWithError:)):
- return sessionDidBecomeInvalidWithError != nil
- case #selector(URLSessionDelegate.urlSession(_:didReceive:completionHandler:)):
- return (sessionDidReceiveChallenge != nil || sessionDidReceiveChallengeWithCompletion != nil)
- case #selector(URLSessionTaskDelegate.urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)):
- return (taskWillPerformHTTPRedirection != nil || taskWillPerformHTTPRedirectionWithCompletion != nil)
- case #selector(URLSessionDataDelegate.urlSession(_:dataTask:didReceive:completionHandler:)):
- return (dataTaskDidReceiveResponse != nil || dataTaskDidReceiveResponseWithCompletion != nil)
- default:
- return type(of: self).instancesRespond(to: selector)
- }
- }
- }
- extension SessionDelegate: URLSessionDelegate {
-
-
-
-
- open func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
- sessionDidBecomeInvalidWithError?(session, error)
- }
-
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- didReceive challenge: URLAuthenticationChallenge,
- completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
- {
- guard sessionDidReceiveChallengeWithCompletion == nil else {
- sessionDidReceiveChallengeWithCompletion?(session, challenge, completionHandler)
- return
- }
- var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling
- var credential: URLCredential?
- if let sessionDidReceiveChallenge = sessionDidReceiveChallenge {
- (disposition, credential) = sessionDidReceiveChallenge(session, challenge)
- } else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
- let host = challenge.protectionSpace.host
- if
- let serverTrustPolicy = session.serverTrustPolicyManager?.serverTrustPolicy(forHost: host),
- let serverTrust = challenge.protectionSpace.serverTrust
- {
- if serverTrustPolicy.evaluate(serverTrust, forHost: host) {
- disposition = .useCredential
- credential = URLCredential(trust: serverTrust)
- } else {
- disposition = .cancelAuthenticationChallenge
- }
- }
- }
- completionHandler(disposition, credential)
- }
- #if !os(macOS)
-
-
-
- open func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
- sessionDidFinishEventsForBackgroundURLSession?(session)
- }
- #endif
- }
- extension SessionDelegate: URLSessionTaskDelegate {
-
-
-
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- task: URLSessionTask,
- willPerformHTTPRedirection response: HTTPURLResponse,
- newRequest request: URLRequest,
- completionHandler: @escaping (URLRequest?) -> Void)
- {
- guard taskWillPerformHTTPRedirectionWithCompletion == nil else {
- taskWillPerformHTTPRedirectionWithCompletion?(session, task, response, request, completionHandler)
- return
- }
- var redirectRequest: URLRequest? = request
- if let taskWillPerformHTTPRedirection = taskWillPerformHTTPRedirection {
- redirectRequest = taskWillPerformHTTPRedirection(session, task, response, request)
- }
- completionHandler(redirectRequest)
- }
-
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- task: URLSessionTask,
- didReceive challenge: URLAuthenticationChallenge,
- completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
- {
- guard taskDidReceiveChallengeWithCompletion == nil else {
- taskDidReceiveChallengeWithCompletion?(session, task, challenge, completionHandler)
- return
- }
- if let taskDidReceiveChallenge = taskDidReceiveChallenge {
- let result = taskDidReceiveChallenge(session, task, challenge)
- completionHandler(result.0, result.1)
- } else if let delegate = self[task]?.delegate {
- delegate.urlSession(
- session,
- task: task,
- didReceive: challenge,
- completionHandler: completionHandler
- )
- } else {
- urlSession(session, didReceive: challenge, completionHandler: completionHandler)
- }
- }
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- task: URLSessionTask,
- needNewBodyStream completionHandler: @escaping (InputStream?) -> Void)
- {
- guard taskNeedNewBodyStreamWithCompletion == nil else {
- taskNeedNewBodyStreamWithCompletion?(session, task, completionHandler)
- return
- }
- if let taskNeedNewBodyStream = taskNeedNewBodyStream {
- completionHandler(taskNeedNewBodyStream(session, task))
- } else if let delegate = self[task]?.delegate {
- delegate.urlSession(session, task: task, needNewBodyStream: completionHandler)
- }
- }
-
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- task: URLSessionTask,
- didSendBodyData bytesSent: Int64,
- totalBytesSent: Int64,
- totalBytesExpectedToSend: Int64)
- {
- if let taskDidSendBodyData = taskDidSendBodyData {
- taskDidSendBodyData(session, task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
- } else if let delegate = self[task]?.delegate as? UploadTaskDelegate {
- delegate.URLSession(
- session,
- task: task,
- didSendBodyData: bytesSent,
- totalBytesSent: totalBytesSent,
- totalBytesExpectedToSend: totalBytesExpectedToSend
- )
- }
- }
- #if !os(watchOS)
-
-
-
-
-
- @available(iOS 10.0, macOS 10.12, tvOS 10.0, *)
- @objc(URLSession:task:didFinishCollectingMetrics:)
- open func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
- self[task]?.delegate.metrics = metrics
- }
- #endif
-
-
-
-
-
- open func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
-
- let completeTask: (URLSession, URLSessionTask, Error?) -> Void = { [weak self] session, task, error in
- guard let strongSelf = self else { return }
- strongSelf.taskDidComplete?(session, task, error)
- strongSelf[task]?.delegate.urlSession(session, task: task, didCompleteWithError: error)
- var userInfo: [String: Any] = [Notification.Key.Task: task]
- if let data = (strongSelf[task]?.delegate as? DataTaskDelegate)?.data {
- userInfo[Notification.Key.ResponseData] = data
- }
- NotificationCenter.default.post(
- name: Notification.Name.Task.DidComplete,
- object: strongSelf,
- userInfo: userInfo
- )
- strongSelf[task] = nil
- }
- guard let request = self[task], let sessionManager = sessionManager else {
- completeTask(session, task, error)
- return
- }
-
- request.validations.forEach { $0() }
-
- var error: Error? = error
- if request.delegate.error != nil {
- error = request.delegate.error
- }
-
-
- if let retrier = retrier, let error = error {
- retrier.should(sessionManager, retry: request, with: error) { [weak self] shouldRetry, timeDelay in
- guard shouldRetry else { completeTask(session, task, error) ; return }
- DispatchQueue.utility.after(timeDelay) { [weak self] in
- guard let strongSelf = self else { return }
- let retrySucceeded = strongSelf.sessionManager?.retry(request) ?? false
- if retrySucceeded, let task = request.task {
- strongSelf[task] = request
- return
- } else {
- completeTask(session, task, error)
- }
- }
- }
- } else {
- completeTask(session, task, error)
- }
- }
- }
- extension SessionDelegate: URLSessionDataDelegate {
-
-
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- dataTask: URLSessionDataTask,
- didReceive response: URLResponse,
- completionHandler: @escaping (URLSession.ResponseDisposition) -> Void)
- {
- guard dataTaskDidReceiveResponseWithCompletion == nil else {
- dataTaskDidReceiveResponseWithCompletion?(session, dataTask, response, completionHandler)
- return
- }
- var disposition: URLSession.ResponseDisposition = .allow
- if let dataTaskDidReceiveResponse = dataTaskDidReceiveResponse {
- disposition = dataTaskDidReceiveResponse(session, dataTask, response)
- }
- completionHandler(disposition)
- }
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- dataTask: URLSessionDataTask,
- didBecome downloadTask: URLSessionDownloadTask)
- {
- if let dataTaskDidBecomeDownloadTask = dataTaskDidBecomeDownloadTask {
- dataTaskDidBecomeDownloadTask(session, dataTask, downloadTask)
- } else {
- self[downloadTask]?.delegate = DownloadTaskDelegate(task: downloadTask)
- }
- }
-
-
-
-
-
- open func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
- if let dataTaskDidReceiveData = dataTaskDidReceiveData {
- dataTaskDidReceiveData(session, dataTask, data)
- } else if let delegate = self[dataTask]?.delegate as? DataTaskDelegate {
- delegate.urlSession(session, dataTask: dataTask, didReceive: data)
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- dataTask: URLSessionDataTask,
- willCacheResponse proposedResponse: CachedURLResponse,
- completionHandler: @escaping (CachedURLResponse?) -> Void)
- {
- guard dataTaskWillCacheResponseWithCompletion == nil else {
- dataTaskWillCacheResponseWithCompletion?(session, dataTask, proposedResponse, completionHandler)
- return
- }
- if let dataTaskWillCacheResponse = dataTaskWillCacheResponse {
- completionHandler(dataTaskWillCacheResponse(session, dataTask, proposedResponse))
- } else if let delegate = self[dataTask]?.delegate as? DataTaskDelegate {
- delegate.urlSession(
- session,
- dataTask: dataTask,
- willCacheResponse: proposedResponse,
- completionHandler: completionHandler
- )
- } else {
- completionHandler(proposedResponse)
- }
- }
- }
- extension SessionDelegate: URLSessionDownloadDelegate {
-
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- downloadTask: URLSessionDownloadTask,
- didFinishDownloadingTo location: URL)
- {
- if let downloadTaskDidFinishDownloadingToURL = downloadTaskDidFinishDownloadingToURL {
- downloadTaskDidFinishDownloadingToURL(session, downloadTask, location)
- } else if let delegate = self[downloadTask]?.delegate as? DownloadTaskDelegate {
- delegate.urlSession(session, downloadTask: downloadTask, didFinishDownloadingTo: location)
- }
- }
-
-
-
-
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- downloadTask: URLSessionDownloadTask,
- didWriteData bytesWritten: Int64,
- totalBytesWritten: Int64,
- totalBytesExpectedToWrite: Int64)
- {
- if let downloadTaskDidWriteData = downloadTaskDidWriteData {
- downloadTaskDidWriteData(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
- } else if let delegate = self[downloadTask]?.delegate as? DownloadTaskDelegate {
- delegate.urlSession(
- session,
- downloadTask: downloadTask,
- didWriteData: bytesWritten,
- totalBytesWritten: totalBytesWritten,
- totalBytesExpectedToWrite: totalBytesExpectedToWrite
- )
- }
- }
-
-
-
-
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- downloadTask: URLSessionDownloadTask,
- didResumeAtOffset fileOffset: Int64,
- expectedTotalBytes: Int64)
- {
- if let downloadTaskDidResumeAtOffset = downloadTaskDidResumeAtOffset {
- downloadTaskDidResumeAtOffset(session, downloadTask, fileOffset, expectedTotalBytes)
- } else if let delegate = self[downloadTask]?.delegate as? DownloadTaskDelegate {
- delegate.urlSession(
- session,
- downloadTask: downloadTask,
- didResumeAtOffset: fileOffset,
- expectedTotalBytes: expectedTotalBytes
- )
- }
- }
- }
- #if !os(watchOS)
- @available(iOS 9.0, macOS 10.11, tvOS 9.0, *)
- extension SessionDelegate: URLSessionStreamDelegate {
-
-
-
-
- open func urlSession(_ session: URLSession, readClosedFor streamTask: URLSessionStreamTask) {
- streamTaskReadClosed?(session, streamTask)
- }
-
-
-
-
- open func urlSession(_ session: URLSession, writeClosedFor streamTask: URLSessionStreamTask) {
- streamTaskWriteClosed?(session, streamTask)
- }
-
-
-
-
- open func urlSession(_ session: URLSession, betterRouteDiscoveredFor streamTask: URLSessionStreamTask) {
- streamTaskBetterRouteDiscovered?(session, streamTask)
- }
-
-
-
-
-
-
- open func urlSession(
- _ session: URLSession,
- streamTask: URLSessionStreamTask,
- didBecome inputStream: InputStream,
- outputStream: OutputStream)
- {
- streamTaskDidBecomeInputAndOutputStreams?(session, streamTask, inputStream, outputStream)
- }
- }
- #endif
|