Blame view

Pods/Alamofire/Source/SessionDelegate.swift 34.7 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
  //
  //  SessionDelegate.swift
  //
  //  Copyright (c) 2014 Alamofire Software Foundation (http://alamofire.org/)
  //
  //  Permission is hereby granted, free of charge, to any person obtaining a copy
  //  of this software and associated documentation files (the "Software"), to deal
  //  in the Software without restriction, including without limitation the rights
  //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  //  copies of the Software, and to permit persons to whom the Software is
  //  furnished to do so, subject to the following conditions:
  //
  //  The above copyright notice and this permission notice shall be included in
  //  all copies or substantial portions of the Software.
  //
  //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  //  THE SOFTWARE.
  //
  
  import Foundation
  
  /// Responsible for handling all delegate callbacks for the underlying session.
  open class SessionDelegate: NSObject {
  
      // MARK: URLSessionDelegate Overrides
  
      /// Overrides default behavior for URLSessionDelegate method `urlSession(_:didBecomeInvalidWithError:)`.
      open var sessionDidBecomeInvalidWithError: ((URLSession, Error?) -> Void)?
  
      /// Overrides default behavior for URLSessionDelegate method `urlSession(_:didReceive:completionHandler:)`.
      open var sessionDidReceiveChallenge: ((URLSession, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))?
  
      /// Overrides all behavior for URLSessionDelegate method `urlSession(_:didReceive:completionHandler:)` and requires the caller to call the `completionHandler`.
      open var sessionDidReceiveChallengeWithCompletion: ((URLSession, URLAuthenticationChallenge, @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) -> Void)?
  
      /// Overrides default behavior for URLSessionDelegate method `urlSessionDidFinishEvents(forBackgroundURLSession:)`.
      open var sessionDidFinishEventsForBackgroundURLSession: ((URLSession) -> Void)?
  
      // MARK: URLSessionTaskDelegate Overrides
  
      /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)`.
      open var taskWillPerformHTTPRedirection: ((URLSession, URLSessionTask, HTTPURLResponse, URLRequest) -> URLRequest?)?
  
      /// Overrides all behavior for URLSessionTaskDelegate method `urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)` and
      /// requires the caller to call the `completionHandler`.
      open var taskWillPerformHTTPRedirectionWithCompletion: ((URLSession, URLSessionTask, HTTPURLResponse, URLRequest, @escaping (URLRequest?) -> Void) -> Void)?
  
      /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:didReceive:completionHandler:)`.
      open var taskDidReceiveChallenge: ((URLSession, URLSessionTask, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))?
  
      /// Overrides all behavior for URLSessionTaskDelegate method `urlSession(_:task:didReceive:completionHandler:)` and
      /// requires the caller to call the `completionHandler`.
      open var taskDidReceiveChallengeWithCompletion: ((URLSession, URLSessionTask, URLAuthenticationChallenge, @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) -> Void)?
  
      /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:needNewBodyStream:)`.
      open var taskNeedNewBodyStream: ((URLSession, URLSessionTask) -> InputStream?)?
  
      /// Overrides all behavior for URLSessionTaskDelegate method `urlSession(_:task:needNewBodyStream:)` and
      /// requires the caller to call the `completionHandler`.
      open var taskNeedNewBodyStreamWithCompletion: ((URLSession, URLSessionTask, @escaping (InputStream?) -> Void) -> Void)?
  
      /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)`.
      open var taskDidSendBodyData: ((URLSession, URLSessionTask, Int64, Int64, Int64) -> Void)?
  
      /// Overrides default behavior for URLSessionTaskDelegate method `urlSession(_:task:didCompleteWithError:)`.
      open var taskDidComplete: ((URLSession, URLSessionTask, Error?) -> Void)?
  
      // MARK: URLSessionDataDelegate Overrides
  
      /// Overrides default behavior for URLSessionDataDelegate method `urlSession(_:dataTask:didReceive:completionHandler:)`.
      open var dataTaskDidReceiveResponse: ((URLSession, URLSessionDataTask, URLResponse) -> URLSession.ResponseDisposition)?
  
      /// Overrides all behavior for URLSessionDataDelegate method `urlSession(_:dataTask:didReceive:completionHandler:)` and
      /// requires caller to call the `completionHandler`.
      open var dataTaskDidReceiveResponseWithCompletion: ((URLSession, URLSessionDataTask, URLResponse, @escaping (URLSession.ResponseDisposition) -> Void) -> Void)?
  
      /// Overrides default behavior for URLSessionDataDelegate method `urlSession(_:dataTask:didBecome:)`.
      open var dataTaskDidBecomeDownloadTask: ((URLSession, URLSessionDataTask, URLSessionDownloadTask) -> Void)?
  
      /// Overrides default behavior for URLSessionDataDelegate method `urlSession(_:dataTask:didReceive:)`.
      open var dataTaskDidReceiveData: ((URLSession, URLSessionDataTask, Data) -> Void)?
  
      /// Overrides default behavior for URLSessionDataDelegate method `urlSession(_:dataTask:willCacheResponse:completionHandler:)`.
      open var dataTaskWillCacheResponse: ((URLSession, URLSessionDataTask, CachedURLResponse) -> CachedURLResponse?)?
  
      /// Overrides all behavior for URLSessionDataDelegate method `urlSession(_:dataTask:willCacheResponse:completionHandler:)` and
      /// requires caller to call the `completionHandler`.
      open var dataTaskWillCacheResponseWithCompletion: ((URLSession, URLSessionDataTask, CachedURLResponse, @escaping (CachedURLResponse?) -> Void) -> Void)?
  
      // MARK: URLSessionDownloadDelegate Overrides
  
      /// Overrides default behavior for URLSessionDownloadDelegate method `urlSession(_:downloadTask:didFinishDownloadingTo:)`.
      open var downloadTaskDidFinishDownloadingToURL: ((URLSession, URLSessionDownloadTask, URL) -> Void)?
  
      /// Overrides default behavior for URLSessionDownloadDelegate method `urlSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)`.
      open var downloadTaskDidWriteData: ((URLSession, URLSessionDownloadTask, Int64, Int64, Int64) -> Void)?
  
      /// Overrides default behavior for URLSessionDownloadDelegate method `urlSession(_:downloadTask:didResumeAtOffset:expectedTotalBytes:)`.
      open var downloadTaskDidResumeAtOffset: ((URLSession, URLSessionDownloadTask, Int64, Int64) -> Void)?
  
      // MARK: URLSessionStreamDelegate Overrides
  
  #if !os(watchOS)
  
      /// Overrides default behavior for URLSessionStreamDelegate method `urlSession(_:readClosedFor:)`.
      @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
          }
      }
  
      /// Overrides default behavior for URLSessionStreamDelegate method `urlSession(_:writeClosedFor:)`.
      @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
          }
      }
  
      /// Overrides default behavior for URLSessionStreamDelegate method `urlSession(_:betterRouteDiscoveredFor:)`.
      @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
          }
      }
  
      /// Overrides default behavior for URLSessionStreamDelegate method `urlSession(_:streamTask:didBecome:outputStream:)`.
      @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
  
      // MARK: Properties
  
      var retrier: RequestRetrier?
      weak var sessionManager: SessionManager?
  
      var requests: [Int: Request] = [:]
      private let lock = NSLock()
  
      /// Access the task delegate for the specified task in a thread-safe manner.
      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
          }
      }
  
      // MARK: Lifecycle
  
      /// Initializes the `SessionDelegate` instance.
      ///
      /// - returns: The new `SessionDelegate` instance.
      public override init() {
          super.init()
      }
  
      // MARK: NSObject Overrides
  
      /// Returns a `Bool` indicating whether the `SessionDelegate` implements or inherits a method that can respond
      /// to a specified message.
      ///
      /// - parameter selector: A selector that identifies a message.
      ///
      /// - returns: `true` if the receiver implements or inherits a method that can respond to selector, otherwise `false`.
      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)
          }
      }
  }
  
  // MARK: - URLSessionDelegate
  
  extension SessionDelegate: URLSessionDelegate {
      /// Tells the delegate that the session has been invalidated.
      ///
      /// - parameter session: The session object that was invalidated.
      /// - parameter error:   The error that caused invalidation, or nil if the invalidation was explicit.
      open func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
          sessionDidBecomeInvalidWithError?(session, error)
      }
  
      /// Requests credentials from the delegate in response to a session-level authentication request from the
      /// remote server.
      ///
      /// - parameter session:           The session containing the task that requested authentication.
      /// - parameter challenge:         An object that contains the request for authentication.
      /// - parameter completionHandler: A handler that your delegate method must call providing the disposition
      ///                                and credential.
      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)
  
      /// Tells the delegate that all messages enqueued for a session have been delivered.
      ///
      /// - parameter session: The session that no longer has any outstanding requests.
      open func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
          sessionDidFinishEventsForBackgroundURLSession?(session)
      }
  
  #endif
  }
  
  // MARK: - URLSessionTaskDelegate
  
  extension SessionDelegate: URLSessionTaskDelegate {
      /// Tells the delegate that the remote server requested an HTTP redirect.
      ///
      /// - parameter session:           The session containing the task whose request resulted in a redirect.
      /// - parameter task:              The task whose request resulted in a redirect.
      /// - parameter response:          An object containing the servers response to the original request.
      /// - parameter request:           A URL request object filled out with the new location.
      /// - parameter completionHandler: A closure that your handler should call with either the value of the request
      ///                                parameter, a modified URL request object, or NULL to refuse the redirect and
      ///                                return the body of the redirect response.
      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)
      }
  
      /// Requests credentials from the delegate in response to an authentication request from the remote server.
      ///
      /// - parameter session:           The session containing the task whose request requires authentication.
      /// - parameter task:              The task whose request requires authentication.
      /// - parameter challenge:         An object that contains the request for authentication.
      /// - parameter completionHandler: A handler that your delegate method must call providing the disposition
      ///                                and credential.
      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)
          }
      }
  
      /// Tells the delegate when a task requires a new request body stream to send to the remote server.
      ///
      /// - parameter session:           The session containing the task that needs a new body stream.
      /// - parameter task:              The task that needs a new body stream.
      /// - parameter completionHandler: A completion handler that your delegate method should call with the new body stream.
      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)
          }
      }
  
      /// Periodically informs the delegate of the progress of sending body content to the server.
      ///
      /// - parameter session:                  The session containing the data task.
      /// - parameter task:                     The data task.
      /// - parameter bytesSent:                The number of bytes sent since the last time this delegate method was called.
      /// - parameter totalBytesSent:           The total number of bytes sent so far.
      /// - parameter totalBytesExpectedToSend: The expected length of the body data.
      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)
  
      /// Tells the delegate that the session finished collecting metrics for the task.
      ///
      /// - parameter session: The session collecting the metrics.
      /// - parameter task:    The task whose metrics have been collected.
      /// - parameter metrics: The collected metrics.
      @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
  
      /// Tells the delegate that the task finished transferring data.
      ///
      /// - parameter session: The session containing the task whose request finished transferring data.
      /// - parameter task:    The task whose request finished transferring data.
      /// - parameter error:   If an error occurred, an error object indicating how the transfer failed, otherwise nil.
      open func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
          /// Executed after it is determined that the request is not going to be retried
          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
          }
  
          // Run all validations on the request before checking if an error occurred
          request.validations.forEach { $0() }
  
          // Determine whether an error has occurred
          var error: Error? = error
  
          if request.delegate.error != nil {
              error = request.delegate.error
          }
  
          /// If an error occurred and the retrier is set, asynchronously ask the retrier if the request
          /// should be retried. Otherwise, complete the task by notifying the task delegate.
          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)
          }
      }
  }
  
  // MARK: - URLSessionDataDelegate
  
  extension SessionDelegate: URLSessionDataDelegate {
      /// Tells the delegate that the data task received the initial reply (headers) from the server.
      ///
      /// - parameter session:           The session containing the data task that received an initial reply.
      /// - parameter dataTask:          The data task that received an initial reply.
      /// - parameter response:          A URL response object populated with headers.
      /// - parameter completionHandler: A completion handler that your code calls to continue the transfer, passing a
      ///                                constant to indicate whether the transfer should continue as a data task or
      ///                                should become a download task.
      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)
      }
  
      /// Tells the delegate that the data task was changed to a download task.
      ///
      /// - parameter session:      The session containing the task that was replaced by a download task.
      /// - parameter dataTask:     The data task that was replaced by a download task.
      /// - parameter downloadTask: The new download task that replaced the data task.
      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)
          }
      }
  
      /// Tells the delegate that the data task has received some of the expected data.
      ///
      /// - parameter session:  The session containing the data task that provided data.
      /// - parameter dataTask: The data task that provided data.
      /// - parameter data:     A data object containing the transferred data.
      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)
          }
      }
  
      /// Asks the delegate whether the data (or upload) task should store the response in the cache.
      ///
      /// - parameter session:           The session containing the data (or upload) task.
      /// - parameter dataTask:          The data (or upload) task.
      /// - parameter proposedResponse:  The default caching behavior. This behavior is determined based on the current
      ///                                caching policy and the values of certain received headers, such as the Pragma
      ///                                and Cache-Control headers.
      /// - parameter completionHandler: A block that your handler must call, providing either the original proposed
      ///                                response, a modified version of that response, or NULL to prevent caching the
      ///                                response. If your delegate implements this method, it must call this completion
      ///                                handler; otherwise, your app leaks memory.
      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)
          }
      }
  }
  
  // MARK: - URLSessionDownloadDelegate
  
  extension SessionDelegate: URLSessionDownloadDelegate {
      /// Tells the delegate that a download task has finished downloading.
      ///
      /// - parameter session:      The session containing the download task that finished.
      /// - parameter downloadTask: The download task that finished.
      /// - parameter location:     A file URL for the temporary file. Because the file is temporary, you must either
      ///                           open the file for reading or move it to a permanent location in your apps sandbox
      ///                           container directory before returning from this delegate method.
      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)
          }
      }
  
      /// Periodically informs the delegate about the downloads progress.
      ///
      /// - parameter session:                   The session containing the download task.
      /// - parameter downloadTask:              The download task.
      /// - parameter bytesWritten:              The number of bytes transferred since the last time this delegate
      ///                                        method was called.
      /// - parameter totalBytesWritten:         The total number of bytes transferred so far.
      /// - parameter totalBytesExpectedToWrite: The expected length of the file, as provided by the Content-Length
      ///                                        header. If this header was not provided, the value is
      ///                                        `NSURLSessionTransferSizeUnknown`.
      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
              )
          }
      }
  
      /// Tells the delegate that the download task has resumed downloading.
      ///
      /// - parameter session:            The session containing the download task that finished.
      /// - parameter downloadTask:       The download task that resumed. See explanation in the discussion.
      /// - parameter fileOffset:         If the file's cache policy or last modified date prevents reuse of the
      ///                                 existing content, then this value is zero. Otherwise, this value is an
      ///                                 integer representing the number of bytes on disk that do not need to be
      ///                                 retrieved again.
      /// - parameter expectedTotalBytes: The expected length of the file, as provided by the Content-Length header.
      ///                                 If this header was not provided, the value is NSURLSessionTransferSizeUnknown.
      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
              )
          }
      }
  }
  
  // MARK: - URLSessionStreamDelegate
  
  #if !os(watchOS)
  
  @available(iOS 9.0, macOS 10.11, tvOS 9.0, *)
  extension SessionDelegate: URLSessionStreamDelegate {
      /// Tells the delegate that the read side of the connection has been closed.
      ///
      /// - parameter session:    The session.
      /// - parameter streamTask: The stream task.
      open func urlSession(_ session: URLSession, readClosedFor streamTask: URLSessionStreamTask) {
          streamTaskReadClosed?(session, streamTask)
      }
  
      /// Tells the delegate that the write side of the connection has been closed.
      ///
      /// - parameter session:    The session.
      /// - parameter streamTask: The stream task.
      open func urlSession(_ session: URLSession, writeClosedFor streamTask: URLSessionStreamTask) {
          streamTaskWriteClosed?(session, streamTask)
      }
  
      /// Tells the delegate that the system has determined that a better route to the host is available.
      ///
      /// - parameter session:    The session.
      /// - parameter streamTask: The stream task.
      open func urlSession(_ session: URLSession, betterRouteDiscoveredFor streamTask: URLSessionStreamTask) {
          streamTaskBetterRouteDiscovered?(session, streamTask)
      }
  
      /// Tells the delegate that the stream task has been completed and provides the unopened stream objects.
      ///
      /// - parameter session:      The session.
      /// - parameter streamTask:   The stream task.
      /// - parameter inputStream:  The new input stream.
      /// - parameter outputStream: The new output stream.
      open func urlSession(
          _ session: URLSession,
          streamTask: URLSessionStreamTask,
          didBecome inputStream: InputStream,
          outputStream: OutputStream)
      {
          streamTaskDidBecomeInputAndOutputStreams?(session, streamTask, inputStream, outputStream)
      }
  }
  
  #endif